Fix: add missing destroy functions to queues/stack APIs
[userspace-rcu.git] / urcu / lfstack.h
CommitLineData
d3bfcb24
MD
1#ifndef _URCU_LFSTACK_H
2#define _URCU_LFSTACK_H
3
4/*
5 * lfstack.h
6 *
7 * Userspace RCU library - Lock-Free Stack
8 *
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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.
15 *
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.
20 *
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
24 */
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
7294103b
MD
30#include <stdbool.h>
31#include <pthread.h>
32
33/*
34 * Lock-free stack.
35 *
36 * Stack implementing push, pop, pop_all operations, as well as iterator
37 * on the stack head returned by pop_all.
38 *
39 * Synchronization table:
40 *
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 "-".
44 *
45 * cds_lfs_push __cds_lfs_pop __cds_lfs_pop_all
46 * cds_lfs_push - - -
47 * __cds_lfs_pop - X X
48 * __cds_lfs_pop_all - X -
49 *
50 * cds_lfs_pop_blocking and cds_lfs_pop_all_blocking use an internal
51 * mutex to provide synchronization.
52 */
53
54/*
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.
58 */
d3bfcb24
MD
59struct cds_lfs_node {
60 struct cds_lfs_node *next;
61};
62
7294103b
MD
63/*
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
67 * both types.
68 */
69struct cds_lfs_head {
70 struct cds_lfs_node node;
71};
72
d3bfcb24 73struct cds_lfs_stack {
7294103b
MD
74 struct cds_lfs_head *head;
75 pthread_mutex_t lock;
d3bfcb24
MD
76};
77
78#ifdef _LGPL_SOURCE
79
80#include <urcu/static/lfstack.h>
81
82#define cds_lfs_node_init _cds_lfs_node_init
83#define cds_lfs_init _cds_lfs_init
93aee570 84#define cds_lfs_destroy _cds_lfs_destroy
cd4075b4 85#define __cds_lfs_init ___cds_lfs_init
7294103b 86#define cds_lfs_empty _cds_lfs_empty
d3bfcb24 87#define cds_lfs_push _cds_lfs_push
7294103b
MD
88
89/* Locking performed internally */
90#define cds_lfs_pop_blocking _cds_lfs_pop_blocking
91#define cds_lfs_pop_all_blocking _cds_lfs_pop_all_blocking
92
93/* Synchronize pop with internal mutex */
94#define cds_lfs_pop_lock _cds_lfs_pop_lock
95#define cds_lfs_pop_unlock _cds_lfs_pop_unlock
96
97/* Synchronization ensured by the caller. See synchronization table. */
98#define __cds_lfs_pop ___cds_lfs_pop
99#define __cds_lfs_pop_all ___cds_lfs_pop_all
d3bfcb24
MD
100
101#else /* !_LGPL_SOURCE */
102
7294103b
MD
103/*
104 * cds_lfs_node_init: initialize lock-free stack node.
105 */
d3bfcb24 106extern void cds_lfs_node_init(struct cds_lfs_node *node);
7294103b
MD
107
108/*
93aee570
MD
109 * cds_lfs_init: initialize lock-free stack (with locking). Pair with
110 * cds_lfs_destroy().
7294103b 111 */
d3bfcb24
MD
112extern void cds_lfs_init(struct cds_lfs_stack *s);
113
93aee570
MD
114/*
115 * cds_lfs_destroy: destroy lock-free stack (with lock). Pair with
116 * cds_lfs_init().
117 */
118extern void cds_lfs_destroy(struct cds_lfs_stack *s);
119
7294103b
MD
120/*
121 * cds_lfs_empty: return whether lock-free stack is empty.
122 *
123 * No memory barrier is issued. No mutual exclusion is required.
124 */
125extern bool cds_lfs_empty(struct cds_lfs_stack *s);
126
d3bfcb24
MD
127/*
128 * cds_lfs_push: push a node into the stack.
129 *
130 * Does not require any synchronization with other push nor pop.
131 *
132 * Returns 0 if the stack was empty prior to adding the node.
133 * Returns non-zero otherwise.
134 */
7294103b 135extern bool cds_lfs_push(struct cds_lfs_stack *s,
d3bfcb24
MD
136 struct cds_lfs_node *node);
137
138/*
7294103b
MD
139 * cds_lfs_pop_blocking: pop a node from the stack.
140 *
141 * Calls __cds_lfs_pop with an internal pop mutex held.
142 */
143extern struct cds_lfs_node *cds_lfs_pop_blocking(struct cds_lfs_stack *s);
144
145/*
146 * cds_lfs_pop_all_blocking: pop all nodes from a stack.
147 *
148 * Calls __cds_lfs_pop_all with an internal pop mutex held.
149 */
150extern struct cds_lfs_head *cds_lfs_pop_all_blocking(struct cds_lfs_stack *s);
151
152/*
153 * cds_lfs_pop_lock: lock stack pop-protection mutex.
154 */
155extern void cds_lfs_pop_lock(struct cds_lfs_stack *s);
156
157/*
158 * cds_lfs_pop_unlock: unlock stack pop-protection mutex.
159 */
160extern void cds_lfs_pop_unlock(struct cds_lfs_stack *s);
161
162/*
163 * __cds_lfs_pop: pop a node from the stack.
d3bfcb24
MD
164 *
165 * Returns NULL if stack is empty.
166 *
7294103b 167 * __cds_lfs_pop needs to be synchronized using one of the following
d3bfcb24
MD
168 * techniques:
169 *
7294103b 170 * 1) Calling __cds_lfs_pop under rcu read lock critical section. The
d3bfcb24
MD
171 * caller must wait for a grace period to pass before freeing the
172 * returned node or modifying the cds_lfs_node structure.
7294103b
MD
173 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop
174 * and __cds_lfs_pop_all callers.
175 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
176 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
d3bfcb24 177 */
7294103b
MD
178extern struct cds_lfs_node *__cds_lfs_pop(struct cds_lfs_stack *s);
179
180/*
181 * __cds_lfs_pop_all: pop all nodes from a stack.
182 *
183 * __cds_lfs_pop_all does not require any synchronization with other
184 * push, nor with other __cds_lfs_pop_all, but requires synchronization
185 * matching the technique used to synchronize __cds_lfs_pop:
186 *
187 * 1) If __cds_lfs_pop is called under rcu read lock critical section,
188 * both __cds_lfs_pop and cds_lfs_pop_all callers must wait for a
189 * grace period to pass before freeing the returned node or modifying
190 * the cds_lfs_node structure. However, no RCU read-side critical
191 * section is needed around __cds_lfs_pop_all.
192 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop and
193 * __cds_lfs_pop_all callers.
194 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
195 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
196 */
197extern struct cds_lfs_head *__cds_lfs_pop_all(struct cds_lfs_stack *s);
d3bfcb24
MD
198
199#endif /* !_LGPL_SOURCE */
200
7294103b
MD
201/*
202 * cds_lfs_for_each: Iterate over all nodes returned by
203 * __cds_lfs_pop_all.
204 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
205 * @__node: node to use as iterator (struct cds_lfs_node pointer).
206 *
207 * Content written into each node before push is guaranteed to be
208 * consistent, but no other memory ordering is ensured.
209 */
210#define cds_lfs_for_each(__head, __node) \
211 for (__node = &__head->node; \
212 __node != NULL; \
213 __node = __node->next)
214
215/*
216 * cds_lfs_for_each_safe: Iterate over all nodes returned by
217 * __cds_lfs_pop_all, safe against node deletion.
218 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
219 * @__node: node to use as iterator (struct cds_lfs_node pointer).
220 * @__n: struct cds_lfs_node pointer holding the next pointer (used
221 * internally).
222 *
223 * Content written into each node before push is guaranteed to be
224 * consistent, but no other memory ordering is ensured.
225 */
226#define cds_lfs_for_each_safe(__head, __node, __n) \
227 for (__node = &__head->node, __n = (__node ? __node->next : NULL); \
228 __node != NULL; \
229 __node = __n, __n = (__node ? __node->next : NULL))
230
d3bfcb24
MD
231#ifdef __cplusplus
232}
233#endif
234
235#endif /* _URCU_LFSTACK_H */
This page took 0.034728 seconds and 4 git commands to generate.