65d3a2e222cc0fbe991c986bce41f4e35722b309
[userspace-rcu.git] / urcu / workqueue-fifo.h
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 cds_wfcq_dequeue_lock(&sibling->head, &sibling->tail);
177 (void) __cds_wfcq_splice_blocking(&worker->head,
178 &worker->tail,
179 &sibling->head,
180 &sibling->tail);
181 cds_wfcq_dequeue_unlock(&sibling->head, &sibling->tail);
182 }
183
184 static inline
185 int __urcu_steal_work(struct urcu_workqueue *queue,
186 struct urcu_worker *worker)
187 {
188 struct urcu_worker *sibling_prev, *sibling_next;
189 struct cds_list_head *sibling_node;
190
191 if (!(worker->flags & URCU_WORKER_STEAL))
192 return 0;
193
194 rcu_read_lock();
195
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,
200 sibling_node);
201 if (sibling_next != worker)
202 ___urcu_steal_work(worker, sibling_next);
203
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,
208 sibling_node);
209 if (sibling_prev != worker && sibling_prev != sibling_next)
210 ___urcu_steal_work(worker, sibling_prev);
211
212 rcu_read_unlock();
213
214 return !cds_wfcq_empty(&worker->head, &worker->tail);
215 }
216
217 static inline
218 void ___urcu_wakeup_sibling(struct urcu_worker *sibling)
219 {
220 urcu_adaptative_wake_up(&sibling->wait_node);
221 }
222
223 static inline
224 void __urcu_wakeup_siblings(struct urcu_workqueue *queue,
225 struct urcu_worker *worker)
226 {
227 struct urcu_worker *sibling_prev, *sibling_next;
228 struct cds_list_head *sibling_node;
229
230 if (!(worker->flags & URCU_WORKER_STEAL))
231 return;
232
233 /* Only wakeup siblings if we have work in our own queue. */
234 if (cds_wfcq_empty(&worker->head, &worker->tail))
235 return;
236
237 rcu_read_lock();
238
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,
243 sibling_node);
244 if (sibling_next != worker)
245 ___urcu_wakeup_sibling(sibling_next);
246
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,
251 sibling_node);
252 if (sibling_prev != worker && sibling_prev != sibling_next)
253 ___urcu_wakeup_sibling(sibling_prev);
254
255 rcu_read_unlock();
256 }
257
258 static inline
259 void urcu_accept_work(struct urcu_workqueue *queue,
260 struct urcu_worker *worker,
261 int blocking)
262 {
263 enum cds_wfcq_ret wfcq_ret;
264
265 wfcq_ret = __cds_wfcq_splice_blocking(&worker->head,
266 &worker->tail,
267 &queue->head,
268 &queue->tail);
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,
272 &worker->tail))
273 goto do_work;
274 /* Try to steal work from sibling instead of blocking */
275 if (__urcu_steal_work(queue, worker))
276 goto do_work;
277 if (!blocking)
278 return;
279 urcu_wait_set_state(&worker->wait_node,
280 URCU_WAIT_WAITING);
281 if (!CMM_LOAD_SHARED(worker->wait_node.node.next)) {
282 int was_empty;
283
284 /*
285 * NULL next pointer. We are therefore not in
286 * the queue.
287 */
288 cds_wfs_node_init(&worker->wait_node.node);
289 was_empty = !urcu_wait_add(&queue->waitqueue,
290 &worker->wait_node);
291 /*
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
298 * a wake up.
299 */
300 if (was_empty && !cds_wfcq_empty(&queue->head,
301 &queue->tail))
302 (void) urcu_dequeue_wake_single(&queue->waitqueue);
303 } else {
304 /*
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
311 * wait pass through.
312 */
313 }
314 urcu_adaptative_busy_wait(&worker->wait_node);
315 return;
316
317 do_work:
318 /*
319 * We will be busy handling the work batch, awaken siblings so
320 * they can steal from us.
321 */
322 __urcu_wakeup_siblings(queue, worker);
323 }
324
325 static inline
326 struct urcu_work *urcu_dequeue_work(struct urcu_worker *worker)
327 {
328 struct cds_wfcq_node *node;
329
330 /*
331 * If we are registered for work stealing, we need to dequeue
332 * safely against siblings.
333 */
334 if (worker->flags & URCU_WORKER_STEAL)
335 node = cds_wfcq_dequeue_blocking(&worker->head,
336 &worker->tail);
337 else
338 node = ___cds_wfcq_dequeue_with_state(&worker->head,
339 &worker->tail, NULL, 1, 0);
340 if (!node)
341 return NULL;
342 return caa_container_of(node, struct urcu_work, node);
343 }
344
345 #endif /* _URCU_WORKQUEUE_FIFO_H */
This page took 0.03546 seconds and 3 git commands to generate.