1 #ifndef _URCU_LFSTACK_H
2 #define _URCU_LFSTACK_H
7 * Userspace RCU library - Lock-Free Stack
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 * Stack implementing push, pop, pop_all operations, as well as iterator
37 * on the stack head returned by pop_all.
39 * Synchronization table:
41 * External synchronization techniques described in the API below is
42 * required between pairs marked with "X". No external synchronization
43 * required between pairs marked with "-".
45 * cds_lfs_push __cds_lfs_pop __cds_lfs_pop_all
48 * __cds_lfs_pop_all - X -
50 * cds_lfs_pop_blocking and cds_lfs_pop_all_blocking use an internal
51 * mutex to provide synchronization.
55 * struct cds_lfs_node is returned by cds_lfs_pop, and also used as
56 * iterator on stack. It is not safe to dereference the node next
57 * pointer when returned by cds_lfs_pop.
60 struct cds_lfs_node
*next
;
64 * struct cds_lfs_head is returned by __cds_lfs_pop_all, and can be used
65 * to begin iteration on the stack. "node" needs to be the first field
66 * of cds_lfs_head, so the end-of-stack pointer value can be used for
70 struct cds_lfs_node node
;
73 struct cds_lfs_stack
{
74 struct cds_lfs_head
*head
;
80 #include <urcu/static/lfstack.h>
82 #define cds_lfs_node_init _cds_lfs_node_init
83 #define cds_lfs_init _cds_lfs_init
84 #define __cds_lfs_init ___cds_lfs_init
85 #define cds_lfs_empty _cds_lfs_empty
86 #define cds_lfs_push _cds_lfs_push
88 /* Locking performed internally */
89 #define cds_lfs_pop_blocking _cds_lfs_pop_blocking
90 #define cds_lfs_pop_all_blocking _cds_lfs_pop_all_blocking
92 /* Synchronize pop with internal mutex */
93 #define cds_lfs_pop_lock _cds_lfs_pop_lock
94 #define cds_lfs_pop_unlock _cds_lfs_pop_unlock
96 /* Synchronization ensured by the caller. See synchronization table. */
97 #define __cds_lfs_pop ___cds_lfs_pop
98 #define __cds_lfs_pop_all ___cds_lfs_pop_all
100 #else /* !_LGPL_SOURCE */
103 * cds_lfs_node_init: initialize lock-free stack node.
105 extern void cds_lfs_node_init(struct cds_lfs_node
*node
);
108 * cds_lfs_init: initialize lock-free stack.
110 extern void cds_lfs_init(struct cds_lfs_stack
*s
);
113 * cds_lfs_empty: return whether lock-free stack is empty.
115 * No memory barrier is issued. No mutual exclusion is required.
117 extern bool cds_lfs_empty(struct cds_lfs_stack
*s
);
120 * cds_lfs_push: push a node into the stack.
122 * Does not require any synchronization with other push nor pop.
124 * Returns 0 if the stack was empty prior to adding the node.
125 * Returns non-zero otherwise.
127 extern bool cds_lfs_push(struct cds_lfs_stack
*s
,
128 struct cds_lfs_node
*node
);
131 * cds_lfs_pop_blocking: pop a node from the stack.
133 * Calls __cds_lfs_pop with an internal pop mutex held.
135 extern struct cds_lfs_node
*cds_lfs_pop_blocking(struct cds_lfs_stack
*s
);
138 * cds_lfs_pop_all_blocking: pop all nodes from a stack.
140 * Calls __cds_lfs_pop_all with an internal pop mutex held.
142 extern struct cds_lfs_head
*cds_lfs_pop_all_blocking(struct cds_lfs_stack
*s
);
145 * cds_lfs_pop_lock: lock stack pop-protection mutex.
147 extern void cds_lfs_pop_lock(struct cds_lfs_stack
*s
);
150 * cds_lfs_pop_unlock: unlock stack pop-protection mutex.
152 extern void cds_lfs_pop_unlock(struct cds_lfs_stack
*s
);
155 * __cds_lfs_pop: pop a node from the stack.
157 * Returns NULL if stack is empty.
159 * __cds_lfs_pop needs to be synchronized using one of the following
162 * 1) Calling __cds_lfs_pop under rcu read lock critical section. The
163 * caller must wait for a grace period to pass before freeing the
164 * returned node or modifying the cds_lfs_node structure.
165 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop
166 * and __cds_lfs_pop_all callers.
167 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
168 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
170 extern struct cds_lfs_node
*__cds_lfs_pop(struct cds_lfs_stack
*s
);
173 * __cds_lfs_pop_all: pop all nodes from a stack.
175 * __cds_lfs_pop_all does not require any synchronization with other
176 * push, nor with other __cds_lfs_pop_all, but requires synchronization
177 * matching the technique used to synchronize __cds_lfs_pop:
179 * 1) If __cds_lfs_pop is called under rcu read lock critical section,
180 * both __cds_lfs_pop and cds_lfs_pop_all callers must wait for a
181 * grace period to pass before freeing the returned node or modifying
182 * the cds_lfs_node structure. However, no RCU read-side critical
183 * section is needed around __cds_lfs_pop_all.
184 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop and
185 * __cds_lfs_pop_all callers.
186 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
187 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
189 extern struct cds_lfs_head
*__cds_lfs_pop_all(struct cds_lfs_stack
*s
);
191 #endif /* !_LGPL_SOURCE */
194 * cds_lfs_for_each: Iterate over all nodes returned by
196 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
197 * @__node: node to use as iterator (struct cds_lfs_node pointer).
199 * Content written into each node before push is guaranteed to be
200 * consistent, but no other memory ordering is ensured.
202 #define cds_lfs_for_each(__head, __node) \
203 for (__node = &__head->node; \
205 __node = __node->next)
208 * cds_lfs_for_each_safe: Iterate over all nodes returned by
209 * __cds_lfs_pop_all, safe against node deletion.
210 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
211 * @__node: node to use as iterator (struct cds_lfs_node pointer).
212 * @__n: struct cds_lfs_node pointer holding the next pointer (used
215 * Content written into each node before push is guaranteed to be
216 * consistent, but no other memory ordering is ensured.
218 #define cds_lfs_for_each_safe(__head, __node, __n) \
219 for (__node = &__head->node, __n = (__node ? __node->next : NULL); \
221 __node = __n, __n = (__node ? __node->next : NULL))
227 #endif /* _URCU_LFSTACK_H */
This page took 0.037452 seconds and 5 git commands to generate.