Debian Codegraph @

src/dex-work-stealing-queue.c

raw symbols SCIP
1 /*
2 * dex-work-stealing-queue.c
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 #include "config.h"
23
24 #include "dex-compat-private.h"
25 #include "dex-work-stealing-queue-private.h"
26
27 #define DEFAULT_BATCH_SIZE 32
28
29 DexWorkStealingQueue *
30 dex_work_stealing_queue_new (gint64 capacity)
31 {
32 DexWorkStealingQueue *work_stealing_queue;
33
34 g_return_val_if_fail (capacity > 0, NULL);
35 g_return_val_if_fail ((capacity & (capacity - 1)) == 0, NULL);
36
37 work_stealing_queue = g_aligned_alloc0 (1,
38 sizeof (DexWorkStealingQueue),
39 G_ALIGNOF (DexWorkStealingQueue));
40 atomic_store_explicit (&work_stealing_queue->top, 0, memory_order_relaxed);
41 atomic_store_explicit (&work_stealing_queue->bottom, 0, memory_order_relaxed);
42 atomic_store_explicit (&work_stealing_queue->array,
43 dex_work_stealing_array_new (capacity),
44 memory_order_relaxed);
45 work_stealing_queue->garbage = g_ptr_array_new_full (32, (GDestroyNotify)dex_work_stealing_array_free);
46 g_atomic_ref_count_init (&work_stealing_queue->ref_count);
47
48 return work_stealing_queue;
49 }
50
51 DexWorkStealingQueue *
52 dex_work_stealing_queue_ref (DexWorkStealingQueue *work_stealing_queue)
53 {
54 g_atomic_ref_count_inc (&work_stealing_queue->ref_count);
55 return work_stealing_queue;
56 }
57
58 void
59 dex_work_stealing_queue_unref (DexWorkStealingQueue *work_stealing_queue)
60 {
61 if (g_atomic_ref_count_dec (&work_stealing_queue->ref_count))
62 {
63 GPtrArray *garbage = work_stealing_queue->garbage;
64 DexWorkStealingArray *array = atomic_load (&work_stealing_queue->array);
65
66 work_stealing_queue->top = 0;
67 work_stealing_queue->bottom = 0;
68 work_stealing_queue->array = NULL;
69 work_stealing_queue->garbage = NULL;
70
71 g_clear_pointer (&garbage, g_ptr_array_unref);
72 g_clear_pointer (&array, dex_work_stealing_array_free);
73
74 g_aligned_free (work_stealing_queue);
75 }
76 }
77
78 typedef struct _DexWorkStealingQueueSource
79 {
80 GSource parent_instance;
81 DexWorkStealingQueue *work_stealing_queue;
82 guint batch_size;
83 } DexWorkStealingQueueSource;
84
85 static gboolean
86 dex_work_stealing_queue_source_prepare (GSource *source,
87 int *timeout)
88 {
89 DexWorkStealingQueueSource *real_source = (DexWorkStealingQueueSource *)source;
90
91 *timeout = -1;
92
93 return !dex_work_stealing_queue_empty (real_source->work_stealing_queue);
94 }
95
96 static gboolean
97 dex_work_stealing_queue_source_check (GSource *source)
98 {
99 DexWorkStealingQueueSource *real_source = (DexWorkStealingQueueSource *)source;
100
101 return !dex_work_stealing_queue_empty (real_source->work_stealing_queue);
102 }
103
104 static gboolean
105 dex_work_stealing_queue_source_dispatch (GSource *source,
106 GSourceFunc callback,
107 gpointer callback_data)
108 {
109 DexWorkStealingQueueSource *real_source = (DexWorkStealingQueueSource *)source;
110 DexWorkStealingQueue *work_stealing_queue = real_source->work_stealing_queue;
111
112 for (guint i = 0; i < real_source->batch_size; i++)
113 {
114 DexWorkItem work_item;
115
116 if (!dex_work_stealing_queue_pop (work_stealing_queue, &work_item))
117 break;
118
119 dex_work_item_invoke (&work_item);
120 }
121
122 return G_SOURCE_CONTINUE;
123 }
124
125 static GSourceFuncs dex_work_stealing_queue_source_funcs = {
126 .prepare = dex_work_stealing_queue_source_prepare,
127 .check = dex_work_stealing_queue_source_check,
128 .dispatch = dex_work_stealing_queue_source_dispatch,
129 };
130
131 GSource *
132 dex_work_stealing_queue_create_source (DexWorkStealingQueue *work_stealing_queue)
133 {
134 DexWorkStealingQueueSource *real_source;
135 GSource *source;
136
137 g_return_val_if_fail (work_stealing_queue != NULL, NULL);
138
139 source = g_source_new (&dex_work_stealing_queue_source_funcs, sizeof (DexWorkStealingQueueSource));
140 real_source = (DexWorkStealingQueueSource *)source;
141
142 _g_source_set_static_name (source, "[dex-work-stealing-queue]");
143 real_source->work_stealing_queue = dex_work_stealing_queue_ref (work_stealing_queue);
144 real_source->batch_size = DEFAULT_BATCH_SIZE;
145
146 return source;
147 }