X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=urcu%2Fwfstack.h;fp=urcu%2Fwfstack.h;h=b6992e888cfa71f2334c485034c11f7a788b9fac;hp=db2ee0c005a85b261997bc10a3a1c72bcb73a809;hb=edac6b69ad8b690437c4624cebb7eef465a09a7e;hpb=e7e6ff7f5b1a64ddd280652fea736c98f2e9fd41 diff --git a/urcu/wfstack.h b/urcu/wfstack.h index db2ee0c..b6992e8 100644 --- a/urcu/wfstack.h +++ b/urcu/wfstack.h @@ -2,9 +2,9 @@ #define _URCU_WFSTACK_H /* - * wfstack.h + * urcu/wfstack.h * - * Userspace RCU library - Stack with Wait-Free push, Blocking pop. + * Userspace RCU library - Stack with wait-free push, blocking traversal. * * Copyright 2010 - Mathieu Desnoyers * @@ -25,18 +25,59 @@ #include #include +#include #include #ifdef __cplusplus extern "C" { #endif +/* + * Stack with wait-free push, blocking traversal. + * + * Stack implementing push, pop, pop_all operations, as well as iterator + * on the stack head returned by pop_all. + * + * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all. + * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, iteration on stack + * head returned by pop_all. + * + * Synchronization table: + * + * External synchronization techniques described in the API below is + * required between pairs marked with "X". No external synchronization + * required between pairs marked with "-". + * + * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all + * cds_wfs_push - - - + * __cds_wfs_pop - X X + * __cds_wfs_pop_all - X - + * + * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide + * synchronization. + */ + +/* + * struct cds_wfs_node is returned by __cds_wfs_pop, and also used as + * iterator on stack. It is not safe to dereference the node next + * pointer when returned by __cds_wfs_pop_blocking. + */ struct cds_wfs_node { struct cds_wfs_node *next; }; +/* + * struct cds_wfs_head is returned by __cds_wfs_pop_all, and can be used + * to begin iteration on the stack. "node" needs to be the first field of + * cds_wfs_head, so the end-of-stack pointer value can be used for both + * types. + */ +struct cds_wfs_head { + struct cds_wfs_node node; +}; + struct cds_wfs_stack { - struct cds_wfs_node *head; + struct cds_wfs_head *head; pthread_mutex_t lock; }; @@ -45,24 +86,179 @@ struct cds_wfs_stack { #include #define cds_wfs_node_init _cds_wfs_node_init -#define cds_wfs_init _cds_wfs_init -#define cds_wfs_push _cds_wfs_push -#define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking -#define cds_wfs_pop_blocking _cds_wfs_pop_blocking +#define cds_wfs_init _cds_wfs_init +#define cds_wfs_empty _cds_wfs_empty +#define cds_wfs_push _cds_wfs_push + +/* Locking performed internally */ +#define cds_wfs_pop_blocking _cds_wfs_pop_blocking +#define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking + +/* + * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or + * cds_wfs_pop_all_blocking. + */ +#define cds_wfs_first_blocking _cds_wfs_first_blocking +#define cds_wfs_next_blocking _cds_wfs_next_blocking + +/* Pop locking with internal mutex */ +#define cds_wfs_pop_lock _cds_wfs_pop_lock +#define cds_wfs_pop_unlock _cds_wfs_pop_unlock + +/* Synchronization ensured by the caller. See synchronization table. */ +#define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking +#define __cds_wfs_pop_all ___cds_wfs_pop_all #else /* !_LGPL_SOURCE */ +/* + * cds_wfs_node_init: initialize wait-free stack node. + */ extern void cds_wfs_node_init(struct cds_wfs_node *node); + +/* + * cds_wfs_init: initialize wait-free stack. + */ extern void cds_wfs_init(struct cds_wfs_stack *s); + +/* + * cds_wfs_empty: return whether wait-free stack is empty. + * + * No memory barrier is issued. No mutual exclusion is required. + */ +extern bool cds_wfs_empty(struct cds_wfs_stack *s); + +/* + * cds_wfs_push: push a node into the stack. + * + * Issues a full memory barrier before push. No mutual exclusion is + * required. + * + * Returns 0 if the stack was empty prior to adding the node. + * Returns non-zero otherwise. + */ extern int cds_wfs_push(struct cds_wfs_stack *s, struct cds_wfs_node *node); -/* __cds_wfs_pop_blocking: caller ensures mutual exclusion between pops */ -extern struct cds_wfs_node *__cds_wfs_pop_blocking(struct cds_wfs_stack *s); + +/* + * cds_wfs_pop_blocking: pop a node from the stack. + * + * Calls __cds_wfs_pop_blocking with an internal pop mutex held. + */ extern struct cds_wfs_node *cds_wfs_pop_blocking(struct cds_wfs_stack *s); +/* + * cds_wfs_pop_all_blocking: pop all nodes from a stack. + * + * Calls __cds_wfs_pop_all with an internal pop mutex held. + */ +extern struct cds_wfs_head *cds_wfs_pop_all_blocking(struct cds_wfs_stack *s); + +/* + * cds_wfs_first_blocking: get first node of a popped stack. + * + * Content written into the node before enqueue is guaranteed to be + * consistent, but no other memory ordering is ensured. + * + * Used by for-like iteration macros in urcu/wfstack.h: + * cds_wfs_for_each_blocking() + * cds_wfs_for_each_blocking_safe() + */ +extern struct cds_wfs_node *cds_wfs_first_blocking(struct cds_wfs_head *head); + +/* + * cds_wfs_next_blocking: get next node of a popped stack. + * + * Content written into the node before enqueue is guaranteed to be + * consistent, but no other memory ordering is ensured. + * + * Used by for-like iteration macros in urcu/wfstack.h: + * cds_wfs_for_each_blocking() + * cds_wfs_for_each_blocking_safe() + */ +extern struct cds_wfs_node *cds_wfs_next_blocking(struct cds_wfs_node *node); + +/* + * cds_wfs_pop_lock: lock stack pop-protection mutex. + */ +extern void cds_wfs_pop_lock(struct cds_wfs_stack *s); + +/* + * cds_wfs_pop_unlock: unlock stack pop-protection mutex. + */ +extern void cds_wfs_pop_unlock(struct cds_wfs_stack *s); + +/* + * __cds_wfs_pop_blocking: pop a node from the stack. + * + * Returns NULL if stack is empty. + * + * __cds_wfs_pop_blocking needs to be synchronized using one of the + * following techniques: + * + * 1) Calling __cds_wfs_pop_blocking 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_wfs_node structure. + * 2) Using mutual exclusion (e.g. mutexes) to protect + * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers. + * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking() + * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme). + */ +extern struct cds_wfs_node *__cds_wfs_pop_blocking(struct cds_wfs_stack *s); + +/* + * __cds_wfs_pop_all: pop all nodes from a stack. + * + * __cds_wfs_pop_all does not require any synchronization with other + * push, nor with other __cds_wfs_pop_all, but requires synchronization + * matching the technique used to synchronize __cds_wfs_pop_blocking: + * + * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical + * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers + * must wait for a grace period to pass before freeing the returned + * node or modifying the cds_wfs_node structure. However, no RCU + * read-side critical section is needed around __cds_wfs_pop_all. + * 2) Using mutual exclusion (e.g. mutexes) to protect + * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers. + * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking() + * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme). + */ +extern struct cds_wfs_head *__cds_wfs_pop_all(struct cds_wfs_stack *s); + #endif /* !_LGPL_SOURCE */ #ifdef __cplusplus } #endif +/* + * cds_wfs_for_each_blocking: Iterate over all nodes returned by + * __cds_wfs_pop_all(). + * @head: head of the queue (struct cds_wfs_head pointer). + * @node: iterator (struct cds_wfs_node pointer). + * + * Content written into each node before enqueue is guaranteed to be + * consistent, but no other memory ordering is ensured. + */ +#define cds_wfs_for_each_blocking(head, node) \ + for (node = cds_wfs_first_blocking(head); \ + node != NULL; \ + node = cds_wfs_next_blocking(node)) + +/* + * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by + * __cds_wfs_pop_all(). Safe against deletion. + * @head: head of the queue (struct cds_wfs_head pointer). + * @node: iterator (struct cds_wfs_node pointer). + * @n: struct cds_wfs_node pointer holding the next pointer (used + * internally). + * + * Content written into each node before enqueue is guaranteed to be + * consistent, but no other memory ordering is ensured. + */ +#define cds_wfs_for_each_blocking_safe(head, node, n) \ + for (node = cds_wfs_first_blocking(head), \ + n = (node ? cds_wfs_next_blocking(node) : NULL); \ + node != NULL; \ + node = n, n = (node ? cds_wfs_next_blocking(node) : NULL)) + #endif /* _URCU_WFSTACK_H */