Commit | Line | Data |
---|---|---|
8ad4ce58 MD |
1 | #ifndef _URCU_WFCQUEUE_STATIC_H |
2 | #define _URCU_WFCQUEUE_STATIC_H | |
3 | ||
4 | /* | |
5 | * wfcqueue-static.h | |
6 | * | |
7 | * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue | |
8 | * | |
9 | * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See wfcqueue.h for linking | |
10 | * dynamically with the userspace rcu library. | |
11 | * | |
12 | * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
13 | * Copyright 2011-2012 - Lai Jiangshan <laijs@cn.fujitsu.com> | |
14 | * | |
15 | * This library is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU Lesser General Public | |
17 | * License as published by the Free Software Foundation; either | |
18 | * version 2.1 of the License, or (at your option) any later version. | |
19 | * | |
20 | * This library is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
23 | * Lesser General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU Lesser General Public | |
26 | * License along with this library; if not, write to the Free Software | |
27 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
28 | */ | |
29 | ||
30 | #include <pthread.h> | |
31 | #include <assert.h> | |
32 | #include <poll.h> | |
33 | #include <stdbool.h> | |
34 | #include <urcu/compiler.h> | |
35 | #include <urcu/uatomic.h> | |
36 | ||
37 | #ifdef __cplusplus | |
38 | extern "C" { | |
39 | #endif | |
40 | ||
41 | /* | |
42 | * Concurrent queue with wait-free enqueue/blocking dequeue. | |
43 | * | |
44 | * Inspired from half-wait-free/half-blocking queue implementation done by | |
45 | * Paul E. McKenney. | |
46 | * | |
47 | * Mutual exclusion of __cds_wfcq_* API | |
48 | * | |
49 | * Unless otherwise stated, the caller must ensure mutual exclusion of | |
50 | * queue update operations "dequeue" and "splice" (for source queue). | |
51 | * Queue read operations "first" and "next" need to be protected against | |
52 | * concurrent "dequeue" and "splice" (for source queue) by the caller. | |
53 | * "enqueue", "splice" (for destination queue), and "empty" are the only | |
54 | * operations that can be used without any mutual exclusion. | |
55 | * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock(). | |
56 | * | |
57 | * For convenience, cds_wfcq_dequeue_blocking() and | |
58 | * cds_wfcq_splice_blocking() hold the dequeue lock. | |
59 | */ | |
60 | ||
61 | #define WFCQ_ADAPT_ATTEMPTS 10 /* Retry if being set */ | |
62 | #define WFCQ_WAIT 10 /* Wait 10 ms if being set */ | |
63 | ||
64 | /* | |
65 | * cds_wfcq_node_init: initialize wait-free queue node. | |
66 | */ | |
67 | static inline void _cds_wfcq_node_init(struct cds_wfcq_node *node) | |
68 | { | |
69 | node->next = NULL; | |
70 | } | |
71 | ||
72 | /* | |
73 | * cds_wfcq_init: initialize wait-free queue. | |
74 | */ | |
75 | static inline void _cds_wfcq_init(struct cds_wfcq_head *head, | |
76 | struct cds_wfcq_tail *tail) | |
77 | { | |
78 | int ret; | |
79 | ||
80 | /* Set queue head and tail */ | |
81 | _cds_wfcq_node_init(&head->node); | |
82 | tail->p = &head->node; | |
83 | ret = pthread_mutex_init(&head->lock, NULL); | |
84 | assert(!ret); | |
85 | } | |
86 | ||
87 | /* | |
88 | * cds_wfcq_empty: return whether wait-free queue is empty. | |
89 | * | |
90 | * No memory barrier is issued. No mutual exclusion is required. | |
91 | */ | |
92 | static inline bool _cds_wfcq_empty(struct cds_wfcq_head *head, | |
93 | struct cds_wfcq_tail *tail) | |
94 | { | |
95 | /* | |
96 | * Queue is empty if no node is pointed by head->node.next nor | |
97 | * tail->p. Even though the tail->p check is sufficient to find | |
98 | * out of the queue is empty, we first check head->node.next as a | |
99 | * common case to ensure that dequeuers do not frequently access | |
100 | * enqueuer's tail->p cache line. | |
101 | */ | |
102 | return CMM_LOAD_SHARED(head->node.next) == NULL | |
103 | && CMM_LOAD_SHARED(tail->p) == &head->node; | |
104 | } | |
105 | ||
106 | static inline void _cds_wfcq_dequeue_lock(struct cds_wfcq_head *head, | |
107 | struct cds_wfcq_tail *tail) | |
108 | { | |
109 | int ret; | |
110 | ||
111 | ret = pthread_mutex_lock(&head->lock); | |
112 | assert(!ret); | |
113 | } | |
114 | ||
115 | static inline void _cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head, | |
116 | struct cds_wfcq_tail *tail) | |
117 | { | |
118 | int ret; | |
119 | ||
120 | ret = pthread_mutex_unlock(&head->lock); | |
121 | assert(!ret); | |
122 | } | |
123 | ||
124 | static inline void ___cds_wfcq_append(struct cds_wfcq_head *head, | |
125 | struct cds_wfcq_tail *tail, | |
126 | struct cds_wfcq_node *new_head, | |
127 | struct cds_wfcq_node *new_tail) | |
128 | { | |
129 | struct cds_wfcq_node *old_tail; | |
130 | ||
131 | /* | |
132 | * Implicit memory barrier before uatomic_xchg() orders earlier | |
133 | * stores to data structure containing node and setting | |
134 | * node->next to NULL before publication. | |
135 | */ | |
136 | old_tail = uatomic_xchg(&tail->p, new_tail); | |
137 | ||
138 | /* | |
139 | * Implicit memory barrier after uatomic_xchg() orders store to | |
140 | * q->tail before store to old_tail->next. | |
141 | * | |
142 | * At this point, dequeuers see a NULL tail->p->next, which | |
143 | * indicates that the queue is being appended to. The following | |
144 | * store will append "node" to the queue from a dequeuer | |
145 | * perspective. | |
146 | */ | |
147 | CMM_STORE_SHARED(old_tail->next, new_head); | |
148 | } | |
149 | ||
150 | /* | |
151 | * cds_wfcq_enqueue: enqueue a node into a wait-free queue. | |
152 | * | |
153 | * Issues a full memory barrier before enqueue. No mutual exclusion is | |
154 | * required. | |
155 | */ | |
156 | static inline void _cds_wfcq_enqueue(struct cds_wfcq_head *head, | |
157 | struct cds_wfcq_tail *tail, | |
158 | struct cds_wfcq_node *new_tail) | |
159 | { | |
160 | ___cds_wfcq_append(head, tail, new_tail, new_tail); | |
161 | } | |
162 | ||
163 | /* | |
164 | * Waiting for enqueuer to complete enqueue and return the next node. | |
165 | */ | |
166 | static inline struct cds_wfcq_node * | |
167 | ___cds_wfcq_node_sync_next(struct cds_wfcq_node *node) | |
168 | { | |
169 | struct cds_wfcq_node *next; | |
170 | int attempt = 0; | |
171 | ||
172 | /* | |
173 | * Adaptative busy-looping waiting for enqueuer to complete enqueue. | |
174 | */ | |
175 | while ((next = CMM_LOAD_SHARED(node->next)) == NULL) { | |
176 | if (++attempt >= WFCQ_ADAPT_ATTEMPTS) { | |
177 | poll(NULL, 0, WFCQ_WAIT); /* Wait for 10ms */ | |
178 | attempt = 0; | |
179 | } else { | |
180 | caa_cpu_relax(); | |
181 | } | |
182 | } | |
183 | ||
184 | return next; | |
185 | } | |
186 | ||
187 | /* | |
188 | * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing. | |
189 | * | |
190 | * Content written into the node before enqueue is guaranteed to be | |
191 | * consistent, but no other memory ordering is ensured. | |
192 | * Should be called with cds_wfcq_dequeue_lock() held. | |
193 | */ | |
194 | static inline struct cds_wfcq_node * | |
195 | ___cds_wfcq_first_blocking(struct cds_wfcq_head *head, | |
196 | struct cds_wfcq_tail *tail) | |
197 | { | |
198 | struct cds_wfcq_node *node; | |
199 | ||
200 | if (_cds_wfcq_empty(head, tail)) | |
201 | return NULL; | |
202 | node = ___cds_wfcq_node_sync_next(&head->node); | |
203 | /* Load head->node.next before loading node's content */ | |
204 | cmm_smp_read_barrier_depends(); | |
205 | return node; | |
206 | } | |
207 | ||
208 | /* | |
209 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. | |
210 | * | |
211 | * Content written into the node before enqueue is guaranteed to be | |
212 | * consistent, but no other memory ordering is ensured. | |
213 | * Should be called with cds_wfcq_dequeue_lock() held. | |
214 | */ | |
215 | static inline struct cds_wfcq_node * | |
216 | ___cds_wfcq_next_blocking(struct cds_wfcq_head *head, | |
217 | struct cds_wfcq_tail *tail, | |
218 | struct cds_wfcq_node *node) | |
219 | { | |
220 | struct cds_wfcq_node *next; | |
221 | ||
222 | /* | |
223 | * Even though the following tail->p check is sufficient to find | |
224 | * out if we reached the end of the queue, we first check | |
225 | * node->next as a common case to ensure that iteration on nodes | |
226 | * do not frequently access enqueuer's tail->p cache line. | |
227 | */ | |
228 | if ((next = CMM_LOAD_SHARED(node->next)) == NULL) { | |
229 | /* Load node->next before tail->p */ | |
230 | cmm_smp_rmb(); | |
231 | if (CMM_LOAD_SHARED(tail->p) == node) | |
232 | return NULL; | |
233 | next = ___cds_wfcq_node_sync_next(node); | |
234 | } | |
235 | /* Load node->next before loading next's content */ | |
236 | cmm_smp_read_barrier_depends(); | |
237 | return next; | |
238 | } | |
239 | ||
240 | /* | |
241 | * __cds_wfcq_dequeue_blocking: dequeue a node from the queue. | |
242 | * | |
243 | * No need to go on a waitqueue here, as there is no possible state in which the | |
244 | * list could cause dequeue to busy-loop needlessly while waiting for another | |
245 | * thread to be scheduled. The queue appears empty until tail->next is set by | |
246 | * enqueue. | |
247 | * | |
248 | * Content written into the node before enqueue is guaranteed to be | |
249 | * consistent, but no other memory ordering is ensured. | |
250 | * It is valid to reuse and free a dequeued node immediately. | |
251 | * Should be called with cds_wfcq_dequeue_lock() held. | |
252 | */ | |
253 | static inline struct cds_wfcq_node * | |
254 | ___cds_wfcq_dequeue_blocking(struct cds_wfcq_head *head, | |
255 | struct cds_wfcq_tail *tail) | |
256 | { | |
257 | struct cds_wfcq_node *node, *next; | |
258 | ||
259 | if (_cds_wfcq_empty(head, tail)) | |
260 | return NULL; | |
261 | ||
262 | node = ___cds_wfcq_node_sync_next(&head->node); | |
263 | ||
264 | if ((next = CMM_LOAD_SHARED(node->next)) == NULL) { | |
265 | /* | |
266 | * @node is probably the only node in the queue. | |
267 | * Try to move the tail to &q->head. | |
268 | * q->head.next is set to NULL here, and stays | |
269 | * NULL if the cmpxchg succeeds. Should the | |
270 | * cmpxchg fail due to a concurrent enqueue, the | |
271 | * q->head.next will be set to the next node. | |
272 | * The implicit memory barrier before | |
273 | * uatomic_cmpxchg() orders load node->next | |
274 | * before loading q->tail. | |
275 | * The implicit memory barrier before uatomic_cmpxchg | |
276 | * orders load q->head.next before loading node's | |
277 | * content. | |
278 | */ | |
279 | _cds_wfcq_node_init(&head->node); | |
280 | if (uatomic_cmpxchg(&tail->p, node, &head->node) == node) | |
281 | return node; | |
282 | next = ___cds_wfcq_node_sync_next(node); | |
283 | } | |
284 | ||
285 | /* | |
286 | * Move queue head forward. | |
287 | */ | |
288 | head->node.next = next; | |
289 | ||
290 | /* Load q->head.next before loading node's content */ | |
291 | cmm_smp_read_barrier_depends(); | |
292 | return node; | |
293 | } | |
294 | ||
295 | /* | |
296 | * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. | |
297 | * | |
298 | * Dequeue all nodes from src_q. | |
299 | * dest_q must be already initialized. | |
300 | * Should be called with cds_wfcq_dequeue_lock() held on src_q. | |
301 | */ | |
302 | static inline void | |
303 | ___cds_wfcq_splice_blocking( | |
304 | struct cds_wfcq_head *dest_q_head, | |
305 | struct cds_wfcq_tail *dest_q_tail, | |
306 | struct cds_wfcq_head *src_q_head, | |
307 | struct cds_wfcq_tail *src_q_tail) | |
308 | { | |
309 | struct cds_wfcq_node *head, *tail; | |
310 | ||
311 | if (_cds_wfcq_empty(src_q_head, src_q_tail)) | |
312 | return; | |
313 | ||
314 | head = ___cds_wfcq_node_sync_next(&src_q_head->node); | |
315 | _cds_wfcq_node_init(&src_q_head->node); | |
316 | ||
317 | /* | |
318 | * Memory barrier implied before uatomic_xchg() orders store to | |
319 | * src_q->head before store to src_q->tail. This is required by | |
320 | * concurrent enqueue on src_q, which exchanges the tail before | |
321 | * updating the previous tail's next pointer. | |
322 | */ | |
323 | tail = uatomic_xchg(&src_q_tail->p, &src_q_head->node); | |
324 | ||
325 | /* | |
326 | * Append the spliced content of src_q into dest_q. Does not | |
327 | * require mutual exclusion on dest_q (wait-free). | |
328 | */ | |
329 | ___cds_wfcq_append(dest_q_head, dest_q_tail, head, tail); | |
330 | } | |
331 | ||
332 | /* | |
333 | * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. | |
334 | * | |
335 | * Content written into the node before enqueue is guaranteed to be | |
336 | * consistent, but no other memory ordering is ensured. | |
337 | * Mutual exlusion with (and only with) cds_wfcq_splice_blocking is | |
338 | * ensured. | |
339 | * It is valid to reuse and free a dequeued node immediately. | |
340 | */ | |
341 | static inline struct cds_wfcq_node * | |
342 | _cds_wfcq_dequeue_blocking(struct cds_wfcq_head *head, | |
343 | struct cds_wfcq_tail *tail) | |
344 | { | |
345 | struct cds_wfcq_node *retval; | |
346 | ||
347 | _cds_wfcq_dequeue_lock(head, tail); | |
348 | retval = ___cds_wfcq_dequeue_blocking(head, tail); | |
349 | _cds_wfcq_dequeue_unlock(head, tail); | |
350 | return retval; | |
351 | } | |
352 | ||
353 | /* | |
354 | * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. | |
355 | * | |
356 | * Dequeue all nodes from src_q. | |
357 | * dest_q must be already initialized. | |
358 | * Content written into the node before enqueue is guaranteed to be | |
359 | * consistent, but no other memory ordering is ensured. | |
360 | * Mutual exlusion with (and only with) cds_wfcq_dequeue_blocking is | |
361 | * ensured. | |
362 | */ | |
363 | static inline void | |
364 | _cds_wfcq_splice_blocking( | |
365 | struct cds_wfcq_head *dest_q_head, | |
366 | struct cds_wfcq_tail *dest_q_tail, | |
367 | struct cds_wfcq_head *src_q_head, | |
368 | struct cds_wfcq_tail *src_q_tail) | |
369 | { | |
370 | _cds_wfcq_dequeue_lock(src_q_head, src_q_tail); | |
371 | ___cds_wfcq_splice_blocking(dest_q_head, dest_q_tail, | |
372 | src_q_head, src_q_tail); | |
373 | _cds_wfcq_dequeue_unlock(src_q_head, src_q_tail); | |
374 | } | |
375 | ||
376 | #ifdef __cplusplus | |
377 | } | |
378 | #endif | |
379 | ||
380 | #endif /* _URCU_WFCQUEUE_STATIC_H */ |