X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=urcu%2Fstatic%2Frculfstack.h;h=3473ccef82299b0b41559734e75377b2ac0f1941;hp=99d3d4ccc92f8cd22011d099417986e274e08d73;hb=a59f39055b5ecb77b68cf78b9839aa9e8e4ec332;hpb=a2e7bf9ce5de5113c7f59c380b0087e291cd603d diff --git a/urcu/static/rculfstack.h b/urcu/static/rculfstack.h index 99d3d4c..3473cce 100644 --- a/urcu/static/rculfstack.h +++ b/urcu/static/rculfstack.h @@ -27,22 +27,51 @@ */ #include -/* A urcu implementation header should be already included. */ +#include #ifdef __cplusplus extern "C" { #endif +static inline void _cds_lfs_node_init_rcu(struct cds_lfs_node_rcu *node) { } +static inline void _cds_lfs_init_rcu(struct cds_lfs_stack_rcu *s) { s->head = NULL; } -void _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, struct cds_lfs_node_rcu *node) +/* + * Lock-free stack push is not subject to ABA problem, so no need to + * take the RCU read-side lock. Even if "head" changes between two + * uatomic_cmpxchg() invocations here (being popped, and then pushed + * again by one or more concurrent threads), the second + * uatomic_cmpxchg() invocation only cares about pushing a new entry at + * the head of the stack, ensuring consistency by making sure the new + * node->next is the same pointer value as the value replaced as head. + * It does not care about the content of the actual next node, so it can + * very well be reallocated between the two uatomic_cmpxchg(). + * + * We take the approach of expecting the stack to be usually empty, so + * we first try an initial uatomic_cmpxchg() on a NULL old_head, and + * retry if the old head was non-NULL (the value read by the first + * uatomic_cmpxchg() is used as old head for the following loop). The + * upside of this scheme is to minimize the amount of cacheline traffic, + * always performing an exclusive cacheline access, rather than doing + * non-exclusive followed by exclusive cacheline access (which would be + * required if we first read the old head value). This design decision + * might be revisited after more throrough benchmarking on various + * platforms. + * + * Returns 0 if the stack was empty prior to adding the node. + * Returns non-zero otherwise. + */ +static inline +int _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, + struct cds_lfs_node_rcu *node) { struct cds_lfs_node_rcu *head = NULL; @@ -58,6 +87,7 @@ void _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, struct cds_lfs_node_rcu *nod if (old_head == head) break; } + return (int) !!((unsigned long) head); } /* @@ -67,6 +97,7 @@ void _cds_lfs_push_rcu(struct cds_lfs_stack_rcu *s, struct cds_lfs_node_rcu *nod * node or modifying the cds_lfs_node_rcu structure. * Returns NULL if stack is empty. */ +static inline struct cds_lfs_node_rcu * _cds_lfs_pop_rcu(struct cds_lfs_stack_rcu *s) {