| 1 | #ifndef _URCU_WFCQUEUE_H |
| 2 | #define _URCU_WFCQUEUE_H |
| 3 | |
| 4 | /* |
| 5 | * urcu/wfcqueue.h |
| 6 | * |
| 7 | * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue |
| 8 | * |
| 9 | * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * Copyright 2011-2012 - Lai Jiangshan <laijs@cn.fujitsu.com> |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | */ |
| 26 | |
| 27 | #include <pthread.h> |
| 28 | #include <assert.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <urcu/compiler.h> |
| 31 | #include <urcu/arch.h> |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /* |
| 38 | * Concurrent queue with wait-free enqueue/blocking dequeue. |
| 39 | * |
| 40 | * This queue has been designed and implemented collaboratively by |
| 41 | * Mathieu Desnoyers and Lai Jiangshan. Inspired from |
| 42 | * half-wait-free/half-blocking queue implementation done by Paul E. |
| 43 | * McKenney. |
| 44 | */ |
| 45 | |
| 46 | #define CDS_WFCQ_WOULDBLOCK ((void *) -1UL) |
| 47 | |
| 48 | enum cds_wfcq_ret { |
| 49 | CDS_WFCQ_RET_WOULDBLOCK = -1, |
| 50 | CDS_WFCQ_RET_DEST_EMPTY = 0, |
| 51 | CDS_WFCQ_RET_DEST_NON_EMPTY = 1, |
| 52 | CDS_WFCQ_RET_SRC_EMPTY = 2, |
| 53 | }; |
| 54 | |
| 55 | enum cds_wfcq_state { |
| 56 | CDS_WFCQ_STATE_LAST = (1U << 0), |
| 57 | }; |
| 58 | |
| 59 | struct cds_wfcq_node { |
| 60 | struct cds_wfcq_node *next; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Do not put head and tail on the same cache-line if concurrent |
| 65 | * enqueue/dequeue are expected from many CPUs. This eliminates |
| 66 | * false-sharing between enqueue and dequeue. |
| 67 | */ |
| 68 | struct __cds_wfcq_head { |
| 69 | struct cds_wfcq_node node; |
| 70 | }; |
| 71 | |
| 72 | struct cds_wfcq_head { |
| 73 | struct cds_wfcq_node node; |
| 74 | pthread_mutex_t lock; |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * The transparent union allows calling functions that work on both |
| 79 | * struct cds_wfcq_head and struct __cds_wfcq_head on any of those two |
| 80 | * types. |
| 81 | */ |
| 82 | typedef union __attribute__((__transparent_union__)) { |
| 83 | struct __cds_wfcq_head *_h; |
| 84 | struct cds_wfcq_head *h; |
| 85 | } cds_wfcq_head_ptr_t; |
| 86 | |
| 87 | struct cds_wfcq_tail { |
| 88 | struct cds_wfcq_node *p; |
| 89 | }; |
| 90 | |
| 91 | #ifdef _LGPL_SOURCE |
| 92 | |
| 93 | #include <urcu/static/wfcqueue.h> |
| 94 | |
| 95 | #define cds_wfcq_node_init _cds_wfcq_node_init |
| 96 | #define cds_wfcq_init _cds_wfcq_init |
| 97 | #define cds_wfcq_empty _cds_wfcq_empty |
| 98 | #define cds_wfcq_enqueue _cds_wfcq_enqueue |
| 99 | |
| 100 | /* Dequeue locking */ |
| 101 | #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock |
| 102 | #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock |
| 103 | |
| 104 | /* Locking performed within cds_wfcq calls. */ |
| 105 | #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking |
| 106 | #define cds_wfcq_dequeue_with_state_blocking \ |
| 107 | _cds_wfcq_dequeue_with_state_blocking |
| 108 | #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking |
| 109 | #define cds_wfcq_first_blocking _cds_wfcq_first_blocking |
| 110 | #define cds_wfcq_next_blocking _cds_wfcq_next_blocking |
| 111 | |
| 112 | /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */ |
| 113 | #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking |
| 114 | #define __cds_wfcq_dequeue_with_state_blocking \ |
| 115 | ___cds_wfcq_dequeue_with_state_blocking |
| 116 | #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking |
| 117 | #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking |
| 118 | #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking |
| 119 | |
| 120 | /* |
| 121 | * Locking ensured by caller by holding cds_wfcq_dequeue_lock(). |
| 122 | * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they |
| 123 | * need to block. splice returns nonzero if it needs to block. |
| 124 | */ |
| 125 | #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking |
| 126 | #define __cds_wfcq_dequeue_with_state_nonblocking \ |
| 127 | ___cds_wfcq_dequeue_with_state_nonblocking |
| 128 | #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking |
| 129 | #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking |
| 130 | #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking |
| 131 | |
| 132 | #else /* !_LGPL_SOURCE */ |
| 133 | |
| 134 | /* |
| 135 | * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API |
| 136 | * |
| 137 | * Synchronization table: |
| 138 | * |
| 139 | * External synchronization techniques described in the API below is |
| 140 | * required between pairs marked with "X". No external synchronization |
| 141 | * required between pairs marked with "-". |
| 142 | * |
| 143 | * Legend: |
| 144 | * [1] cds_wfcq_enqueue |
| 145 | * [2] __cds_wfcq_splice (destination queue) |
| 146 | * [3] __cds_wfcq_dequeue |
| 147 | * [4] __cds_wfcq_splice (source queue) |
| 148 | * [5] __cds_wfcq_first |
| 149 | * [6] __cds_wfcq_next |
| 150 | * |
| 151 | * [1] [2] [3] [4] [5] [6] |
| 152 | * [1] - - - - - - |
| 153 | * [2] - - - - - - |
| 154 | * [3] - - X X X X |
| 155 | * [4] - - X - X X |
| 156 | * [5] - - X X - - |
| 157 | * [6] - - X X - - |
| 158 | * |
| 159 | * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock(). |
| 160 | * |
| 161 | * For convenience, cds_wfcq_dequeue_blocking() and |
| 162 | * cds_wfcq_splice_blocking() hold the dequeue lock. |
| 163 | * |
| 164 | * Besides locking, mutual exclusion of dequeue, splice and iteration |
| 165 | * can be ensured by performing all of those operations from a single |
| 166 | * thread, without requiring any lock. |
| 167 | */ |
| 168 | |
| 169 | /* |
| 170 | * cds_wfcq_node_init: initialize wait-free queue node. |
| 171 | */ |
| 172 | extern void cds_wfcq_node_init(struct cds_wfcq_node *node); |
| 173 | |
| 174 | /* |
| 175 | * cds_wfcq_init: initialize wait-free queue. |
| 176 | */ |
| 177 | extern void cds_wfcq_init(struct cds_wfcq_head *head, |
| 178 | struct cds_wfcq_tail *tail); |
| 179 | |
| 180 | /* |
| 181 | * __cds_wfcq_init: initialize wait-free queue. |
| 182 | */ |
| 183 | extern void __cds_wfcq_init(struct __cds_wfcq_head *head, |
| 184 | struct cds_wfcq_tail *tail); |
| 185 | |
| 186 | /* |
| 187 | * cds_wfcq_empty: return whether wait-free queue is empty. |
| 188 | * |
| 189 | * No memory barrier is issued. No mutual exclusion is required. |
| 190 | */ |
| 191 | extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head, |
| 192 | struct cds_wfcq_tail *tail); |
| 193 | |
| 194 | /* |
| 195 | * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock. |
| 196 | */ |
| 197 | extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head, |
| 198 | struct cds_wfcq_tail *tail); |
| 199 | |
| 200 | /* |
| 201 | * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock. |
| 202 | */ |
| 203 | extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head, |
| 204 | struct cds_wfcq_tail *tail); |
| 205 | |
| 206 | /* |
| 207 | * cds_wfcq_enqueue: enqueue a node into a wait-free queue. |
| 208 | * |
| 209 | * Issues a full memory barrier before enqueue. No mutual exclusion is |
| 210 | * required. |
| 211 | * |
| 212 | * Returns false if the queue was empty prior to adding the node. |
| 213 | * Returns true otherwise. |
| 214 | */ |
| 215 | extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head, |
| 216 | struct cds_wfcq_tail *tail, |
| 217 | struct cds_wfcq_node *node); |
| 218 | |
| 219 | /* |
| 220 | * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
| 221 | * |
| 222 | * Content written into the node before enqueue is guaranteed to be |
| 223 | * consistent, but no other memory ordering is ensured. |
| 224 | * It is valid to reuse and free a dequeued node immediately. |
| 225 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
| 226 | * ensured. |
| 227 | */ |
| 228 | extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking( |
| 229 | struct cds_wfcq_head *head, |
| 230 | struct cds_wfcq_tail *tail); |
| 231 | |
| 232 | /* |
| 233 | * cds_wfcq_dequeue_with_state_blocking: dequeue with state. |
| 234 | * |
| 235 | * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the |
| 236 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). |
| 237 | */ |
| 238 | extern struct cds_wfcq_node *cds_wfcq_dequeue_with_state_blocking( |
| 239 | struct cds_wfcq_head *head, |
| 240 | struct cds_wfcq_tail *tail, |
| 241 | int *state); |
| 242 | |
| 243 | /* |
| 244 | * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. |
| 245 | * |
| 246 | * Dequeue all nodes from src_q. |
| 247 | * dest_q must be already initialized. |
| 248 | * Content written into the node before enqueue is guaranteed to be |
| 249 | * consistent, but no other memory ordering is ensured. |
| 250 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
| 251 | * ensured. |
| 252 | * |
| 253 | * Returns enum cds_wfcq_ret which indicates the state of the src or |
| 254 | * dest queue. |
| 255 | */ |
| 256 | extern enum cds_wfcq_ret cds_wfcq_splice_blocking( |
| 257 | struct cds_wfcq_head *dest_q_head, |
| 258 | struct cds_wfcq_tail *dest_q_tail, |
| 259 | struct cds_wfcq_head *src_q_head, |
| 260 | struct cds_wfcq_tail *src_q_tail); |
| 261 | |
| 262 | /* |
| 263 | * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
| 264 | * |
| 265 | * Content written into the node before enqueue is guaranteed to be |
| 266 | * consistent, but no other memory ordering is ensured. |
| 267 | * It is valid to reuse and free a dequeued node immediately. |
| 268 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 269 | * caller. |
| 270 | */ |
| 271 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( |
| 272 | cds_wfcq_head_ptr_t head, |
| 273 | struct cds_wfcq_tail *tail); |
| 274 | |
| 275 | /* |
| 276 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. |
| 277 | * |
| 278 | * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the |
| 279 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). |
| 280 | */ |
| 281 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( |
| 282 | cds_wfcq_head_ptr_t head, |
| 283 | struct cds_wfcq_tail *tail, |
| 284 | int *state); |
| 285 | |
| 286 | /* |
| 287 | * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue. |
| 288 | * |
| 289 | * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK |
| 290 | * if it needs to block. |
| 291 | */ |
| 292 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( |
| 293 | cds_wfcq_head_ptr_t head, |
| 294 | struct cds_wfcq_tail *tail); |
| 295 | |
| 296 | /* |
| 297 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. |
| 298 | * |
| 299 | * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing |
| 300 | * the last node of the queue into state (CDS_WFCQ_STATE_LAST). |
| 301 | */ |
| 302 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( |
| 303 | cds_wfcq_head_ptr_t head, |
| 304 | struct cds_wfcq_tail *tail, |
| 305 | int *state); |
| 306 | |
| 307 | /* |
| 308 | * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. |
| 309 | * |
| 310 | * Dequeue all nodes from src_q. |
| 311 | * dest_q must be already initialized. |
| 312 | * Mutual exclusion for src_q should be ensured by the caller as |
| 313 | * specified in the "Synchronisation table". |
| 314 | * Returns enum cds_wfcq_ret which indicates the state of the src or |
| 315 | * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK. |
| 316 | */ |
| 317 | extern enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
| 318 | cds_wfcq_head_ptr_t dest_q_head, |
| 319 | struct cds_wfcq_tail *dest_q_tail, |
| 320 | cds_wfcq_head_ptr_t src_q_head, |
| 321 | struct cds_wfcq_tail *src_q_tail); |
| 322 | |
| 323 | /* |
| 324 | * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q. |
| 325 | * |
| 326 | * Same as __cds_wfcq_splice_blocking, but returns |
| 327 | * CDS_WFCQ_RET_WOULDBLOCK if it needs to block. |
| 328 | */ |
| 329 | extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
| 330 | cds_wfcq_head_ptr_t dest_q_head, |
| 331 | struct cds_wfcq_tail *dest_q_tail, |
| 332 | cds_wfcq_head_ptr_t src_q_head, |
| 333 | struct cds_wfcq_tail *src_q_tail); |
| 334 | |
| 335 | /* |
| 336 | * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing. |
| 337 | * |
| 338 | * Content written into the node before enqueue is guaranteed to be |
| 339 | * consistent, but no other memory ordering is ensured. |
| 340 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 341 | * caller. |
| 342 | * |
| 343 | * Used by for-like iteration macros: |
| 344 | * __cds_wfcq_for_each_blocking() |
| 345 | * __cds_wfcq_for_each_blocking_safe() |
| 346 | * |
| 347 | * Returns NULL if queue is empty, first node otherwise. |
| 348 | */ |
| 349 | extern struct cds_wfcq_node *__cds_wfcq_first_blocking( |
| 350 | cds_wfcq_head_ptr_t head, |
| 351 | struct cds_wfcq_tail *tail); |
| 352 | |
| 353 | /* |
| 354 | * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing. |
| 355 | * |
| 356 | * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if |
| 357 | * it needs to block. |
| 358 | */ |
| 359 | extern struct cds_wfcq_node *__cds_wfcq_first_nonblocking( |
| 360 | cds_wfcq_head_ptr_t head, |
| 361 | struct cds_wfcq_tail *tail); |
| 362 | |
| 363 | /* |
| 364 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. |
| 365 | * |
| 366 | * Content written into the node before enqueue is guaranteed to be |
| 367 | * consistent, but no other memory ordering is ensured. |
| 368 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 369 | * caller. |
| 370 | * |
| 371 | * Used by for-like iteration macros: |
| 372 | * __cds_wfcq_for_each_blocking() |
| 373 | * __cds_wfcq_for_each_blocking_safe() |
| 374 | * |
| 375 | * Returns NULL if reached end of queue, non-NULL next queue node |
| 376 | * otherwise. |
| 377 | */ |
| 378 | extern struct cds_wfcq_node *__cds_wfcq_next_blocking( |
| 379 | cds_wfcq_head_ptr_t head, |
| 380 | struct cds_wfcq_tail *tail, |
| 381 | struct cds_wfcq_node *node); |
| 382 | |
| 383 | /* |
| 384 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. |
| 385 | * |
| 386 | * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if |
| 387 | * it needs to block. |
| 388 | */ |
| 389 | extern struct cds_wfcq_node *__cds_wfcq_next_nonblocking( |
| 390 | cds_wfcq_head_ptr_t head, |
| 391 | struct cds_wfcq_tail *tail, |
| 392 | struct cds_wfcq_node *node); |
| 393 | |
| 394 | #endif /* !_LGPL_SOURCE */ |
| 395 | |
| 396 | /* |
| 397 | * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue, |
| 398 | * without dequeuing them. |
| 399 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
| 400 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
| 401 | * @node: iterator on the queue (struct cds_wfcq_node pointer). |
| 402 | * |
| 403 | * Content written into each node before enqueue is guaranteed to be |
| 404 | * consistent, but no other memory ordering is ensured. |
| 405 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 406 | * caller. |
| 407 | */ |
| 408 | #define __cds_wfcq_for_each_blocking(head, tail, node) \ |
| 409 | for (node = __cds_wfcq_first_blocking(head, tail); \ |
| 410 | node != NULL; \ |
| 411 | node = __cds_wfcq_next_blocking(head, tail, node)) |
| 412 | |
| 413 | /* |
| 414 | * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue, |
| 415 | * without dequeuing them. Safe against deletion. |
| 416 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
| 417 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
| 418 | * @node: iterator on the queue (struct cds_wfcq_node pointer). |
| 419 | * @n: struct cds_wfcq_node pointer holding the next pointer (used |
| 420 | * internally). |
| 421 | * |
| 422 | * Content written into each node before enqueue is guaranteed to be |
| 423 | * consistent, but no other memory ordering is ensured. |
| 424 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 425 | * caller. |
| 426 | */ |
| 427 | #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \ |
| 428 | for (node = __cds_wfcq_first_blocking(head, tail), \ |
| 429 | n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \ |
| 430 | node != NULL; \ |
| 431 | node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL)) |
| 432 | |
| 433 | #ifdef __cplusplus |
| 434 | } |
| 435 | #endif |
| 436 | |
| 437 | #endif /* _URCU_WFCQUEUE_H */ |