1 #ifndef _URCU_WORKQUEUE_FIFO_H
2 #define _URCU_WORKQUEUE_FIFO_H
5 * urcu/workqueue-fifo.h
7 * Userspace RCU library - work queue scheme with FIFO semantic
9 * Copyright (c) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
26 #include <urcu/uatomic.h>
27 #include <urcu/lfstack.h>
28 #include <urcu/waitqueue-lifo.h>
29 #include <urcu/wfcqueue.h>
30 #include <urcu/rculist.h>
34 enum urcu_accept_ret
{
36 URCU_ACCEPT_SHUTDOWN
= 1,
40 * We use RCU to steal work from siblings. Therefore, one of RCU flavors
41 * need to be included before this header. All worker that participate
42 * in stealing (initialized with the URCU_WORKER_STEAL flag) need to be
43 * registered RCU readers threads.
47 struct cds_wfcq_node node
;
50 struct urcu_workqueue
{
52 struct __cds_wfcq_head head
;
53 struct cds_wfcq_tail tail
;
55 /* Associated wait queue for LIFO wait/wakeup */
56 struct urcu_wait_queue waitqueue
;
58 /* RCU linked list head of siblings for work stealing. */
59 struct cds_list_head sibling_head
;
60 pthread_mutex_t sibling_lock
; /* Protect sibling list updates */
62 bool shutdown
; /* Shutdown performed */
66 /* Workqueue which can be either used by worker, or stolen. */
67 struct cds_wfcq_head head
;
68 struct cds_wfcq_tail tail
;
70 /* Work belonging to worker. Cannot be stolen. */
71 struct urcu_work
*own
;
73 struct urcu_wait_node wait_node
;
74 /* RCU linked list node of siblings for work stealing. */
75 struct cds_list_head sibling_node
;
76 int flags
; /* enum urcu_worker_flags */
79 enum urcu_worker_flags
{
80 URCU_WORKER_STEAL
= (1 << 0),
84 void urcu_workqueue_init(struct urcu_workqueue
*queue
)
86 __cds_wfcq_init(&queue
->head
, &queue
->tail
);
87 urcu_wait_queue_init(&queue
->waitqueue
);
88 CDS_INIT_LIST_HEAD(&queue
->sibling_head
);
89 queue
->shutdown
= false;
93 void urcu_queue_work(struct urcu_workqueue
*queue
, struct urcu_work
*work
)
97 cds_wfcq_node_init(&work
->node
);
100 was_empty
= !cds_wfcq_enqueue(&queue
->head
, &queue
->tail
,
103 * If workqueue was previously empty, wakeup one worker thread.
104 * It will eventually grab the entire content of the work-queue
105 * (therefore grabbing a "work batch"). After having grabbed the
106 * work batch, while that thread is running and taking care of
107 * that work batch, when we enqueue more work, we will wake
108 * another thread (if there is one waiting), which will
109 * eventually grab the new batch, and so on. This scheme ensures
110 * that contiguous batch of work are handled by the same thread
111 * (for locality), and also ensures that we scale work to many
112 * worker threads when threads are busy enough to still be
113 * running when work is enqueued.
116 rcu_read_lock(); /* Protect stack dequeue */
117 (void) urcu_dequeue_wake_single(&queue
->waitqueue
);
118 rcu_read_unlock(); /* Protect stack dequeue */
123 void __urcu_workqueue_wakeup_all(struct urcu_workqueue
*queue
)
125 struct urcu_waiters waiters
;
127 rcu_read_lock(); /* Protect stack dequeue */
128 urcu_move_waiters(&waiters
, &queue
->waitqueue
);
129 rcu_read_unlock(); /* Protect stack dequeue */
131 (void) urcu_wake_all_waiters(&waiters
);
135 void urcu_worker_init(struct urcu_worker
*worker
, int flags
)
137 cds_wfcq_init(&worker
->head
, &worker
->tail
);
138 worker
->flags
= flags
;
139 urcu_wait_node_init(&worker
->wait_node
, URCU_WAIT_RUNNING
);
144 void urcu_worker_register(struct urcu_workqueue
*queue
,
145 struct urcu_worker
*worker
)
147 if (worker
->flags
& URCU_WORKER_STEAL
) {
148 pthread_mutex_lock(&queue
->sibling_lock
);
149 cds_list_add_rcu(&worker
->sibling_node
, &queue
->sibling_head
);
150 pthread_mutex_unlock(&queue
->sibling_lock
);
155 void urcu_worker_unregister(struct urcu_workqueue
*queue
,
156 struct urcu_worker
*worker
)
158 enum cds_wfcq_ret wfcq_ret
;
160 if (worker
->flags
& URCU_WORKER_STEAL
) {
161 pthread_mutex_lock(&queue
->sibling_lock
);
162 cds_list_del_rcu(&worker
->sibling_node
);
163 pthread_mutex_unlock(&queue
->sibling_lock
);
167 * Make sure we are removed from waitqueue.
169 if (CMM_LOAD_SHARED(worker
->wait_node
.node
.next
))
170 __urcu_workqueue_wakeup_all(queue
);
173 * Put any local work we still have back into the workqueue.
175 wfcq_ret
= __cds_wfcq_splice_blocking(&queue
->head
,
179 if (wfcq_ret
!= CDS_WFCQ_RET_SRC_EMPTY
180 && wfcq_ret
== CDS_WFCQ_RET_DEST_EMPTY
) {
182 * Wakeup worker thread if we have put work back into
183 * workqueue that was previously empty.
185 rcu_read_lock(); /* Protect stack dequeue */
186 (void) urcu_dequeue_wake_single(&queue
->waitqueue
);
187 rcu_read_unlock(); /* Protect stack dequeue */
191 * Wait for grace period before freeing or reusing
192 * "worker" because used by RCU linked list.
193 * Also prevents ABA for waitqueue stack dequeue: matches RCU
194 * read-side critical sections around dequeue and move all
195 * operations on waitqueue).
201 bool ___urcu_grab_work(struct urcu_worker
*worker
,
202 cds_wfcq_head_ptr_t src_head
,
203 struct cds_wfcq_tail
*src_tail
,
206 enum cds_wfcq_ret splice_ret
;
207 struct cds_wfcq_head tmp_head
;
208 struct cds_wfcq_tail tmp_tail
;
209 struct cds_wfcq_node
*node
;
212 * Don't bother grabbing the src queue lock if it is empty.
214 if (cds_wfcq_empty(src_head
, src_tail
))
216 cds_wfcq_init(&tmp_head
, &tmp_tail
);
218 /* Ensure that we preserve FIFO work order. */
219 assert(!steal
|| worker
->own
== NULL
);
221 /* Splice to temporary queue. */
223 cds_wfcq_dequeue_lock(src_head
.h
, src_tail
);
224 splice_ret
= __cds_wfcq_splice_blocking(&tmp_head
,
229 cds_wfcq_dequeue_unlock(src_head
.h
, src_tail
);
230 if (splice_ret
== CDS_WFCQ_RET_SRC_EMPTY
)
234 * Keep one work entry for ourself. This ensures forward
235 * progress amongst stealing co-workers. This also ensures that
236 * when a worker grab some work from the global workqueue, it
237 * will have at least one work item to deal with.
239 if (worker
->own
== NULL
) {
242 * Try to grab own work from worker workqueue to
243 * preserve FIFO order.
245 node
= cds_wfcq_dequeue_blocking(&worker
->head
,
250 node
= __cds_wfcq_dequeue_blocking(&tmp_head
, &tmp_tail
);
251 assert(node
!= NULL
);
253 worker
->own
= caa_container_of(node
, struct urcu_work
, node
);
256 /* Splice into worker workqueue. */
257 splice_ret
= cds_wfcq_splice_blocking(&worker
->head
,
261 /* Ensure that we preserve FIFO work order. */
262 assert(!steal
|| splice_ret
!= CDS_WFCQ_RET_DEST_NON_EMPTY
);
267 * Try stealing work from siblings when we have nothing to do.
270 bool ___urcu_steal_work(struct urcu_worker
*worker
,
271 struct urcu_worker
*sibling
)
273 return ___urcu_grab_work(worker
, &sibling
->head
, &sibling
->tail
, 1);
277 bool __urcu_steal_work(struct urcu_workqueue
*queue
,
278 struct urcu_worker
*worker
)
280 struct urcu_worker
*sibling_prev
, *sibling_next
;
281 struct cds_list_head
*sibling_node
;
282 bool steal_performed
= 0;
284 if (!(worker
->flags
& URCU_WORKER_STEAL
))
289 sibling_node
= rcu_dereference(worker
->sibling_node
.next
);
290 if (sibling_node
== &queue
->sibling_head
)
291 sibling_node
= rcu_dereference(sibling_node
->next
);
292 sibling_next
= caa_container_of(sibling_node
, struct urcu_worker
,
294 if (sibling_next
!= worker
)
295 steal_performed
= ___urcu_steal_work(worker
, sibling_next
);
299 sibling_node
= rcu_dereference(worker
->sibling_node
.prev
);
300 if (sibling_node
== &queue
->sibling_head
)
301 sibling_node
= rcu_dereference(sibling_node
->prev
);
302 sibling_prev
= caa_container_of(sibling_node
, struct urcu_worker
,
304 if (sibling_prev
!= worker
&& sibling_prev
!= sibling_next
)
305 steal_performed
= ___urcu_steal_work(worker
, sibling_prev
);
309 return steal_performed
;
313 bool ___urcu_wakeup_sibling(struct urcu_worker
*sibling
)
315 return urcu_adaptative_wake_up(&sibling
->wait_node
);
319 bool __urcu_wakeup_siblings(struct urcu_workqueue
*queue
,
320 struct urcu_worker
*worker
)
322 struct urcu_worker
*sibling_prev
, *sibling_next
;
323 struct cds_list_head
*sibling_node
;
324 bool wakeup_performed
= 0;
326 if (!(worker
->flags
& URCU_WORKER_STEAL
))
329 /* Only wakeup siblings if we have work in our own queue. */
330 if (cds_wfcq_empty(&worker
->head
, &worker
->tail
))
335 sibling_node
= rcu_dereference(worker
->sibling_node
.next
);
336 if (sibling_node
== &queue
->sibling_head
)
337 sibling_node
= rcu_dereference(sibling_node
->next
);
338 sibling_next
= caa_container_of(sibling_node
, struct urcu_worker
,
340 if (sibling_next
!= worker
)
341 wakeup_performed
= ___urcu_wakeup_sibling(sibling_next
);
342 if (wakeup_performed
)
345 sibling_node
= rcu_dereference(worker
->sibling_node
.prev
);
346 if (sibling_node
== &queue
->sibling_head
)
347 sibling_node
= rcu_dereference(sibling_node
->prev
);
348 sibling_prev
= caa_container_of(sibling_node
, struct urcu_worker
,
350 if (sibling_prev
!= worker
&& sibling_prev
!= sibling_next
)
351 wakeup_performed
= ___urcu_wakeup_sibling(sibling_prev
);
355 return wakeup_performed
;
359 enum urcu_accept_ret
urcu_accept_work(struct urcu_workqueue
*queue
,
360 struct urcu_worker
*worker
)
362 enum cds_wfcq_ret wfcq_ret
;
365 has_work
= ___urcu_grab_work(worker
, &queue
->head
, &queue
->tail
, 0);
366 /* Don't wait if we have work to do. */
367 if (has_work
|| !cds_wfcq_empty(&worker
->head
, &worker
->tail
))
369 /* Try to steal work from sibling instead of blocking */
370 if (__urcu_steal_work(queue
, worker
))
372 /* No more work to do, check shutdown state */
373 if (CMM_LOAD_SHARED(queue
->shutdown
))
374 return URCU_ACCEPT_SHUTDOWN
;
375 urcu_wait_set_state(&worker
->wait_node
,
377 if (!CMM_LOAD_SHARED(worker
->wait_node
.node
.next
)) {
381 * NULL next pointer. We are therefore not in
384 cds_lfs_node_init(&worker
->wait_node
.node
);
385 /* Protect stack dequeue against ABA */
387 was_empty
= !urcu_wait_add(&queue
->waitqueue
,
390 * If the wait queue was empty, it means we are the
391 * first thread to be put back into an otherwise empty
392 * wait queue. Re-check if work queue is empty after
393 * adding ourself to wait queue, so we can wakeup the
394 * top of wait queue since new work have appeared, and
395 * work enqueuer may not have seen that it needed to do
398 if (was_empty
&& !cds_wfcq_empty(&queue
->head
,
400 rcu_read_lock(); /* Protect stack dequeue */
401 (void) urcu_dequeue_wake_single(&queue
->waitqueue
);
402 rcu_read_unlock(); /* Protect stack dequeue */
406 * Non-NULL next pointer. We are therefore in
407 * the queue, or the dispatcher just removed us
408 * from it (after we read the next pointer), and
409 * is therefore awakening us. The state will
410 * therefore have been changed from WAITING to
411 * some other state, which will let the busy
415 urcu_adaptative_busy_wait(&worker
->wait_node
);
420 * We will be busy handling the work batch, awaken siblings so
421 * they can steal from us.
423 (void) __urcu_wakeup_siblings(queue
, worker
);
424 return URCU_ACCEPT_WORK
;
428 struct urcu_work
*urcu_dequeue_work(struct urcu_worker
*worker
)
430 struct cds_wfcq_node
*node
;
433 struct urcu_work
*work
;
435 /* Process our own work entry. */
441 * If we are registered for work stealing, we need to dequeue
442 * safely against siblings.
444 if (worker
->flags
& URCU_WORKER_STEAL
) {
446 * Don't bother grabbing the worker queue lock if it is
449 if (cds_wfcq_empty(&worker
->head
, &worker
->tail
))
451 node
= cds_wfcq_dequeue_blocking(&worker
->head
,
454 node
= ___cds_wfcq_dequeue_with_state(&worker
->head
,
455 &worker
->tail
, NULL
, 1, 0);
459 return caa_container_of(node
, struct urcu_work
, node
);
463 void urcu_workqueue_shutdown(struct urcu_workqueue
*queue
)
466 CMM_STORE_SHARED(queue
->shutdown
, true);
467 /* Wakeup all workers */
468 __urcu_workqueue_wakeup_all(queue
);
471 #endif /* _URCU_WORKQUEUE_FIFO_H */