Debian Codegraph @

src/dex-work-stealing-queue-private.h

raw symbols SCIP
1 /*
2 * dex-work-stealing-queue-private.h
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 /* This code is heavily based upon wsq.hpp¹ from Tsung-Wei Huang, under
23 * the MIT license. Its original license is provided below.
24 *
25 * ¹ https://github.com/taskflow/work-stealing-queue/tree/master/wsq.hpp
26 *
27 * MIT License
28 *
29 * Copyright (c) 2020 T.-W. Huang
30 *
31 * University of Utah, Salt Lake City, UT, USA
32 *
33 * Permission is hereby granted, free of charge, to any person obtaining a copy
34 * of this software and associated documentation files (the "Software"), to deal
35 * in the Software without restriction, including without limitation the rights
36 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37 * copies of the Software, and to permit persons to whom the Software is
38 * furnished to do so, subject to the following conditions:
39 *
40 * The above copyright notice and this permission notice shall be included in all
41 * copies or substantial portions of the Software.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
49 * SOFTWARE.
50 */
51
52 #pragma once
53
54 #include <stdatomic.h>
55
56 #include <glib.h>
57
58 #include "dex-scheduler-private.h"
59
60 /**
61 * SECTION:dex-work-stealing-queue
62 * @title: DexWorkStealinQueue
63 * @short_description: Lock-free unbounded single-producer multiple consumer queue
64 *
65 * This class implements the work stealing queue described in the paper,
66 * "Correct and Efficient Work-Stealing for Weak Memory Models," available at
67 * https://www.di.ens.fr/~zappa/readings/ppopp13.pdf
68 *
69 * Only the queue owner can perform pop and push operations, while others can
70 * steal data from the queue.
71 */
72
73 G_BEGIN_DECLS
74
75 #ifndef DEX_CACHELINE_SIZE
76 # define DEX_CACHELINE_SIZE 64
77 #endif
78
79 typedef struct _DexWorkStealingArray
80 {
81 gint64 C;
82 gint64 M;
83 _Atomic(DexWorkItem) S[];
84 } DexWorkStealingArray;
85
86 typedef struct _DexWorkStealingQueue
87 {
88 _Alignas(DEX_CACHELINE_SIZE) _Atomic(gint64) top;
89 _Alignas(DEX_CACHELINE_SIZE) _Atomic(gint64) bottom;
90 _Alignas(DEX_CACHELINE_SIZE) _Atomic(DexWorkStealingArray*) array;
91 GPtrArray *garbage;
92 gatomicrefcount ref_count;
93 } DexWorkStealingQueue;
94
95 DexWorkStealingQueue *dex_work_stealing_queue_new (gint64 capacity);
96 DexWorkStealingQueue *dex_work_stealing_queue_ref (DexWorkStealingQueue *work_stealing_queue);
97 void dex_work_stealing_queue_unref (DexWorkStealingQueue *work_stealing_queue);
98 GSource *dex_work_stealing_queue_create_source (DexWorkStealingQueue *work_stealing_queue);
99
100 static inline DexWorkStealingArray *
101 dex_work_stealing_array_new (gint64 c)
102 {
103 DexWorkStealingArray *work_stealing_array;
104
105 work_stealing_array = g_aligned_alloc0 (1,
106 sizeof (DexWorkStealingArray) + (c * sizeof (DexWorkItem)),
107 G_ALIGNOF (DexWorkStealingArray));
108 work_stealing_array->C = c;
109 work_stealing_array->M = c-1;
110
111 return work_stealing_array;
112 }
113
114 static inline gint64
115 dex_work_stealing_array_capacity (DexWorkStealingArray *work_stealing_array)
116 {
117 return work_stealing_array->C;
118 }
119
120 static inline void
121 dex_work_stealing_array_push (DexWorkStealingArray *work_stealing_array,
122 gint64 i,
123 DexWorkItem work_item)
124 {
125 atomic_store_explicit (&work_stealing_array->S[i & work_stealing_array->M],
126 work_item,
127 memory_order_relaxed);
128 }
129
130 static inline DexWorkItem
131 dex_work_stealing_array_pop (DexWorkStealingArray *work_stealing_array,
132 gint64 i)
133 {
134 return atomic_load_explicit (&work_stealing_array->S[i & work_stealing_array->M],
135 memory_order_relaxed);
136 }
137
138 static inline DexWorkStealingArray *
139 dex_work_stealing_array_resize (DexWorkStealingArray *work_stealing_array,
140 gint64 b,
141 gint64 t)
142 {
143 DexWorkStealingArray *ptr = dex_work_stealing_array_new (work_stealing_array->C * 2);
144 for (gint64 i = t; i != b; i++)
145 dex_work_stealing_array_push (ptr, i, dex_work_stealing_array_pop (work_stealing_array, i));
146 return ptr;
147 }
148
149 static inline void
150 dex_work_stealing_array_free (DexWorkStealingArray *work_stealing_array)
151 {
152 g_aligned_free (work_stealing_array);
153 }
154
155 static inline gboolean
156 dex_work_stealing_queue_empty (DexWorkStealingQueue *work_stealing_queue)
157 {
158 gint64 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed);
159 gint64 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_relaxed);
160 return b <= t;
161 }
162
163 static inline gsize
164 dex_work_stealing_queue_size (DexWorkStealingQueue *work_stealing_queue)
165 {
166 gint64 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed);
167 gint64 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_relaxed);
168 return b >= t ? b - t : 0;
169 }
170
171 /**
172 * dex_work_stealing_queue_push:
173 * @work_stealing_queue: a #DexWorkStealingQueue
174 * @work_item: the work item to push
175 *
176 * This adds @work_item to the queue so that it can be processed by the
177 * local worker, or optionally, stolen by another worker after there are
178 * no more items to process in its queue or the global queue.
179 *
180 * This may _ONLY_ be called by the thread that owns @work_stealing_queue.
181 *
182 * Work items that originate outside of a worker thread should be pushed into
183 * a global queue shared among workers.
184 */
185 static inline void
186 dex_work_stealing_queue_push (DexWorkStealingQueue *work_stealing_queue,
187 DexWorkItem work_item)
188 {
189 gint64 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed);
190 gint64 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_acquire);
191 DexWorkStealingArray *a = atomic_load_explicit (&work_stealing_queue->array, memory_order_relaxed);
192
193 /* queue is full */
194 if (dex_work_stealing_array_capacity (a) - 1 < (b - t))
195 {
196 DexWorkStealingArray *tmp = dex_work_stealing_array_resize (a, b, t);
197 g_ptr_array_add (work_stealing_queue->garbage, a);
198 a = tmp;
199 atomic_store_explicit (&work_stealing_queue->array, a, memory_order_relaxed);
200 }
201
202 dex_work_stealing_array_push (a, b, work_item);
203 atomic_thread_fence (memory_order_release);
204 atomic_store_explicit (&work_stealing_queue->bottom, b + 1, memory_order_relaxed);
205 }
206
207 /**
208 * dex_work_stealing_queue_pop:
209 * @work_stealing_queue: a #DexWorkStealingQueue
210 * @out_work_item: (out): a location for a #DexWorkItem
211 *
212 * Pops the next work item from the queue without synchronization.
213 *
214 * This may _ONLY_ be called by the thread that owns @work_stealing_queue.
215 *
216 * Returns: %TRUE if @out_work_item was set, otherwise %FALSE and the queue
217 * is currently empty.
218 */
219 static inline gboolean
220 dex_work_stealing_queue_pop (DexWorkStealingQueue *work_stealing_queue,
221 DexWorkItem *out_work_item)
222 {
223 DexWorkStealingArray *a;
224 gboolean ret;
225 gint64 b;
226 gint64 t;
227
228 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed) - 1;
229 a = atomic_load_explicit (&work_stealing_queue->array, memory_order_relaxed);
230 atomic_store_explicit (&work_stealing_queue->bottom, b, memory_order_relaxed);
231 atomic_thread_fence (memory_order_seq_cst);
232 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_relaxed);
233
234 ret = t <= b;
235
236 if (ret)
237 {
238 *out_work_item = dex_work_stealing_array_pop (a, b);
239
240 if (t == b)
241 {
242 /* the last item just got stolen */
243 if (!atomic_compare_exchange_strong_explicit (&work_stealing_queue->top,
244 &t,
245 t + 1,
246 memory_order_seq_cst,
247 memory_order_relaxed))
248 ret = FALSE;
249
250 atomic_store_explicit (&work_stealing_queue->bottom, b + 1, memory_order_relaxed);
251 }
252 }
253 else
254 {
255 atomic_store_explicit (&work_stealing_queue->bottom, b + 1, memory_order_relaxed);
256 }
257
258 return ret;
259 }
260
261 /**
262 * dex_work_stealing_queue_steal:
263 * @work_stealing_queue: a #DexWorkStealingQueue
264 * @out_work_item: (out): a location to store the work item
265 *
266 * Attempts to steal a #DexWorkItem from @work_stealing_queue with lock-free
267 * synchronization among workers.
268 *
269 * This function is _ONLY_ be called by threads other than the thread owning
270 * @work_stealing_queue.
271 *
272 * Returns: %TRUE if @out_work_item is set; otherwise %FALSE
273 */
274 static inline gboolean
275 dex_work_stealing_queue_steal (DexWorkStealingQueue *work_stealing_queue,
276 DexWorkItem *out_work_item)
277 {
278 gint64 t;
279 gint64 b;
280
281 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_acquire);
282 atomic_thread_fence (memory_order_seq_cst);
283 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_acquire);
284
285 if (t < b)
286 {
287 DexWorkStealingArray *a = atomic_load_explicit (&work_stealing_queue->array, memory_order_consume);
288
289 *out_work_item = dex_work_stealing_array_pop (a, t);
290
291 if (atomic_compare_exchange_strong_explicit (&work_stealing_queue->top,
292 &t,
293 t + 1,
294 memory_order_seq_cst,
295 memory_order_relaxed))
296 return TRUE;
297 }
298
299 return FALSE;
300 }
301
302 static inline gint64
303 dex_work_stealing_queue_capacity (DexWorkStealingQueue *work_stealing_queue)
304 {
305 return dex_work_stealing_array_capacity (
306 atomic_load_explicit (&work_stealing_queue->array, memory_order_relaxed));
307 }
308
309 G_END_DECLS