diff --git a/libcfs/include/libcfs/posix/libcfs.h b/libcfs/include/libcfs/posix/libcfs.h index 8509eb4d265cc17029dd2ebb5d92e030c12b727d..b50554f76e0bbec292eaf09cd240572eec5a9b17 100644 --- a/libcfs/include/libcfs/posix/libcfs.h +++ b/libcfs/include/libcfs/posix/libcfs.h @@ -221,7 +221,7 @@ static inline void MODULE_AUTHOR(char *name) #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name) #define MODULE_LICENSE(name) MODULE_AUTHOR(name) -#define THIS_MODULE NULL +#define THIS_MODULE (void *)0x11111 #define __init #define __exit @@ -265,7 +265,7 @@ typedef int (*shrinker_t)(int, unsigned int); static inline struct shrinker *set_shrinker(int seeks, shrinker_t shrinkert) { - return NULL; + return (struct shrinker *)0xdeadbea1; // Cannot return NULL here } static inline void remove_shrinker(struct shrinker *shrinker) @@ -283,63 +283,87 @@ static inline void remove_shrinker(struct shrinker *shrinker) ***************************************************************************/ struct radix_tree_root { - struct list_head *rnode; + struct list_head list; + void *rnode; }; +struct radix_tree_node { + struct list_head _node; + unsigned long index; + void *item; +}; + #define RADIX_TREE_INIT(mask) { \ - .rnode = NULL, \ + NOT_IMPLEMENTED \ } #define RADIX_TREE(name, mask) \ struct radix_tree_root name = RADIX_TREE_INIT(mask) -#define INIT_RADIX_TREE(root, mask) \ -do { \ - (root)->rnode = NULL; \ + +#define INIT_RADIX_TREE(root, mask) \ +do { \ + CFS_INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list); \ + ((struct radix_tree_root *)root)->rnode = NULL; \ } while (0) static inline int radix_tree_insert(struct radix_tree_root *root, - unsigned long idx, struct page *page) + unsigned long idx, void *item) { - if (root->rnode == NULL) - root->rnode = &page->_node; - else - list_add_tail(&page->_node, root->rnode); + struct radix_tree_node *node; + node = malloc(sizeof(*node)); + if (!node) + return -ENOMEM; + + CFS_INIT_LIST_HEAD(&node->_node); + node->index = idx; + node->item = item; + list_add_tail(&node->_node, &root->list); + root->rnode = (void *)1001; return 0; } -static inline void *radix_tree_lookup(struct radix_tree_root *root, +static inline struct radix_tree_node *radix_tree_lookup0(struct radix_tree_root *root, unsigned long idx) { - struct page *p; + struct radix_tree_node *node; - if (root->rnode == NULL) + if (list_empty(&root->list)) return NULL; - p = list_entry(root->rnode, struct page, _node); - if (p->index == idx) - return p; - - list_for_each_entry(p, root->rnode, _node) - if (p->index == idx) - return p; + list_for_each_entry(node, &root->list, _node) + if (node->index == idx) + return node; return NULL; } +static inline void *radix_tree_lookup(struct radix_tree_root *root, + unsigned long idx) +{ + struct radix_tree_node *node = radix_tree_lookup0(root, idx); + + if (node) + return node->item; + return node; +} + static inline void *radix_tree_delete(struct radix_tree_root *root, unsigned long idx) { - struct page *p = radix_tree_lookup(root, idx); + struct radix_tree_node *p = radix_tree_lookup0(root, idx); + void *item; if (p == NULL) return NULL; - if (list_empty(root->rnode)) - root->rnode = NULL; - else if (root->rnode == &p->_node) - root->rnode = p->_node.next; + list_del_init(&p->_node); - return p; + item = p->item; + free(p); + if (list_empty(&root->list)) + root->rnode = NULL; + + return item; } static inline unsigned int diff --git a/libcfs/include/libcfs/user-lock.h b/libcfs/include/libcfs/user-lock.h index 6f4e145a6fe7f1603ca31b84a7970dbb74ca57ba..2a384635b853bfe24416ad121ca88b71ca4b882f 100644 --- a/libcfs/include/libcfs/user-lock.h +++ b/libcfs/include/libcfs/user-lock.h @@ -146,7 +146,8 @@ struct completion { unsigned int done; cfs_waitq_t wait; }; - +typedef int (*cfs_wait_handler_t) (int timeout); +void set_completion_wait_handler(cfs_wait_handler_t *handler); void init_completion(struct completion *c); void complete(struct completion *c); void wait_for_completion(struct completion *c); @@ -279,6 +280,8 @@ struct mutex { struct semaphore m_sem; }; +#define DEFINE_MUTEX(m) struct mutex m + static inline void mutex_init(struct mutex *mutex) { init_mutex(&mutex->m_sem); @@ -297,13 +300,27 @@ static inline void mutex_unlock(struct mutex *mutex) /** * Try-lock this mutex. * - * \retval 1 try-lock succeeded. * - * \retval 0 try-lock failed. + * \retval 0 try-lock succeeded (lock acquired). + * \retval errno indicates lock contention. + */ +static inline int mutex_down_trylock(struct mutex *mutex) +{ + return 0; +} + +/** + * Try-lock this mutex. + * + * Note, return values are negation of what is expected from down_trylock() or + * pthread_mutex_trylock(). + * + * \retval 1 try-lock succeeded (lock acquired). + * \retval 0 indicates lock contention. */ static inline int mutex_trylock(struct mutex *mutex) { - return 1; + return !mutex_down_trylock(mutex); } static inline void mutex_destroy(struct mutex *lock) diff --git a/libcfs/libcfs/user-lock.c b/libcfs/libcfs/user-lock.c index 1f392f6a9ef554a733914d8fba4a1188528082ef..53ab2c447a94581640915af0846a9df3cb513198 100644 --- a/libcfs/libcfs/user-lock.c +++ b/libcfs/libcfs/user-lock.c @@ -147,6 +147,13 @@ void __up(struct semaphore *s) * - wait_for_completion(c) */ +static cfs_wait_handler_t wait_handler; + +void init_completion_module(cfs_wait_handler_t handler) +{ + wait_handler = handler; +} + void init_completion(struct completion *c) { LASSERT(c != NULL); @@ -164,11 +171,23 @@ void complete(struct completion *c) void wait_for_completion(struct completion *c) { LASSERT(c != NULL); + do { + if (wait_handler) + wait_handler(1000); + else + break; + } while (c->done == 0); } int wait_for_completion_interruptible(struct completion *c) { LASSERT(c != NULL); + do { + if (wait_handler) + wait_handler(1000); + else + break; + } while (c->done == 0); return 0; }