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/wfstack.h>
28 #include <urcu/waitqueue-lifo.h>
29 #include <urcu/wfcqueue.h>
30 #include <urcu/rculist.h>
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.
41 struct cds_wfcq_node node
;
44 struct urcu_workqueue
{
46 struct __cds_wfcq_head head
;
47 struct cds_wfcq_tail tail
;
49 /* Associated wait queue for LIFO wait/wakeup */
50 struct urcu_wait_queue waitqueue
;
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 */
58 struct cds_wfcq_head head
;
59 struct cds_wfcq_tail tail
;
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 */
67 enum urcu_worker_flags
{
68 URCU_WORKER_STEAL
= (1 << 0),
72 void urcu_workqueue_init(struct urcu_workqueue
*queue
)
74 __cds_wfcq_init(&queue
->head
, &queue
->tail
);
75 urcu_wait_queue_init(&queue
->waitqueue
);
76 CDS_INIT_LIST_HEAD(&queue
->sibling_head
);
80 void urcu_queue_work(struct urcu_workqueue
*queue
, struct urcu_work
*work
)
84 cds_wfcq_node_init(&work
->node
);
87 was_empty
= !cds_wfcq_enqueue(&queue
->head
, &queue
->tail
,
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.
103 (void) urcu_dequeue_wake_single(&queue
->waitqueue
);
107 void urcu_workqueue_wakeup_all(struct urcu_workqueue
*queue
)
109 struct urcu_waiters waiters
;
111 urcu_move_waiters(&waiters
, &queue
->waitqueue
);
112 (void) urcu_wake_all_waiters(&waiters
);
116 void urcu_worker_init(struct urcu_worker
*worker
, int flags
)
118 cds_wfcq_init(&worker
->head
, &worker
->tail
);
119 worker
->flags
= flags
;
120 urcu_wait_node_init(&worker
->wait_node
, URCU_WAIT_RUNNING
);
124 void urcu_worker_register(struct urcu_workqueue
*queue
,
125 struct urcu_worker
*worker
)
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
);
135 void urcu_worker_unregister(struct urcu_workqueue
*queue
,
136 struct urcu_worker
*worker
)
138 enum cds_wfcq_ret wfcq_ret
;
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
);
146 * Wait for grace period before freeing or reusing
147 * "worker" because used by RCU linked list.
153 * Put any local work we still have back into the workqueue.
155 wfcq_ret
= __cds_wfcq_splice_blocking(&queue
->head
,
159 if (wfcq_ret
!= CDS_WFCQ_RET_SRC_EMPTY
160 && wfcq_ret
== CDS_WFCQ_RET_DEST_EMPTY
) {
162 * Wakeup worker thread if we have put work back into
163 * workqueue that was previously empty.
165 (void) urcu_dequeue_wake_single(&queue
->waitqueue
);
170 * Try stealing work from siblings when we have nothing to do.
173 void ___urcu_steal_work(struct urcu_worker
*worker
,
174 struct urcu_worker
*sibling
)
176 cds_wfcq_dequeue_lock(&sibling
->head
, &sibling
->tail
);
177 (void) __cds_wfcq_splice_blocking(&worker
->head
,
181 cds_wfcq_dequeue_unlock(&sibling
->head
, &sibling
->tail
);
185 int __urcu_steal_work(struct urcu_workqueue
*queue
,
186 struct urcu_worker
*worker
)
188 struct urcu_worker
*sibling_prev
, *sibling_next
;
189 struct cds_list_head
*sibling_node
;
191 if (!(worker
->flags
& URCU_WORKER_STEAL
))
196 sibling_node
= rcu_dereference(worker
->sibling_node
.next
);
197 if (sibling_node
== &queue
->sibling_head
)
198 sibling_node
= rcu_dereference(sibling_node
->next
);
199 sibling_next
= caa_container_of(sibling_node
, struct urcu_worker
,
201 if (sibling_next
!= worker
)
202 ___urcu_steal_work(worker
, sibling_next
);
204 sibling_node
= rcu_dereference(worker
->sibling_node
.prev
);
205 if (sibling_node
== &queue
->sibling_head
)
206 sibling_node
= rcu_dereference(sibling_node
->prev
);
207 sibling_prev
= caa_container_of(sibling_node
, struct urcu_worker
,
209 if (sibling_prev
!= worker
&& sibling_prev
!= sibling_next
)
210 ___urcu_steal_work(worker
, sibling_prev
);
214 return !cds_wfcq_empty(&worker
->head
, &worker
->tail
);
218 void ___urcu_wakeup_sibling(struct urcu_worker
*sibling
)
220 urcu_adaptative_wake_up(&sibling
->wait_node
);
224 void __urcu_wakeup_siblings(struct urcu_workqueue
*queue
,
225 struct urcu_worker
*worker
)
227 struct urcu_worker
*sibling_prev
, *sibling_next
;
228 struct cds_list_head
*sibling_node
;
230 if (!(worker
->flags
& URCU_WORKER_STEAL
))
233 /* Only wakeup siblings if we have work in our own queue. */
234 if (cds_wfcq_empty(&worker
->head
, &worker
->tail
))
239 sibling_node
= rcu_dereference(worker
->sibling_node
.next
);
240 if (sibling_node
== &queue
->sibling_head
)
241 sibling_node
= rcu_dereference(sibling_node
->next
);
242 sibling_next
= caa_container_of(sibling_node
, struct urcu_worker
,
244 if (sibling_next
!= worker
)
245 ___urcu_wakeup_sibling(sibling_next
);
247 sibling_node
= rcu_dereference(worker
->sibling_node
.prev
);
248 if (sibling_node
== &queue
->sibling_head
)
249 sibling_node
= rcu_dereference(sibling_node
->prev
);
250 sibling_prev
= caa_container_of(sibling_node
, struct urcu_worker
,
252 if (sibling_prev
!= worker
&& sibling_prev
!= sibling_next
)
253 ___urcu_wakeup_sibling(sibling_prev
);
259 void urcu_accept_work(struct urcu_workqueue
*queue
,
260 struct urcu_worker
*worker
,
263 enum cds_wfcq_ret wfcq_ret
;
265 wfcq_ret
= __cds_wfcq_splice_blocking(&worker
->head
,
269 /* Don't wait if we have work to do. */
270 if (wfcq_ret
!= CDS_WFCQ_RET_SRC_EMPTY
271 || !cds_wfcq_empty(&worker
->head
,
274 /* Try to steal work from sibling instead of blocking */
275 if (__urcu_steal_work(queue
, worker
))
279 urcu_wait_set_state(&worker
->wait_node
,
281 if (!CMM_LOAD_SHARED(worker
->wait_node
.node
.next
)) {
285 * NULL next pointer. We are therefore not in
288 cds_wfs_node_init(&worker
->wait_node
.node
);
289 was_empty
= !urcu_wait_add(&queue
->waitqueue
,
292 * If the wait queue was empty, it means we are the
293 * first thread to be put back into an otherwise empty
294 * wait queue. Re-check if work queue is empty after
295 * adding ourself to wait queue, so we can wakeup the
296 * top of wait queue since new work have appeared, and
297 * work enqueuer may not have seen that it needed to do
300 if (was_empty
&& !cds_wfcq_empty(&queue
->head
,
302 (void) urcu_dequeue_wake_single(&queue
->waitqueue
);
305 * Non-NULL next pointer. We are therefore in
306 * the queue, or the dispatcher just removed us
307 * from it (after we read the next pointer), and
308 * is therefore awakening us. The state will
309 * therefore have been changed from WAITING to
310 * some other state, which will let the busy
314 urcu_adaptative_busy_wait(&worker
->wait_node
);
319 * We will be busy handling the work batch, awaken siblings so
320 * they can steal from us.
322 __urcu_wakeup_siblings(queue
, worker
);
326 struct urcu_work
*urcu_dequeue_work(struct urcu_worker
*worker
)
328 struct cds_wfcq_node
*node
;
331 * If we are registered for work stealing, we need to dequeue
332 * safely against siblings.
334 if (worker
->flags
& URCU_WORKER_STEAL
)
335 node
= cds_wfcq_dequeue_blocking(&worker
->head
,
338 node
= ___cds_wfcq_dequeue_with_state(&worker
->head
,
339 &worker
->tail
, NULL
, 1, 0);
342 return caa_container_of(node
, struct urcu_work
, node
);
345 #endif /* _URCU_WORKQUEUE_FIFO_H */