| 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 <stdbool.h> |
| 29 | #include <urcu/compiler.h> |
| 30 | #include <urcu/arch.h> |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /* |
| 37 | * Concurrent queue with wait-free enqueue/blocking dequeue. |
| 38 | * |
| 39 | * This queue has been designed and implemented collaboratively by |
| 40 | * Mathieu Desnoyers and Lai Jiangshan. Inspired from |
| 41 | * half-wait-free/half-blocking queue implementation done by Paul E. |
| 42 | * McKenney. |
| 43 | */ |
| 44 | |
| 45 | #define CDS_WFCQ_WOULDBLOCK ((struct cds_wfcq_node *) -1UL) |
| 46 | |
| 47 | enum cds_wfcq_ret { |
| 48 | CDS_WFCQ_RET_WOULDBLOCK = -1, |
| 49 | CDS_WFCQ_RET_DEST_EMPTY = 0, |
| 50 | CDS_WFCQ_RET_DEST_NON_EMPTY = 1, |
| 51 | CDS_WFCQ_RET_SRC_EMPTY = 2, |
| 52 | }; |
| 53 | |
| 54 | enum cds_wfcq_state { |
| 55 | CDS_WFCQ_STATE_LAST = (1U << 0), |
| 56 | }; |
| 57 | |
| 58 | struct cds_wfcq_node { |
| 59 | struct cds_wfcq_node *next; |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * Do not put head and tail on the same cache-line if concurrent |
| 64 | * enqueue/dequeue are expected from many CPUs. This eliminates |
| 65 | * false-sharing between enqueue and dequeue. |
| 66 | */ |
| 67 | struct __cds_wfcq_head { |
| 68 | struct cds_wfcq_node node; |
| 69 | }; |
| 70 | |
| 71 | struct cds_wfcq_head { |
| 72 | struct cds_wfcq_node node; |
| 73 | pthread_mutex_t lock; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * In C, the transparent union allows calling functions that work on both |
| 78 | * struct cds_wfcq_head and struct __cds_wfcq_head on any of those two |
| 79 | * types. |
| 80 | * |
| 81 | * In C++, implement static inline wrappers using function overloading |
| 82 | * to obtain an API similar to C. |
| 83 | * |
| 84 | * Avoid complaints from clang++ not knowing the transparent union |
| 85 | * attribute. |
| 86 | */ |
| 87 | #if defined(__clang__) |
| 88 | #pragma clang diagnostic push |
| 89 | #pragma clang diagnostic ignored "-Wignored-attributes" |
| 90 | #endif |
| 91 | typedef union { |
| 92 | struct __cds_wfcq_head *_h; |
| 93 | struct cds_wfcq_head *h; |
| 94 | } __attribute__((__transparent_union__)) cds_wfcq_head_ptr_t; |
| 95 | #if defined(__clang__) |
| 96 | #pragma clang diagnostic pop |
| 97 | #endif |
| 98 | |
| 99 | #ifndef __cplusplus |
| 100 | /* |
| 101 | * This static inline is only present for compatibility with C++. It is |
| 102 | * effect-less in C. |
| 103 | */ |
| 104 | static inline struct __cds_wfcq_head *__cds_wfcq_head_cast(struct __cds_wfcq_head *head) |
| 105 | { |
| 106 | return head; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * This static inline is only present for compatibility with C++. It is |
| 111 | * effect-less in C. |
| 112 | */ |
| 113 | static inline struct cds_wfcq_head *cds_wfcq_head_cast(struct cds_wfcq_head *head) |
| 114 | { |
| 115 | return head; |
| 116 | } |
| 117 | #else /* #ifndef __cplusplus */ |
| 118 | |
| 119 | /* |
| 120 | * This static inline is used by internally in the static inline |
| 121 | * implementation of the API. |
| 122 | */ |
| 123 | static inline cds_wfcq_head_ptr_t __cds_wfcq_head_cast(struct __cds_wfcq_head *head) |
| 124 | { |
| 125 | cds_wfcq_head_ptr_t ret = { ._h = head }; |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * This static inline is used by internally in the static inline |
| 131 | * implementation of the API. |
| 132 | */ |
| 133 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast(struct cds_wfcq_head *head) |
| 134 | { |
| 135 | cds_wfcq_head_ptr_t ret = { .h = head }; |
| 136 | return ret; |
| 137 | } |
| 138 | #endif /* #else #ifndef __cplusplus */ |
| 139 | |
| 140 | struct cds_wfcq_tail { |
| 141 | struct cds_wfcq_node *p; |
| 142 | }; |
| 143 | |
| 144 | #ifdef _LGPL_SOURCE |
| 145 | |
| 146 | #include <urcu/static/wfcqueue.h> |
| 147 | |
| 148 | #define cds_wfcq_node_init _cds_wfcq_node_init |
| 149 | #define cds_wfcq_init _cds_wfcq_init |
| 150 | #define __cds_wfcq_init ___cds_wfcq_init |
| 151 | #define cds_wfcq_destroy _cds_wfcq_destroy |
| 152 | #define cds_wfcq_empty _cds_wfcq_empty |
| 153 | #define cds_wfcq_enqueue _cds_wfcq_enqueue |
| 154 | |
| 155 | /* Dequeue locking */ |
| 156 | #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock |
| 157 | #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock |
| 158 | |
| 159 | /* Locking performed within cds_wfcq calls. */ |
| 160 | #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking |
| 161 | #define cds_wfcq_dequeue_with_state_blocking \ |
| 162 | _cds_wfcq_dequeue_with_state_blocking |
| 163 | #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking |
| 164 | #define cds_wfcq_first_blocking _cds_wfcq_first_blocking |
| 165 | #define cds_wfcq_next_blocking _cds_wfcq_next_blocking |
| 166 | |
| 167 | /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */ |
| 168 | #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking |
| 169 | #define __cds_wfcq_dequeue_with_state_blocking \ |
| 170 | ___cds_wfcq_dequeue_with_state_blocking |
| 171 | #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking |
| 172 | #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking |
| 173 | #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking |
| 174 | |
| 175 | /* |
| 176 | * Locking ensured by caller by holding cds_wfcq_dequeue_lock(). |
| 177 | * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they |
| 178 | * need to block. splice returns nonzero if it needs to block. |
| 179 | */ |
| 180 | #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking |
| 181 | #define __cds_wfcq_dequeue_with_state_nonblocking \ |
| 182 | ___cds_wfcq_dequeue_with_state_nonblocking |
| 183 | #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking |
| 184 | #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking |
| 185 | #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking |
| 186 | |
| 187 | #else /* !_LGPL_SOURCE */ |
| 188 | |
| 189 | /* |
| 190 | * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API |
| 191 | * |
| 192 | * Synchronization table: |
| 193 | * |
| 194 | * External synchronization techniques described in the API below is |
| 195 | * required between pairs marked with "X". No external synchronization |
| 196 | * required between pairs marked with "-". |
| 197 | * |
| 198 | * Legend: |
| 199 | * [1] cds_wfcq_enqueue |
| 200 | * [2] __cds_wfcq_splice (destination queue) |
| 201 | * [3] __cds_wfcq_dequeue |
| 202 | * [4] __cds_wfcq_splice (source queue) |
| 203 | * [5] __cds_wfcq_first |
| 204 | * [6] __cds_wfcq_next |
| 205 | * |
| 206 | * [1] [2] [3] [4] [5] [6] |
| 207 | * [1] - - - - - - |
| 208 | * [2] - - - - - - |
| 209 | * [3] - - X X X X |
| 210 | * [4] - - X - X X |
| 211 | * [5] - - X X - - |
| 212 | * [6] - - X X - - |
| 213 | * |
| 214 | * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock(). |
| 215 | * |
| 216 | * For convenience, cds_wfcq_dequeue_blocking() and |
| 217 | * cds_wfcq_splice_blocking() hold the dequeue lock. |
| 218 | * |
| 219 | * Besides locking, mutual exclusion of dequeue, splice and iteration |
| 220 | * can be ensured by performing all of those operations from a single |
| 221 | * thread, without requiring any lock. |
| 222 | */ |
| 223 | |
| 224 | /* |
| 225 | * cds_wfcq_node_init: initialize wait-free queue node. |
| 226 | */ |
| 227 | extern void cds_wfcq_node_init(struct cds_wfcq_node *node); |
| 228 | |
| 229 | /* |
| 230 | * cds_wfcq_init: initialize wait-free queue. Pair with |
| 231 | * cds_wfcq_destroy(). |
| 232 | */ |
| 233 | extern void cds_wfcq_init(struct cds_wfcq_head *head, |
| 234 | struct cds_wfcq_tail *tail); |
| 235 | |
| 236 | /* |
| 237 | * cds_wfcq_destroy: destroy wait-free queue. Pair with |
| 238 | * cds_wfcq_init(). |
| 239 | */ |
| 240 | extern void cds_wfcq_destroy(struct cds_wfcq_head *head, |
| 241 | struct cds_wfcq_tail *tail); |
| 242 | |
| 243 | /* |
| 244 | * __cds_wfcq_init: initialize wait-free queue (without lock). Don't |
| 245 | * pair with any destroy function. |
| 246 | */ |
| 247 | extern void __cds_wfcq_init(struct __cds_wfcq_head *head, |
| 248 | struct cds_wfcq_tail *tail); |
| 249 | |
| 250 | /* |
| 251 | * cds_wfcq_empty: return whether wait-free queue is empty. |
| 252 | * |
| 253 | * No memory barrier is issued. No mutual exclusion is required. |
| 254 | */ |
| 255 | extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head, |
| 256 | struct cds_wfcq_tail *tail); |
| 257 | |
| 258 | /* |
| 259 | * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock. |
| 260 | */ |
| 261 | extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head, |
| 262 | struct cds_wfcq_tail *tail); |
| 263 | |
| 264 | /* |
| 265 | * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock. |
| 266 | */ |
| 267 | extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head, |
| 268 | struct cds_wfcq_tail *tail); |
| 269 | |
| 270 | /* |
| 271 | * cds_wfcq_enqueue: enqueue a node into a wait-free queue. |
| 272 | * |
| 273 | * Issues a full memory barrier before enqueue. No mutual exclusion is |
| 274 | * required. |
| 275 | * |
| 276 | * Returns false if the queue was empty prior to adding the node. |
| 277 | * Returns true otherwise. |
| 278 | */ |
| 279 | extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head, |
| 280 | struct cds_wfcq_tail *tail, |
| 281 | struct cds_wfcq_node *node); |
| 282 | |
| 283 | /* |
| 284 | * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
| 285 | * |
| 286 | * Content written into the node before enqueue is guaranteed to be |
| 287 | * consistent, but no other memory ordering is ensured. |
| 288 | * It is valid to reuse and free a dequeued node immediately. |
| 289 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
| 290 | * ensured. |
| 291 | */ |
| 292 | extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking( |
| 293 | struct cds_wfcq_head *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_blocking, but saves whether dequeueing the |
| 300 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). |
| 301 | */ |
| 302 | extern struct cds_wfcq_node *cds_wfcq_dequeue_with_state_blocking( |
| 303 | struct cds_wfcq_head *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 | * Content written into the node before enqueue is guaranteed to be |
| 313 | * consistent, but no other memory ordering is ensured. |
| 314 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
| 315 | * ensured. |
| 316 | * |
| 317 | * Returns enum cds_wfcq_ret which indicates the state of the src or |
| 318 | * dest queue. |
| 319 | */ |
| 320 | extern enum cds_wfcq_ret cds_wfcq_splice_blocking( |
| 321 | struct cds_wfcq_head *dest_q_head, |
| 322 | struct cds_wfcq_tail *dest_q_tail, |
| 323 | struct cds_wfcq_head *src_q_head, |
| 324 | struct cds_wfcq_tail *src_q_tail); |
| 325 | |
| 326 | /* |
| 327 | * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
| 328 | * |
| 329 | * Content written into the node before enqueue is guaranteed to be |
| 330 | * consistent, but no other memory ordering is ensured. |
| 331 | * It is valid to reuse and free a dequeued node immediately. |
| 332 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 333 | * caller. |
| 334 | */ |
| 335 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( |
| 336 | cds_wfcq_head_ptr_t head, |
| 337 | struct cds_wfcq_tail *tail); |
| 338 | |
| 339 | /* |
| 340 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. |
| 341 | * |
| 342 | * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the |
| 343 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). |
| 344 | */ |
| 345 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( |
| 346 | cds_wfcq_head_ptr_t head, |
| 347 | struct cds_wfcq_tail *tail, |
| 348 | int *state); |
| 349 | |
| 350 | /* |
| 351 | * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue. |
| 352 | * |
| 353 | * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK |
| 354 | * if it needs to block. |
| 355 | */ |
| 356 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( |
| 357 | cds_wfcq_head_ptr_t head, |
| 358 | struct cds_wfcq_tail *tail); |
| 359 | |
| 360 | /* |
| 361 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. |
| 362 | * |
| 363 | * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing |
| 364 | * the last node of the queue into state (CDS_WFCQ_STATE_LAST). |
| 365 | */ |
| 366 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( |
| 367 | cds_wfcq_head_ptr_t head, |
| 368 | struct cds_wfcq_tail *tail, |
| 369 | int *state); |
| 370 | |
| 371 | /* |
| 372 | * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. |
| 373 | * |
| 374 | * Dequeue all nodes from src_q. |
| 375 | * dest_q must be already initialized. |
| 376 | * Mutual exclusion for src_q should be ensured by the caller as |
| 377 | * specified in the "Synchronisation table". |
| 378 | * Returns enum cds_wfcq_ret which indicates the state of the src or |
| 379 | * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK. |
| 380 | */ |
| 381 | extern enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
| 382 | cds_wfcq_head_ptr_t dest_q_head, |
| 383 | struct cds_wfcq_tail *dest_q_tail, |
| 384 | cds_wfcq_head_ptr_t src_q_head, |
| 385 | struct cds_wfcq_tail *src_q_tail); |
| 386 | |
| 387 | /* |
| 388 | * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q. |
| 389 | * |
| 390 | * Same as __cds_wfcq_splice_blocking, but returns |
| 391 | * CDS_WFCQ_RET_WOULDBLOCK if it needs to block. |
| 392 | */ |
| 393 | extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
| 394 | cds_wfcq_head_ptr_t dest_q_head, |
| 395 | struct cds_wfcq_tail *dest_q_tail, |
| 396 | cds_wfcq_head_ptr_t src_q_head, |
| 397 | struct cds_wfcq_tail *src_q_tail); |
| 398 | |
| 399 | /* |
| 400 | * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing. |
| 401 | * |
| 402 | * Content written into the node before enqueue is guaranteed to be |
| 403 | * consistent, but no other memory ordering is ensured. |
| 404 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 405 | * caller. |
| 406 | * |
| 407 | * Used by for-like iteration macros: |
| 408 | * __cds_wfcq_for_each_blocking() |
| 409 | * __cds_wfcq_for_each_blocking_safe() |
| 410 | * |
| 411 | * Returns NULL if queue is empty, first node otherwise. |
| 412 | */ |
| 413 | extern struct cds_wfcq_node *__cds_wfcq_first_blocking( |
| 414 | cds_wfcq_head_ptr_t head, |
| 415 | struct cds_wfcq_tail *tail); |
| 416 | |
| 417 | /* |
| 418 | * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing. |
| 419 | * |
| 420 | * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if |
| 421 | * it needs to block. |
| 422 | */ |
| 423 | extern struct cds_wfcq_node *__cds_wfcq_first_nonblocking( |
| 424 | cds_wfcq_head_ptr_t head, |
| 425 | struct cds_wfcq_tail *tail); |
| 426 | |
| 427 | /* |
| 428 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. |
| 429 | * |
| 430 | * Content written into the node before enqueue is guaranteed to be |
| 431 | * consistent, but no other memory ordering is ensured. |
| 432 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 433 | * caller. |
| 434 | * |
| 435 | * Used by for-like iteration macros: |
| 436 | * __cds_wfcq_for_each_blocking() |
| 437 | * __cds_wfcq_for_each_blocking_safe() |
| 438 | * |
| 439 | * Returns NULL if reached end of queue, non-NULL next queue node |
| 440 | * otherwise. |
| 441 | */ |
| 442 | extern struct cds_wfcq_node *__cds_wfcq_next_blocking( |
| 443 | cds_wfcq_head_ptr_t head, |
| 444 | struct cds_wfcq_tail *tail, |
| 445 | struct cds_wfcq_node *node); |
| 446 | |
| 447 | /* |
| 448 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. |
| 449 | * |
| 450 | * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if |
| 451 | * it needs to block. |
| 452 | */ |
| 453 | extern struct cds_wfcq_node *__cds_wfcq_next_nonblocking( |
| 454 | cds_wfcq_head_ptr_t head, |
| 455 | struct cds_wfcq_tail *tail, |
| 456 | struct cds_wfcq_node *node); |
| 457 | |
| 458 | #endif /* !_LGPL_SOURCE */ |
| 459 | |
| 460 | /* |
| 461 | * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue, |
| 462 | * without dequeuing them. |
| 463 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
| 464 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
| 465 | * @node: iterator on the queue (struct cds_wfcq_node pointer). |
| 466 | * |
| 467 | * Content written into each node before enqueue is guaranteed to be |
| 468 | * consistent, but no other memory ordering is ensured. |
| 469 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 470 | * caller. |
| 471 | */ |
| 472 | #define __cds_wfcq_for_each_blocking(head, tail, node) \ |
| 473 | for (node = __cds_wfcq_first_blocking(head, tail); \ |
| 474 | node != NULL; \ |
| 475 | node = __cds_wfcq_next_blocking(head, tail, node)) |
| 476 | |
| 477 | /* |
| 478 | * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue, |
| 479 | * without dequeuing them. Safe against deletion. |
| 480 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
| 481 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
| 482 | * @node: iterator on the queue (struct cds_wfcq_node pointer). |
| 483 | * @n: struct cds_wfcq_node pointer holding the next pointer (used |
| 484 | * internally). |
| 485 | * |
| 486 | * Content written into each node before enqueue is guaranteed to be |
| 487 | * consistent, but no other memory ordering is ensured. |
| 488 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 489 | * caller. |
| 490 | */ |
| 491 | #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \ |
| 492 | for (node = __cds_wfcq_first_blocking(head, tail), \ |
| 493 | n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \ |
| 494 | node != NULL; \ |
| 495 | node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL)) |
| 496 | |
| 497 | #ifdef __cplusplus |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * In C++, implement static inline wrappers using function overloading |
| 502 | * to obtain an API similar to C. |
| 503 | */ |
| 504 | |
| 505 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast_cpp(struct __cds_wfcq_head *head) |
| 506 | { |
| 507 | cds_wfcq_head_ptr_t ret = { ._h = head }; |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast_cpp(struct cds_wfcq_head *head) |
| 512 | { |
| 513 | cds_wfcq_head_ptr_t ret = { .h = head }; |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | template<typename T> static inline bool cds_wfcq_empty(T head, |
| 518 | struct cds_wfcq_tail *tail) |
| 519 | { |
| 520 | return cds_wfcq_empty(cds_wfcq_head_cast_cpp(head), tail); |
| 521 | } |
| 522 | |
| 523 | template<typename T> static inline bool cds_wfcq_enqueue(T head, |
| 524 | struct cds_wfcq_tail *tail, |
| 525 | struct cds_wfcq_node *node) |
| 526 | { |
| 527 | return cds_wfcq_enqueue(cds_wfcq_head_cast_cpp(head), tail, node); |
| 528 | } |
| 529 | |
| 530 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( |
| 531 | T head, struct cds_wfcq_tail *tail) |
| 532 | { |
| 533 | return __cds_wfcq_dequeue_blocking(cds_wfcq_head_cast_cpp(head), tail); |
| 534 | } |
| 535 | |
| 536 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( |
| 537 | T head, struct cds_wfcq_tail *tail, int *state) |
| 538 | { |
| 539 | return __cds_wfcq_dequeue_with_state_blocking(cds_wfcq_head_cast_cpp(head), |
| 540 | tail, state); |
| 541 | } |
| 542 | |
| 543 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( |
| 544 | T head, struct cds_wfcq_tail *tail) |
| 545 | { |
| 546 | return __cds_wfcq_dequeue_nonblocking(cds_wfcq_head_cast_cpp(head), tail); |
| 547 | } |
| 548 | |
| 549 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( |
| 550 | T head, struct cds_wfcq_tail *tail, int *state) |
| 551 | { |
| 552 | return __cds_wfcq_dequeue_with_state_nonblocking(cds_wfcq_head_cast_cpp(head), |
| 553 | tail, state); |
| 554 | } |
| 555 | |
| 556 | template<typename T, typename U> static inline enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
| 557 | T dest_q_head, |
| 558 | struct cds_wfcq_tail *dest_q_tail, |
| 559 | U src_q_head, |
| 560 | struct cds_wfcq_tail *src_q_tail) |
| 561 | { |
| 562 | return __cds_wfcq_splice_blocking(cds_wfcq_head_cast_cpp(dest_q_head), |
| 563 | dest_q_tail, |
| 564 | cds_wfcq_head_cast_cpp(src_q_head), |
| 565 | src_q_tail); |
| 566 | } |
| 567 | |
| 568 | template<typename T, typename U> static inline enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
| 569 | T dest_q_head, |
| 570 | struct cds_wfcq_tail *dest_q_tail, |
| 571 | U *src_q_head, |
| 572 | struct cds_wfcq_tail *src_q_tail) |
| 573 | { |
| 574 | return __cds_wfcq_splice_nonblocking(cds_wfcq_head_cast_cpp(dest_q_head), |
| 575 | dest_q_tail, |
| 576 | cds_wfcq_head_cast_cpp(src_q_head), |
| 577 | src_q_tail); |
| 578 | } |
| 579 | |
| 580 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_first_blocking( |
| 581 | T head, struct cds_wfcq_tail *tail) |
| 582 | { |
| 583 | return __cds_wfcq_first_blocking(cds_wfcq_head_cast_cpp(head), tail); |
| 584 | } |
| 585 | |
| 586 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_first_nonblocking( |
| 587 | T head, struct cds_wfcq_tail *tail) |
| 588 | { |
| 589 | return __cds_wfcq_first_nonblocking(cds_wfcq_head_cast_cpp(head), tail); |
| 590 | } |
| 591 | |
| 592 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_next_blocking( |
| 593 | T head, |
| 594 | struct cds_wfcq_tail *tail, |
| 595 | struct cds_wfcq_node *node) |
| 596 | { |
| 597 | return __cds_wfcq_next_blocking(cds_wfcq_head_cast_cpp(head), tail, node); |
| 598 | } |
| 599 | |
| 600 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_next_nonblocking( |
| 601 | T head, |
| 602 | struct cds_wfcq_tail *tail, |
| 603 | struct cds_wfcq_node *node) |
| 604 | { |
| 605 | return __cds_wfcq_next_nonblocking(cds_wfcq_head_cast_cpp(head), tail, node); |
| 606 | } |
| 607 | |
| 608 | #endif |
| 609 | |
| 610 | #endif /* _URCU_WFCQUEUE_H */ |