X-Git-Url: https://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=urcu%2Fstatic%2Flfstack.h;h=05d0bb478b8b169a03864272810d1b5c76bfa4f8;hp=0be959436e7d4577c4e6e7673dad6b49fae74469;hb=200d100e05ed8e10c47f971939042f2406df92ef;hpb=ffa11a1830c532f3b052146eb9f0dd450cb2a0f2 diff --git a/urcu/static/lfstack.h b/urcu/static/lfstack.h index 0be9594..05d0bb4 100644 --- a/urcu/static/lfstack.h +++ b/urcu/static/lfstack.h @@ -66,7 +66,8 @@ void _cds_lfs_node_init(struct cds_lfs_node *node) } /* - * cds_lfs_init: initialize lock-free stack. + * cds_lfs_init: initialize lock-free stack (with lock). Pair with + * cds_lfs_destroy(). */ static inline void _cds_lfs_init(struct cds_lfs_stack *s) @@ -78,6 +79,27 @@ void _cds_lfs_init(struct cds_lfs_stack *s) assert(!ret); } +/* + * cds_lfs_destroy: destroy lock-free stack (with lock). Pair with + * cds_lfs_init(). + */ +static inline +void _cds_lfs_destroy(struct cds_lfs_stack *s) +{ + int ret = pthread_mutex_destroy(&s->lock); + assert(!ret); +} + +/* + * ___cds_lfs_init: initialize lock-free stack (without lock). + * Don't pair with any destroy function. + */ +static inline +void ___cds_lfs_init(struct __cds_lfs_stack *s) +{ + s->head = NULL; +} + static inline bool ___cds_lfs_empty_head(struct cds_lfs_head *head) { @@ -90,9 +112,9 @@ bool ___cds_lfs_empty_head(struct cds_lfs_head *head) * No memory barrier is issued. No mutual exclusion is required. */ static inline -bool _cds_lfs_empty(struct cds_lfs_stack *s) +bool _cds_lfs_empty(cds_lfs_stack_ptr_t s) { - return ___cds_lfs_empty_head(CMM_LOAD_SHARED(s->head)); + return ___cds_lfs_empty_head(CMM_LOAD_SHARED(s._s->head)); } /* @@ -125,9 +147,10 @@ bool _cds_lfs_empty(struct cds_lfs_stack *s) * Returns non-zero otherwise. */ static inline -bool _cds_lfs_push(struct cds_lfs_stack *s, +bool _cds_lfs_push(cds_lfs_stack_ptr_t u_s, struct cds_lfs_node *node) { + struct __cds_lfs_stack *s = u_s._s; struct cds_lfs_head *head = NULL; struct cds_lfs_head *new_head = caa_container_of(node, struct cds_lfs_head, node); @@ -148,7 +171,7 @@ bool _cds_lfs_push(struct cds_lfs_stack *s, if (old_head == head) break; } - return ___cds_lfs_empty_head(head); + return !___cds_lfs_empty_head(head); } /* @@ -159,17 +182,23 @@ bool _cds_lfs_push(struct cds_lfs_stack *s, * __cds_lfs_pop needs to be synchronized using one of the following * techniques: * - * 1) Calling __cds_lfs_pop under rcu read lock critical section. The - * caller must wait for a grace period to pass before freeing the - * returned node or modifying the cds_lfs_node structure. + * 1) Calling __cds_lfs_pop under rcu read lock critical section. + * Both __cds_lfs_pop and __cds_lfs_pop_all callers must wait for a + * grace period to pass before freeing the returned node or pushing + * the node back into the stack. It is valid to overwrite the content + * of cds_lfs_node immediately after __cds_lfs_pop and + * __cds_lfs_pop_all. No RCU read-side critical section is needed + * around __cds_lfs_pop_all. * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop * and __cds_lfs_pop_all callers. * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme). */ static inline -struct cds_lfs_node *___cds_lfs_pop(struct cds_lfs_stack *s) +struct cds_lfs_node *___cds_lfs_pop(cds_lfs_stack_ptr_t u_s) { + struct __cds_lfs_stack *s = u_s._s; + for (;;) { struct cds_lfs_head *head, *next_head; struct cds_lfs_node *next; @@ -201,18 +230,22 @@ struct cds_lfs_node *___cds_lfs_pop(struct cds_lfs_stack *s) * matching the technique used to synchronize __cds_lfs_pop: * * 1) If __cds_lfs_pop is called under rcu read lock critical section, - * both __cds_lfs_pop and cds_lfs_pop_all callers must wait for a - * grace period to pass before freeing the returned node or modifying - * the cds_lfs_node structure. However, no RCU read-side critical - * section is needed around __cds_lfs_pop_all. + * both __cds_lfs_pop and __cds_lfs_pop_all callers must wait for a + * grace period to pass before freeing the returned node or pushing + * the node back into the stack. It is valid to overwrite the content + * of cds_lfs_node immediately after __cds_lfs_pop and + * __cds_lfs_pop_all. No RCU read-side critical section is needed + * around __cds_lfs_pop_all. * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop and * __cds_lfs_pop_all callers. * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme). */ static inline -struct cds_lfs_head *___cds_lfs_pop_all(struct cds_lfs_stack *s) +struct cds_lfs_head *___cds_lfs_pop_all(cds_lfs_stack_ptr_t u_s) { + struct __cds_lfs_stack *s = u_s._s; + /* * Implicit memory barrier after uatomic_xchg() matches implicit * memory barrier before uatomic_cmpxchg() in cds_lfs_push. It