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