| 1 | #ifndef _URCU_WORKQUEUE_FIFO_H |
| 2 | #define _URCU_WORKQUEUE_FIFO_H |
| 3 | |
| 4 | /* |
| 5 | * urcu/workqueue-fifo.h |
| 6 | * |
| 7 | * Userspace RCU library - work queue scheme with FIFO semantic |
| 8 | * |
| 9 | * Copyright (c) 2014 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 | #include <urcu/uatomic.h> |
| 27 | #include <urcu/wfstack.h> |
| 28 | #include <urcu/waitqueue-lifo.h> |
| 29 | #include <urcu/wfcqueue.h> |
| 30 | #include <urcu/rculist.h> |
| 31 | #include <pthread.h> |
| 32 | |
| 33 | /* |
| 34 | * We use RCU to steal work from siblings. Therefore, one of RCU flavors |
| 35 | * need to be included before this header. All worker that participate |
| 36 | * in stealing (initialized with the URCU_WORKER_STEAL flag) need to be |
| 37 | * registered RCU readers threads. |
| 38 | */ |
| 39 | |
| 40 | struct urcu_work { |
| 41 | struct cds_wfcq_node node; |
| 42 | }; |
| 43 | |
| 44 | struct urcu_workqueue { |
| 45 | /* FIFO work queue */ |
| 46 | struct __cds_wfcq_head head; |
| 47 | struct cds_wfcq_tail tail; |
| 48 | |
| 49 | /* Associated wait queue for LIFO wait/wakeup */ |
| 50 | struct urcu_wait_queue waitqueue; |
| 51 | |
| 52 | /* RCU linked list head of siblings for work stealing. */ |
| 53 | struct cds_list_head sibling_head; |
| 54 | pthread_mutex_t sibling_lock; /* Protect sibling list updates */ |
| 55 | }; |
| 56 | |
| 57 | struct urcu_worker { |
| 58 | struct cds_wfcq_head head; |
| 59 | struct cds_wfcq_tail tail; |
| 60 | |
| 61 | struct urcu_wait_node wait_node; |
| 62 | /* RCU linked list node of siblings for work stealing. */ |
| 63 | struct cds_list_head sibling_node; |
| 64 | int flags; /* enum urcu_worker_flags */ |
| 65 | }; |
| 66 | |
| 67 | enum urcu_worker_flags { |
| 68 | URCU_WORKER_STEAL = (1 << 0), |
| 69 | }; |
| 70 | |
| 71 | static inline |
| 72 | void urcu_workqueue_init(struct urcu_workqueue *queue) |
| 73 | { |
| 74 | __cds_wfcq_init(&queue->head, &queue->tail); |
| 75 | urcu_wait_queue_init(&queue->waitqueue); |
| 76 | CDS_INIT_LIST_HEAD(&queue->sibling_head); |
| 77 | } |
| 78 | |
| 79 | static inline |
| 80 | void urcu_queue_work(struct urcu_workqueue *queue, struct urcu_work *work) |
| 81 | { |
| 82 | bool was_empty; |
| 83 | |
| 84 | cds_wfcq_node_init(&work->node); |
| 85 | |
| 86 | /* Enqueue work. */ |
| 87 | was_empty = !cds_wfcq_enqueue(&queue->head, &queue->tail, |
| 88 | &work->node); |
| 89 | /* |
| 90 | * If workqueue was previously empty, wakeup one worker thread. |
| 91 | * It will eventually grab the entire content of the work-queue |
| 92 | * (therefore grabbing a "work batch"). After having grabbed the |
| 93 | * work batch, while that thread is running and taking care of |
| 94 | * that work batch, when we enqueue more work, we will wake |
| 95 | * another thread (if there is one waiting), which will |
| 96 | * eventually grab the new batch, and so on. This scheme ensures |
| 97 | * that contiguous batch of work are handled by the same thread |
| 98 | * (for locality), and also ensures that we scale work to many |
| 99 | * worker threads when threads are busy enough to still be |
| 100 | * running when work is enqueued. |
| 101 | */ |
| 102 | if (was_empty) |
| 103 | (void) urcu_dequeue_wake_single(&queue->waitqueue); |
| 104 | } |
| 105 | |
| 106 | static inline |
| 107 | void urcu_workqueue_wakeup_all(struct urcu_workqueue *queue) |
| 108 | { |
| 109 | struct urcu_waiters waiters; |
| 110 | |
| 111 | urcu_move_waiters(&waiters, &queue->waitqueue); |
| 112 | (void) urcu_wake_all_waiters(&waiters); |
| 113 | } |
| 114 | |
| 115 | static inline |
| 116 | void urcu_worker_init(struct urcu_worker *worker, int flags) |
| 117 | { |
| 118 | cds_wfcq_init(&worker->head, &worker->tail); |
| 119 | worker->flags = flags; |
| 120 | urcu_wait_node_init(&worker->wait_node, URCU_WAIT_RUNNING); |
| 121 | } |
| 122 | |
| 123 | static inline |
| 124 | void urcu_worker_register(struct urcu_workqueue *queue, |
| 125 | struct urcu_worker *worker) |
| 126 | { |
| 127 | if (worker->flags & URCU_WORKER_STEAL) { |
| 128 | pthread_mutex_lock(&queue->sibling_lock); |
| 129 | cds_list_add_rcu(&worker->sibling_node, &queue->sibling_head); |
| 130 | pthread_mutex_unlock(&queue->sibling_lock); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static inline |
| 135 | void urcu_worker_unregister(struct urcu_workqueue *queue, |
| 136 | struct urcu_worker *worker) |
| 137 | { |
| 138 | enum cds_wfcq_ret wfcq_ret; |
| 139 | |
| 140 | if (worker->flags & URCU_WORKER_STEAL) { |
| 141 | pthread_mutex_lock(&queue->sibling_lock); |
| 142 | cds_list_del_rcu(&worker->sibling_node); |
| 143 | pthread_mutex_unlock(&queue->sibling_lock); |
| 144 | |
| 145 | /* |
| 146 | * Wait for grace period before freeing or reusing |
| 147 | * "worker" because used by RCU linked list. |
| 148 | */ |
| 149 | synchronize_rcu(); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Put any local work we still have back into the workqueue. |
| 154 | */ |
| 155 | wfcq_ret = __cds_wfcq_splice_blocking(&queue->head, |
| 156 | &queue->tail, |
| 157 | &worker->head, |
| 158 | &worker->tail); |
| 159 | if (wfcq_ret != CDS_WFCQ_RET_SRC_EMPTY |
| 160 | && wfcq_ret == CDS_WFCQ_RET_DEST_EMPTY) { |
| 161 | /* |
| 162 | * Wakeup worker thread if we have put work back into |
| 163 | * workqueue that was previously empty. |
| 164 | */ |
| 165 | (void) urcu_dequeue_wake_single(&queue->waitqueue); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Try stealing work from siblings when we have nothing to do. |
| 171 | */ |
| 172 | static inline |
| 173 | void ___urcu_steal_work(struct urcu_worker *worker, |
| 174 | struct urcu_worker *sibling) |
| 175 | { |
| 176 | /* |
| 177 | * Don't bother grabbing the sibling queue lock if it is empty. |
| 178 | */ |
| 179 | if (cds_wfcq_empty(&sibling->head, &sibling->tail)) |
| 180 | return; |
| 181 | cds_wfcq_dequeue_lock(&sibling->head, &sibling->tail); |
| 182 | (void) __cds_wfcq_splice_blocking(&worker->head, |
| 183 | &worker->tail, |
| 184 | &sibling->head, |
| 185 | &sibling->tail); |
| 186 | cds_wfcq_dequeue_unlock(&sibling->head, &sibling->tail); |
| 187 | } |
| 188 | |
| 189 | static inline |
| 190 | int __urcu_steal_work(struct urcu_workqueue *queue, |
| 191 | struct urcu_worker *worker) |
| 192 | { |
| 193 | struct urcu_worker *sibling_prev, *sibling_next; |
| 194 | struct cds_list_head *sibling_node; |
| 195 | |
| 196 | if (!(worker->flags & URCU_WORKER_STEAL)) |
| 197 | return 0; |
| 198 | |
| 199 | rcu_read_lock(); |
| 200 | |
| 201 | sibling_node = rcu_dereference(worker->sibling_node.next); |
| 202 | if (sibling_node == &queue->sibling_head) |
| 203 | sibling_node = rcu_dereference(sibling_node->next); |
| 204 | sibling_next = caa_container_of(sibling_node, struct urcu_worker, |
| 205 | sibling_node); |
| 206 | if (sibling_next != worker) |
| 207 | ___urcu_steal_work(worker, sibling_next); |
| 208 | |
| 209 | sibling_node = rcu_dereference(worker->sibling_node.prev); |
| 210 | if (sibling_node == &queue->sibling_head) |
| 211 | sibling_node = rcu_dereference(sibling_node->prev); |
| 212 | sibling_prev = caa_container_of(sibling_node, struct urcu_worker, |
| 213 | sibling_node); |
| 214 | if (sibling_prev != worker && sibling_prev != sibling_next) |
| 215 | ___urcu_steal_work(worker, sibling_prev); |
| 216 | |
| 217 | rcu_read_unlock(); |
| 218 | |
| 219 | return !cds_wfcq_empty(&worker->head, &worker->tail); |
| 220 | } |
| 221 | |
| 222 | static inline |
| 223 | void ___urcu_wakeup_sibling(struct urcu_worker *sibling) |
| 224 | { |
| 225 | urcu_adaptative_wake_up(&sibling->wait_node); |
| 226 | } |
| 227 | |
| 228 | static inline |
| 229 | void __urcu_wakeup_siblings(struct urcu_workqueue *queue, |
| 230 | struct urcu_worker *worker) |
| 231 | { |
| 232 | struct urcu_worker *sibling_prev, *sibling_next; |
| 233 | struct cds_list_head *sibling_node; |
| 234 | |
| 235 | if (!(worker->flags & URCU_WORKER_STEAL)) |
| 236 | return; |
| 237 | |
| 238 | /* Only wakeup siblings if we have work in our own queue. */ |
| 239 | if (cds_wfcq_empty(&worker->head, &worker->tail)) |
| 240 | return; |
| 241 | |
| 242 | rcu_read_lock(); |
| 243 | |
| 244 | sibling_node = rcu_dereference(worker->sibling_node.next); |
| 245 | if (sibling_node == &queue->sibling_head) |
| 246 | sibling_node = rcu_dereference(sibling_node->next); |
| 247 | sibling_next = caa_container_of(sibling_node, struct urcu_worker, |
| 248 | sibling_node); |
| 249 | if (sibling_next != worker) |
| 250 | ___urcu_wakeup_sibling(sibling_next); |
| 251 | |
| 252 | sibling_node = rcu_dereference(worker->sibling_node.prev); |
| 253 | if (sibling_node == &queue->sibling_head) |
| 254 | sibling_node = rcu_dereference(sibling_node->prev); |
| 255 | sibling_prev = caa_container_of(sibling_node, struct urcu_worker, |
| 256 | sibling_node); |
| 257 | if (sibling_prev != worker && sibling_prev != sibling_next) |
| 258 | ___urcu_wakeup_sibling(sibling_prev); |
| 259 | |
| 260 | rcu_read_unlock(); |
| 261 | } |
| 262 | |
| 263 | static inline |
| 264 | void urcu_accept_work(struct urcu_workqueue *queue, |
| 265 | struct urcu_worker *worker, |
| 266 | int blocking) |
| 267 | { |
| 268 | enum cds_wfcq_ret wfcq_ret; |
| 269 | |
| 270 | wfcq_ret = __cds_wfcq_splice_blocking(&worker->head, |
| 271 | &worker->tail, |
| 272 | &queue->head, |
| 273 | &queue->tail); |
| 274 | /* Don't wait if we have work to do. */ |
| 275 | if (wfcq_ret != CDS_WFCQ_RET_SRC_EMPTY |
| 276 | || !cds_wfcq_empty(&worker->head, |
| 277 | &worker->tail)) |
| 278 | goto do_work; |
| 279 | /* Try to steal work from sibling instead of blocking */ |
| 280 | if (__urcu_steal_work(queue, worker)) |
| 281 | goto do_work; |
| 282 | if (!blocking) |
| 283 | return; |
| 284 | urcu_wait_set_state(&worker->wait_node, |
| 285 | URCU_WAIT_WAITING); |
| 286 | if (!CMM_LOAD_SHARED(worker->wait_node.node.next)) { |
| 287 | int was_empty; |
| 288 | |
| 289 | /* |
| 290 | * NULL next pointer. We are therefore not in |
| 291 | * the queue. |
| 292 | */ |
| 293 | cds_wfs_node_init(&worker->wait_node.node); |
| 294 | was_empty = !urcu_wait_add(&queue->waitqueue, |
| 295 | &worker->wait_node); |
| 296 | /* |
| 297 | * If the wait queue was empty, it means we are the |
| 298 | * first thread to be put back into an otherwise empty |
| 299 | * wait queue. Re-check if work queue is empty after |
| 300 | * adding ourself to wait queue, so we can wakeup the |
| 301 | * top of wait queue since new work have appeared, and |
| 302 | * work enqueuer may not have seen that it needed to do |
| 303 | * a wake up. |
| 304 | */ |
| 305 | if (was_empty && !cds_wfcq_empty(&queue->head, |
| 306 | &queue->tail)) |
| 307 | (void) urcu_dequeue_wake_single(&queue->waitqueue); |
| 308 | } else { |
| 309 | /* |
| 310 | * Non-NULL next pointer. We are therefore in |
| 311 | * the queue, or the dispatcher just removed us |
| 312 | * from it (after we read the next pointer), and |
| 313 | * is therefore awakening us. The state will |
| 314 | * therefore have been changed from WAITING to |
| 315 | * some other state, which will let the busy |
| 316 | * wait pass through. |
| 317 | */ |
| 318 | } |
| 319 | urcu_adaptative_busy_wait(&worker->wait_node); |
| 320 | return; |
| 321 | |
| 322 | do_work: |
| 323 | /* |
| 324 | * We will be busy handling the work batch, awaken siblings so |
| 325 | * they can steal from us. |
| 326 | */ |
| 327 | __urcu_wakeup_siblings(queue, worker); |
| 328 | } |
| 329 | |
| 330 | static inline |
| 331 | struct urcu_work *urcu_dequeue_work(struct urcu_worker *worker) |
| 332 | { |
| 333 | struct cds_wfcq_node *node; |
| 334 | |
| 335 | /* |
| 336 | * If we are registered for work stealing, we need to dequeue |
| 337 | * safely against siblings. |
| 338 | */ |
| 339 | if (worker->flags & URCU_WORKER_STEAL) { |
| 340 | /* |
| 341 | * Don't bother grabbing the worker queue lock if it is |
| 342 | * empty. |
| 343 | */ |
| 344 | if (cds_wfcq_empty(&worker->head, &worker->tail)) |
| 345 | return NULL; |
| 346 | node = cds_wfcq_dequeue_blocking(&worker->head, |
| 347 | &worker->tail); |
| 348 | } else { |
| 349 | node = ___cds_wfcq_dequeue_with_state(&worker->head, |
| 350 | &worker->tail, NULL, 1, 0); |
| 351 | } |
| 352 | if (!node) |
| 353 | return NULL; |
| 354 | return caa_container_of(node, struct urcu_work, node); |
| 355 | } |
| 356 | |
| 357 | #endif /* _URCU_WORKQUEUE_FIFO_H */ |