/** Openat system call * * \todo Implement flags and mode tests. */ int sys_openat(int dirfd, uaddr_t u_pathname, int flags, mode_t mode) { int fd; char pathname[ MAX_PATHLEN ]; struct file *file; struct inode * root_inode = NULL; if( strncpy_from_user( pathname, (void*) u_pathname, sizeof(pathname) ) < 0 ) return -EFAULT; //printk("sys_openat(%s): %d %08x %03x\n", pathname, dirfd, flags, mode); dbg( "name='%s' dirfd=%d flags %x mode %x\n", pathname, dirfd, flags, mode); //_KDBG( "name='%s' flags %x mode %x\n", pathname, flags, mode); if ((pathname[0] == '/') || (dirfd == AT_FDCWD)) { root_inode = kfs_root; } else { struct file * root_file = get_current_file(dirfd); if (!root_file) { return -EBADF; } root_inode = root_file->inode; } // FIX ME __lock(&_lock); if ( ! kfs_lookup( root_inode, pathname, 0 ) && ( flags & O_CREAT ) ) { extern struct kfs_fops in_mem_fops; extern struct inode_operations in_mem_iops; if ( kfs_create_at( root_inode, pathname, &in_mem_iops, &in_mem_fops, 0777, 0, 0 ) == NULL ) { fd = -EFAULT; goto out; } } if((fd = kfs_open_path_at(root_inode, pathname, flags, mode, &file)) < 0) { //printk("sys_openat failed : %s (%d)\n",pathname,fd); goto out; } fd = fdTableGetUnused( current->fdTable ); fdTableInstallFd( current->fdTable, fd, file ); // TODO XXX - check to see if the file's path identifies it as a pipe, and set the pipe stuff dbg("name=`%s` fd=%d \n", pathname, fd ); out: __unlock(&_lock); return fd; }
static ssize_t kfs_default_read(struct file * dir, char * buf, size_t len, loff_t * off ) { size_t offset = 0; struct inode * inode; __lock(&_lock); struct htable_iter iter = htable_iter( dir->inode->files ); while( (inode = htable_next( &iter )) ) { int rc = snprintf( (char*)buf + offset, len - offset, "%s\n", inode->name ); if( rc > len - offset ) { offset = len-1; goto done; } offset += rc; } done: __unlock(&_lock); return offset; }
int pthread_setschedprio(pthread_t t, int prio) { int r; __lock(t->killlock); r = t->dead ? ESRCH : -__syscall(SYS_sched_setparam, t->tid, &prio); __unlock(t->killlock); return r; }
int pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param) { int r; __lock(t->killlock); r = t->dead ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, ¶m); __unlock(t->killlock); return r; }
static void return_to_pool(message_t *msg) { /*** NB: there's an implicit s-l-o-w SW multiply in msg-mpool !!! */ uint16_t mask = _bmaps[msg - mpool]; __lock(); bmask |= mask; __unlock(); }
void pilo::core::threading::recursive_mutex::unlock() { if (!--_m_recursion_count) { PILO_WIN_INTERLOCKED_EXCHANGE(&_m_locking_thread_id, 0); __unlock(); } }
/* Return a random integer between 0 and RAND_MAX inclusive. */ int rand(void) { int n; __lock(); n = rand_r(&_seed); __unlock(); return n; }
int kfs_readdir(struct file * filp, uaddr_t buf, unsigned int count, dirent_filler f) { struct inode *child; __lock(&_lock); struct htable_iter iter = htable_iter( filp->inode->files ); int res = 0, rv = 0, i; /* for directories, we just increase pos by 1 for every child we successfully fill into the dirent buffer; linux uses 0 and 1 for . and .., then inodes for the rest */ if(filp->pos == 0) { rv = f(buf, count, filp->inode, ".", 1, filp->pos + 1, 4 /* DT_DIR */); if(rv < 0) goto out; res += rv; count -= rv; filp->pos++; buf += rv; } if(filp->pos == 1) { rv = f(buf, count, filp->inode, "..", 2, filp->pos + 1, 4 /* DT_DIR */); if(rv < 0) goto out; res += rv; count -= rv; filp->pos++; buf += rv; } i = 2; while( (child = htable_next( &iter )) ) { if(filp->pos > i++) continue; rv = f(buf, count, child, child->name, strlen(child->name), filp->pos + 1, (child->mode >> 12) & 15); if(rv < 0) goto out; res += rv; count -= rv; filp->pos++; buf += rv; } out: __unlock(&_lock); if(rv < 0 && (rv != -EINVAL || res == 0)) return rv; return res; }
void hash_map(struct hash *h, void (*fn)(struct hashelem *obj)) { __lock(h); for(size_t index = 0;index < h->length;index++) { if(h->table[index]) linkedlist_apply_data(h->table[index], __fnjmp, fn); } __unlock(h); }
static void max14688_det_work (struct work_struct *work) { struct max14688 *me = container_of(work, struct max14688, det_work.work); int micZ, leftZ; if(earjack_detect == true) { log_dbg("duplication intterupt already connect earjack\n"); return; } else { earjack_detect = true; } __lock(me); if (unlikely(!me->detect_jack(me->dev))) { log_warn("no jack in detection work\n"); earjack_detect = false; goto out; } max14688_update_status(me); /* Read MIC and L-line impedences */ micZ = me->read_mic_impedence(me->dev); leftZ = me->read_left_impedence(me->dev); log_dbg("%s[micZ = %d, leftZ = %d\n", __func__, micZ, leftZ); /* Look up jack matching impedence ranges */ max14688_lookup_jack(me, micZ, leftZ); if (unlikely(!__present_valid_jack(me))) { earjack_detect = false; goto no_match_found; } max14688_write_mode0(me, __current_jack_mode0(me)); max14688_write_mode1(me, __current_jack_mode1(me)); if (__current_jack_has_button(me)) { max14688_enable_irq(me, IRQ_SWD); } else { max14688_disable_irq(me, IRQ_SWD); } log_dbg("jack %s inserted\n", __current_jack_name(me)); me->report_jack(me->dev, __current_jack(me), JACK_IN_VALUE); goto out; no_match_found: /* Handle exception */ log_err("unknown jack - mic %d, left %d\n", micZ, leftZ); out: __unlock(me); return; }
/*** return msg to pool (you'd better have gotten it from get_from_pool :-) */ static void return_to_pool(message_t *msg) { message_item_t *mi; __lock(); mi = (message_item_t *) (((char *) msg) - msg_ofs); mi->next = mpool_head; mpool_head = mi; __unlock(); }
int array_count(struct array *a) { int retval; __lock(a); retval = a->a_items; __unlock(a); return retval; }
int hash_delete(struct hash *h, const void *key, size_t keylen) { __lock(h); size_t index = __hashfn(key, keylen, h->length); if(h->table[index] == NULL) { __unlock(h); return -ENOENT; } struct hashelem tmp; tmp.key = key; tmp.keylen = keylen; struct linkedentry *ent = linkedlist_find(h->table[index], __ll_check_exist, &tmp); if(ent) { linkedlist_remove(h->table[index], ent); h->count--; } __unlock(h); return ent ? 0 : -ENOENT; }
void array_free(struct array *a) { __lock(a); if (a->a_arr) free(a->a_arr); a->a_arr = NULL; a->a_size = 0; a->a_items = 0; __unlock(a); }
void *hash_lookup(struct hash *h, const void *key, size_t keylen) { __lock(h); size_t index = __hashfn(key, keylen, h->length); if(h->table[index] == NULL) { __unlock(h); return NULL; } struct hashelem tmp; tmp.key = key; tmp.keylen = keylen; struct linkedentry *ent = linkedlist_find(h->table[index], __ll_check_exist, &tmp); void *ret = NULL; if(ent) { struct hashelem *elem = ent->obj; ret = elem->ptr; } __unlock(h); return ret; }
static ssize_t max14688_log_level_store (struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct max14688 *me = dev_get_drvdata(dev); __lock(me); max14688_log_level = (int)simple_strtol(buf, NULL, 10); __unlock(me); return (ssize_t)count; }
void *array_at(struct array *a, int nr) { void *retval; __lock(a); if (nr < 0 || nr >= a->a_size) retval = NULL; else retval = a->a_arr[nr].ai_data; __unlock(a); return retval; }
void* thread_fct(void *arg) { int id = *((int*)arg); int j=0; for (j=0 ; j<NB_LOOP ; j++) { __lock(&global_lock, id); /* critical section */ global_var++; __unlock(&global_lock, id); } pthread_exit(0); }
static ssize_t max14688_status_store (struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct max14688 *me = dev_get_drvdata(dev); __lock(me); max14688_update_status(me); __unlock(me); return (ssize_t)count; }
/*** grab msg from pool -- returns NULL if none are available */ static message_t *get_from_pool(void) { message_item_t *rec; message_t *ret = NULL; __lock(); if ((rec = mpool_head) != NULL) { mpool_head = rec->next; ret = &(rec->msg); } __unlock(); return ret; }
int hash_insert(struct hash *h, const void *key, size_t keylen, struct hashelem *elem, void *data) { __lock(h); size_t index = __hashfn(key, keylen, h->length); elem->ptr = data; elem->key = key; elem->keylen = keylen; if(h->table[index] == NULL) { /* lazy-init the buckets */ h->table[index] = linkedlist_create(0, LINKEDLIST_LOCKLESS); } else { struct linkedentry *ent = linkedlist_find(h->table[index], __ll_check_exist, elem); if(ent) { __unlock(h); return -EEXIST; } } linkedlist_insert(h->table[index], &elem->entry, elem); h->count++; __unlock(h); return 0; }
static ssize_t max14688_log_level_show (struct device *dev, struct device_attribute *devattr, char *buf) { struct max14688 *me = dev_get_drvdata(dev); int rc; __lock(me); rc = (int)snprintf(buf, PAGE_SIZE, "%d\n", max14688_log_level); __unlock(me); return (ssize_t)rc; }
int sys_stat( const char *path, uaddr_t buf) { int ret = -EBADF; __lock(&_lock); struct inode * const inode = kfs_lookup(NULL, path, 0); if( !inode ) goto out; ret = kfs_stat(inode, buf); out: __unlock(&_lock); return ret; }
void *array_put_at(struct array *a, int nr, void *m) { void *retval; __lock(a); if (nr < 0 || nr >= a->a_size) retval = NULL; else { update_a_items(a, nr, m); retval = a->a_arr[nr].ai_data; a->a_arr[nr].ai_data = m; } __unlock(a); return retval; }
void *array_pop(struct array *a) { struct array_item *ai; void *retval = NULL; __lock(a); for (ai = a->a_arr; ai < a->a_arr + a->a_size; ai++) { if (ai->ai_data) { retval = ai->ai_data; ai->ai_data = NULL; a->a_items--; break; } } __unlock(a); return retval; }
void *array_iter_get(struct array_iterator *ai) { struct array *a; void *retval; retval = NULL; a = ai->i_array; __lock(a); while (++ai->i_pos < a->a_size) { if (a->a_arr[ai->i_pos].ai_data) { retval = a->a_arr[ai->i_pos].ai_data; break; } } __unlock(a); return retval; }
static message_t *get_from_pool(void) { uint16_t item; message_t *ret = NULL; __lock(); item = bmask & (~bmask + 1); /* grab mask for lowest 1-bit in bmask */ bmask ^= item; /* and turn it off in bmask */ __unlock(); if (item) { ret = mpool; /* linear search for result */ while (item != 1) { item >>= 1; ret++; } } return ret; }
int array_put(struct array *a, void *m) { struct array_item *ai; int retval; __lock(a); need_space(a); for (ai = a->a_arr; ai < a->a_arr + a->a_size; ai++) { if (ai->ai_data == NULL) { ai->ai_data = m; a->a_items++; break; } } retval = ai - a->a_arr; __unlock(a); return retval; }
int sys_fstat(int fd, uaddr_t buf) { int ret = -EBADF; struct file * const file = get_current_file( fd ); if( !file ) goto out; __lock(&_lock); if(file->inode) ret = kfs_stat(file->inode, buf); else printk(KERN_WARNING "Attempting fstat() on fd %d with no backing inode.\n", fd); out: __unlock(&_lock); return ret; }
void *array_remove(struct array *a, void *m) { struct array_item *ai; void *retval; __lock(a); if (!m) retval = NULL; else { retval = NULL; for (ai = a->a_arr; ai < a->a_arr + a->a_size; ai++) if (ai->ai_data == m) { ai->ai_data = NULL; a->a_items--; retval = m; break; } } __unlock(a); return retval; }