| 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 | * This queue has been designed and implemented collaboratively by |
| 45 | * Mathieu Desnoyers and Lai Jiangshan. Inspired from |
| 46 | * half-wait-free/half-blocking queue implementation done by Paul E. |
| 47 | * McKenney. |
| 48 | * |
| 49 | * Mutual exclusion of __cds_wfcq_* API |
| 50 | * |
| 51 | * Unless otherwise stated, the caller must ensure mutual exclusion of |
| 52 | * queue update operations "dequeue" and "splice" (for source queue). |
| 53 | * Queue read operations "first" and "next", which are used by |
| 54 | * "for_each" iterations, need to be protected against concurrent |
| 55 | * "dequeue" and "splice" (for source queue) by the caller. |
| 56 | * "enqueue", "splice" (for destination queue), and "empty" are the only |
| 57 | * operations that can be used without any mutual exclusion. |
| 58 | * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock(). |
| 59 | * |
| 60 | * For convenience, cds_wfcq_dequeue_blocking() and |
| 61 | * cds_wfcq_splice_blocking() hold the dequeue lock. |
| 62 | * |
| 63 | * Besides locking, mutual exclusion of dequeue, splice and iteration |
| 64 | * can be ensured by performing all of those operations from a single |
| 65 | * thread, without requiring any lock. |
| 66 | */ |
| 67 | |
| 68 | #define WFCQ_ADAPT_ATTEMPTS 10 /* Retry if being set */ |
| 69 | #define WFCQ_WAIT 10 /* Wait 10 ms if being set */ |
| 70 | |
| 71 | /* |
| 72 | * cds_wfcq_node_init: initialize wait-free queue node. |
| 73 | */ |
| 74 | static inline void _cds_wfcq_node_init(struct cds_wfcq_node *node) |
| 75 | { |
| 76 | node->next = NULL; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * cds_wfcq_init: initialize wait-free queue. |
| 81 | */ |
| 82 | static inline void _cds_wfcq_init(struct cds_wfcq_head *head, |
| 83 | struct cds_wfcq_tail *tail) |
| 84 | { |
| 85 | int ret; |
| 86 | |
| 87 | /* Set queue head and tail */ |
| 88 | _cds_wfcq_node_init(&head->node); |
| 89 | tail->p = &head->node; |
| 90 | ret = pthread_mutex_init(&head->lock, NULL); |
| 91 | assert(!ret); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * cds_wfcq_empty: return whether wait-free queue is empty. |
| 96 | * |
| 97 | * No memory barrier is issued. No mutual exclusion is required. |
| 98 | */ |
| 99 | static inline bool _cds_wfcq_empty(struct cds_wfcq_head *head, |
| 100 | struct cds_wfcq_tail *tail) |
| 101 | { |
| 102 | /* |
| 103 | * Queue is empty if no node is pointed by head->node.next nor |
| 104 | * tail->p. Even though the tail->p check is sufficient to find |
| 105 | * out of the queue is empty, we first check head->node.next as a |
| 106 | * common case to ensure that dequeuers do not frequently access |
| 107 | * enqueuer's tail->p cache line. |
| 108 | */ |
| 109 | return CMM_LOAD_SHARED(head->node.next) == NULL |
| 110 | && CMM_LOAD_SHARED(tail->p) == &head->node; |
| 111 | } |
| 112 | |
| 113 | static inline void _cds_wfcq_dequeue_lock(struct cds_wfcq_head *head, |
| 114 | struct cds_wfcq_tail *tail) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = pthread_mutex_lock(&head->lock); |
| 119 | assert(!ret); |
| 120 | } |
| 121 | |
| 122 | static inline void _cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head, |
| 123 | struct cds_wfcq_tail *tail) |
| 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | ret = pthread_mutex_unlock(&head->lock); |
| 128 | assert(!ret); |
| 129 | } |
| 130 | |
| 131 | static inline void ___cds_wfcq_append(struct cds_wfcq_head *head, |
| 132 | struct cds_wfcq_tail *tail, |
| 133 | struct cds_wfcq_node *new_head, |
| 134 | struct cds_wfcq_node *new_tail) |
| 135 | { |
| 136 | struct cds_wfcq_node *old_tail; |
| 137 | |
| 138 | /* |
| 139 | * Implicit memory barrier before uatomic_xchg() orders earlier |
| 140 | * stores to data structure containing node and setting |
| 141 | * node->next to NULL before publication. |
| 142 | */ |
| 143 | old_tail = uatomic_xchg(&tail->p, new_tail); |
| 144 | |
| 145 | /* |
| 146 | * Implicit memory barrier after uatomic_xchg() orders store to |
| 147 | * q->tail before store to old_tail->next. |
| 148 | * |
| 149 | * At this point, dequeuers see a NULL tail->p->next, which |
| 150 | * indicates that the queue is being appended to. The following |
| 151 | * store will append "node" to the queue from a dequeuer |
| 152 | * perspective. |
| 153 | */ |
| 154 | CMM_STORE_SHARED(old_tail->next, new_head); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * cds_wfcq_enqueue: enqueue a node into a wait-free queue. |
| 159 | * |
| 160 | * Issues a full memory barrier before enqueue. No mutual exclusion is |
| 161 | * required. |
| 162 | */ |
| 163 | static inline void _cds_wfcq_enqueue(struct cds_wfcq_head *head, |
| 164 | struct cds_wfcq_tail *tail, |
| 165 | struct cds_wfcq_node *new_tail) |
| 166 | { |
| 167 | ___cds_wfcq_append(head, tail, new_tail, new_tail); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Waiting for enqueuer to complete enqueue and return the next node. |
| 172 | */ |
| 173 | static inline struct cds_wfcq_node * |
| 174 | ___cds_wfcq_node_sync_next(struct cds_wfcq_node *node) |
| 175 | { |
| 176 | struct cds_wfcq_node *next; |
| 177 | int attempt = 0; |
| 178 | |
| 179 | /* |
| 180 | * Adaptative busy-looping waiting for enqueuer to complete enqueue. |
| 181 | */ |
| 182 | while ((next = CMM_LOAD_SHARED(node->next)) == NULL) { |
| 183 | if (++attempt >= WFCQ_ADAPT_ATTEMPTS) { |
| 184 | poll(NULL, 0, WFCQ_WAIT); /* Wait for 10ms */ |
| 185 | attempt = 0; |
| 186 | } else { |
| 187 | caa_cpu_relax(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return next; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing. |
| 196 | * |
| 197 | * Content written into the node before enqueue is guaranteed to be |
| 198 | * consistent, but no other memory ordering is ensured. |
| 199 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 200 | * caller. |
| 201 | * |
| 202 | * Used by for-like iteration macros in urcu/wfqueue.h: |
| 203 | * __cds_wfcq_for_each_blocking() |
| 204 | * __cds_wfcq_for_each_blocking_safe() |
| 205 | */ |
| 206 | static inline struct cds_wfcq_node * |
| 207 | ___cds_wfcq_first_blocking(struct cds_wfcq_head *head, |
| 208 | struct cds_wfcq_tail *tail) |
| 209 | { |
| 210 | struct cds_wfcq_node *node; |
| 211 | |
| 212 | if (_cds_wfcq_empty(head, tail)) |
| 213 | return NULL; |
| 214 | node = ___cds_wfcq_node_sync_next(&head->node); |
| 215 | /* Load head->node.next before loading node's content */ |
| 216 | cmm_smp_read_barrier_depends(); |
| 217 | return node; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. |
| 222 | * |
| 223 | * Content written into the node before enqueue is guaranteed to be |
| 224 | * consistent, but no other memory ordering is ensured. |
| 225 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 226 | * caller. |
| 227 | * |
| 228 | * Used by for-like iteration macros in urcu/wfqueue.h: |
| 229 | * __cds_wfcq_for_each_blocking() |
| 230 | * __cds_wfcq_for_each_blocking_safe() |
| 231 | */ |
| 232 | static inline struct cds_wfcq_node * |
| 233 | ___cds_wfcq_next_blocking(struct cds_wfcq_head *head, |
| 234 | struct cds_wfcq_tail *tail, |
| 235 | struct cds_wfcq_node *node) |
| 236 | { |
| 237 | struct cds_wfcq_node *next; |
| 238 | |
| 239 | /* |
| 240 | * Even though the following tail->p check is sufficient to find |
| 241 | * out if we reached the end of the queue, we first check |
| 242 | * node->next as a common case to ensure that iteration on nodes |
| 243 | * do not frequently access enqueuer's tail->p cache line. |
| 244 | */ |
| 245 | if ((next = CMM_LOAD_SHARED(node->next)) == NULL) { |
| 246 | /* Load node->next before tail->p */ |
| 247 | cmm_smp_rmb(); |
| 248 | if (CMM_LOAD_SHARED(tail->p) == node) |
| 249 | return NULL; |
| 250 | next = ___cds_wfcq_node_sync_next(node); |
| 251 | } |
| 252 | /* Load node->next before loading next's content */ |
| 253 | cmm_smp_read_barrier_depends(); |
| 254 | return next; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * __cds_wfcq_dequeue_blocking: dequeue a node from the queue. |
| 259 | * |
| 260 | * Content written into the node before enqueue is guaranteed to be |
| 261 | * consistent, but no other memory ordering is ensured. |
| 262 | * It is valid to reuse and free a dequeued node immediately. |
| 263 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
| 264 | * caller. |
| 265 | */ |
| 266 | static inline struct cds_wfcq_node * |
| 267 | ___cds_wfcq_dequeue_blocking(struct cds_wfcq_head *head, |
| 268 | struct cds_wfcq_tail *tail) |
| 269 | { |
| 270 | struct cds_wfcq_node *node, *next; |
| 271 | |
| 272 | if (_cds_wfcq_empty(head, tail)) |
| 273 | return NULL; |
| 274 | |
| 275 | node = ___cds_wfcq_node_sync_next(&head->node); |
| 276 | |
| 277 | if ((next = CMM_LOAD_SHARED(node->next)) == NULL) { |
| 278 | /* |
| 279 | * @node is probably the only node in the queue. |
| 280 | * Try to move the tail to &q->head. |
| 281 | * q->head.next is set to NULL here, and stays |
| 282 | * NULL if the cmpxchg succeeds. Should the |
| 283 | * cmpxchg fail due to a concurrent enqueue, the |
| 284 | * q->head.next will be set to the next node. |
| 285 | * The implicit memory barrier before |
| 286 | * uatomic_cmpxchg() orders load node->next |
| 287 | * before loading q->tail. |
| 288 | * The implicit memory barrier before uatomic_cmpxchg |
| 289 | * orders load q->head.next before loading node's |
| 290 | * content. |
| 291 | */ |
| 292 | _cds_wfcq_node_init(&head->node); |
| 293 | if (uatomic_cmpxchg(&tail->p, node, &head->node) == node) |
| 294 | return node; |
| 295 | next = ___cds_wfcq_node_sync_next(node); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Move queue head forward. |
| 300 | */ |
| 301 | head->node.next = next; |
| 302 | |
| 303 | /* Load q->head.next before loading node's content */ |
| 304 | cmm_smp_read_barrier_depends(); |
| 305 | return node; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. |
| 310 | * |
| 311 | * Dequeue all nodes from src_q. |
| 312 | * dest_q must be already initialized. |
| 313 | * Dequeue/splice/iteration mutual exclusion for src_q should be ensured |
| 314 | * by the caller. |
| 315 | */ |
| 316 | static inline void |
| 317 | ___cds_wfcq_splice_blocking( |
| 318 | struct cds_wfcq_head *dest_q_head, |
| 319 | struct cds_wfcq_tail *dest_q_tail, |
| 320 | struct cds_wfcq_head *src_q_head, |
| 321 | struct cds_wfcq_tail *src_q_tail) |
| 322 | { |
| 323 | struct cds_wfcq_node *head, *tail; |
| 324 | |
| 325 | if (_cds_wfcq_empty(src_q_head, src_q_tail)) |
| 326 | return; |
| 327 | |
| 328 | head = ___cds_wfcq_node_sync_next(&src_q_head->node); |
| 329 | _cds_wfcq_node_init(&src_q_head->node); |
| 330 | |
| 331 | /* |
| 332 | * Memory barrier implied before uatomic_xchg() orders store to |
| 333 | * src_q->head before store to src_q->tail. This is required by |
| 334 | * concurrent enqueue on src_q, which exchanges the tail before |
| 335 | * updating the previous tail's next pointer. |
| 336 | */ |
| 337 | tail = uatomic_xchg(&src_q_tail->p, &src_q_head->node); |
| 338 | |
| 339 | /* |
| 340 | * Append the spliced content of src_q into dest_q. Does not |
| 341 | * require mutual exclusion on dest_q (wait-free). |
| 342 | */ |
| 343 | ___cds_wfcq_append(dest_q_head, dest_q_tail, head, tail); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
| 348 | * |
| 349 | * Content written into the node before enqueue is guaranteed to be |
| 350 | * consistent, but no other memory ordering is ensured. |
| 351 | * Mutual exlusion with cds_wfcq_splice_blocking and dequeue lock is |
| 352 | * ensured. |
| 353 | * It is valid to reuse and free a dequeued node immediately. |
| 354 | */ |
| 355 | static inline struct cds_wfcq_node * |
| 356 | _cds_wfcq_dequeue_blocking(struct cds_wfcq_head *head, |
| 357 | struct cds_wfcq_tail *tail) |
| 358 | { |
| 359 | struct cds_wfcq_node *retval; |
| 360 | |
| 361 | _cds_wfcq_dequeue_lock(head, tail); |
| 362 | retval = ___cds_wfcq_dequeue_blocking(head, tail); |
| 363 | _cds_wfcq_dequeue_unlock(head, tail); |
| 364 | return retval; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. |
| 369 | * |
| 370 | * Dequeue all nodes from src_q. |
| 371 | * dest_q must be already initialized. |
| 372 | * Content written into the node before enqueue is guaranteed to be |
| 373 | * consistent, but no other memory ordering is ensured. |
| 374 | * Mutual exlusion with cds_wfcq_dequeue_blocking and dequeue lock is |
| 375 | * ensured. |
| 376 | */ |
| 377 | static inline void |
| 378 | _cds_wfcq_splice_blocking( |
| 379 | struct cds_wfcq_head *dest_q_head, |
| 380 | struct cds_wfcq_tail *dest_q_tail, |
| 381 | struct cds_wfcq_head *src_q_head, |
| 382 | struct cds_wfcq_tail *src_q_tail) |
| 383 | { |
| 384 | _cds_wfcq_dequeue_lock(src_q_head, src_q_tail); |
| 385 | ___cds_wfcq_splice_blocking(dest_q_head, dest_q_tail, |
| 386 | src_q_head, src_q_tail); |
| 387 | _cds_wfcq_dequeue_unlock(src_q_head, src_q_tail); |
| 388 | } |
| 389 | |
| 390 | #ifdef __cplusplus |
| 391 | } |
| 392 | #endif |
| 393 | |
| 394 | #endif /* _URCU_WFCQUEUE_STATIC_H */ |