Debian Codegraph @

src/dex-semaphore.c

raw symbols SCIP
1 /*
2 * dex-semaphore.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but 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 Lesser General Public License
17 * along with this library; 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 <gio/gio.h>
25
26 #include "dex-block-private.h"
27 #include "dex-error.h"
28 #include "dex-future-private.h"
29 #include "dex-object-private.h"
30 #include "dex-promise.h"
31 #include "dex-semaphore-private.h"
32 #include "dex-thread-storage-private.h"
33
34 /*
35 * NOTES:
36 *
37 * This is a userspace-only semaphore implementation. When a post occurs,
38 * we complete waiting futures directly when possible. If a waiter has not yet
39 * been registered we queue it and complete it on the next post.
40 *
41 * We use a DexFuture waiter that is completed when an item is posted.
42 *
43 * In the past, we used io_uring/eventfd on Linux but this turns out to be
44 * faster simply because we do not enter the kernel. In synthetic benchmarks
45 * we spent about 2/3 the time in kernel workers that was not necessary.
46 * Expecially since we need this implementation on non-Linux anyway.
47 */
48
49 #define DEX_TYPE_SEMAPHORE_WAITER dex_semaphore_waiter_type
50
51 typedef struct _DexSemaphoreWaiter
52 {
53 DexFuture parent_instance;
54 GList link;
55 } DexSemaphoreWaiter;
56
57 typedef struct _DexSemaphoreWaiterClass
58 {
59 DexFutureClass parent_class;
60 } DexSemaphoreWaiterClass;
61
62 static GType dex_semaphore_waiter_get_type (void);
63
64 DEX_DEFINE_FINAL_TYPE (DexSemaphoreWaiter, dex_semaphore_waiter, DEX_TYPE_FUTURE)
65
66 static GError semaphore_closed_error;
67 static GValue semaphore_waiter_value;
68
69 static void
70 dex_semaphore_waiter_class_init (DexSemaphoreWaiterClass *semaphore_waiter_class)
71 {
72 g_value_init (&semaphore_waiter_value, G_TYPE_BOOLEAN);
73 g_value_set_boolean (&semaphore_waiter_value, TRUE);
74 semaphore_closed_error = (GError) {
75 .domain = DEX_ERROR,
76 .code = DEX_ERROR_SEMAPHORE_CLOSED,
77 .message = (char *)"Semaphore is closed",
78 };
79 }
80
81 static void
82 dex_semaphore_waiter_init (DexSemaphoreWaiter *semaphore_waiter)
83 {
84 semaphore_waiter->link.data = semaphore_waiter;
85 }
86
87 struct _DexSemaphore
88 {
89 DexObject parent_instance;
90
91 gint64 counter;
92 GQueue waiters;
93 };
94
95 typedef struct _DexSemaphoreClass
96 {
97 DexObjectClass parent_class;
98 } DexSemaphoreClass;
99
100 DEX_DEFINE_FINAL_TYPE (DexSemaphore, dex_semaphore, DEX_TYPE_OBJECT)
101
102 #undef DEX_TYPE_SEMAPHORE
103 #define DEX_TYPE_SEMAPHORE dex_semaphore_type
104
105 DexSemaphore *
106 dex_semaphore_new (void)
107 {
108 return (DexSemaphore *)dex_object_create_instance (DEX_TYPE_SEMAPHORE);
109 }
110
111 static void
112 dex_semaphore_finalize (DexObject *object)
113 {
114 DexSemaphore *semaphore = (DexSemaphore *)object;
115
116 while (semaphore->waiters.length > 0)
117 {
118 DexSemaphoreWaiter *waiter = g_queue_pop_head_link (&semaphore->waiters)->data;
119 dex_future_complete (DEX_FUTURE (waiter),
120 NULL,
121 g_error_copy (&semaphore_closed_error));
122 dex_unref (waiter);
123 }
124 DEX_OBJECT_CLASS (dex_semaphore_parent_class)->finalize (object);
125 }
126
127 static void
128 dex_semaphore_class_init (DexSemaphoreClass *semaphore_class)
129 {
130 DexObjectClass *object_class = DEX_OBJECT_CLASS (semaphore_class);
131
132 object_class->finalize = dex_semaphore_finalize;
133
134 g_type_ensure (dex_semaphore_waiter_get_type ());
135 }
136
137 static void
138 dex_semaphore_init (DexSemaphore *semaphore)
139 {
140 }
141
142 void
143 dex_semaphore_post (DexSemaphore *semaphore)
144 {
145 dex_semaphore_post_many (semaphore, 1);
146 }
147
148 void
149 dex_semaphore_post_many (DexSemaphore *semaphore,
150 guint count)
151 {
152 GQueue queue;
153
154 g_return_if_fail (DEX_IS_SEMAPHORE (semaphore));
155
156 if (count == 0)
157 return;
158
159 queue = (GQueue) G_QUEUE_INIT;
160
161 /* Post count and steal as many workers as we can complete
162 * immediately which are waiting on a result.
163 */
164 dex_object_lock (semaphore);
165 semaphore->counter += count;
166 while (semaphore->counter > 0 && semaphore->waiters.length > 0)
167 {
168 g_queue_push_tail_link (&queue, g_queue_pop_head_link (&semaphore->waiters));
169 semaphore->counter--;
170 }
171 dex_object_unlock (semaphore);
172
173 /* Now complete the waiters outside our object lock */
174 while (queue.length > 0)
175 {
176 DexSemaphoreWaiter *waiter = g_queue_pop_head_link (&queue)->data;
177 dex_future_complete (DEX_FUTURE (waiter), &semaphore_waiter_value, NULL);
178 dex_unref (waiter);
179 }
180 }
181
182 static DexFuture *
183 dex_semaphore_wait_on_scheduler (DexFuture *future,
184 gpointer user_data)
185 {
186 return dex_ref (future);
187 }
188
189 DexFuture *
190 dex_semaphore_wait (DexSemaphore *semaphore)
191 {
192 DexFuture *ret = NULL;
193 DexSemaphoreWaiter *waiter;
194 DexScheduler *scheduler;
195 DexFuture *block;
196
197 g_return_val_if_fail (DEX_IS_SEMAPHORE (semaphore), NULL);
198
199 waiter = (DexSemaphoreWaiter *)
200 dex_object_create_instance (DEX_TYPE_SEMAPHORE_WAITER);
201
202 dex_object_lock (semaphore);
203 if (semaphore->counter > 0)
204 {
205 semaphore->counter--;
206 dex_future_complete (DEX_FUTURE (waiter), &semaphore_waiter_value, NULL);
207 ret = DEX_FUTURE (g_steal_pointer (&waiter));
208 }
209 else
210 {
211 scheduler = dex_scheduler_ref_thread_default ();
212
213 g_assert (scheduler != NULL);
214 g_assert (DEX_IS_SCHEDULER (scheduler));
215
216 block = dex_block_new (dex_ref (waiter),
217 scheduler,
218 DEX_BLOCK_KIND_FINALLY,
219 dex_semaphore_wait_on_scheduler, NULL, NULL);
220 g_queue_push_tail_link (&semaphore->waiters, &waiter->link);
221 ret = DEX_FUTURE (g_steal_pointer (&block));
222
223 dex_unref (scheduler);
224 }
225 dex_object_unlock (semaphore);
226
227 g_assert (ret != NULL);
228 g_assert (DEX_IS_FUTURE (ret));
229
230 return ret;
231 }
232
233 void
234 dex_semaphore_close (DexSemaphore *semaphore)
235 {
236 g_return_if_fail (DEX_IS_SEMAPHORE (semaphore));
237
238 dex_object_lock (semaphore);
239
240 if (semaphore->waiters.length > 0)
241 {
242 GQueue queue = semaphore->waiters;
243 semaphore->waiters = (GQueue) {NULL, NULL, 0};
244
245 while (queue.length)
246 {
247 DexSemaphoreWaiter *waiter = g_queue_pop_head_link (&queue)->data;
248 dex_future_complete (DEX_FUTURE (waiter),
249 NULL,
250 g_error_copy (&semaphore_closed_error));
251 dex_unref (waiter);
252 }
253 }
254
255 dex_object_unlock (semaphore);
256 }