Debian Codegraph @

src/dex-coroutine.c

raw symbols SCIP
1 /*
2 * dex-coroutine.c
3 *
4 * Copyright 2026 Christian Hergert
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
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21
22 #include "dex-coroutine.h"
23 #include "dex-coroutine-private.h"
24 #include "dex-error.h"
25 #include "dex-future-private.h"
26 #include "dex-compat-private.h"
27 #include "dex-thread-storage-private.h"
28
29 /**
30 * DexCoroutine:
31 *
32 * `DexCoroutine` is a [[email protected]] implemented as a stateful stackless
33 * coroutine cooperatively scheduled on a [[email protected]].
34 *
35 * Each coroutine stores stackful suspension state and optionally accepts user
36 * data via `user_data`.
37 *
38 * Use user data passed at spawn time for stateful data.
39 */
40
41 typedef enum _DexCoroutineQueue
42 {
43 CORO_QUEUE_NONE,
44 CORO_QUEUE_RUNNABLE,
45 CORO_QUEUE_BLOCKED,
46 CORO_QUEUE_LAST,
47 } DexCoroutineQueue;
48
49 typedef struct _DexCoroutineClass
50 {
51 DexFutureClass parent_class;
52 } DexCoroutineClass;
53
54 typedef struct _DexCoroutineScheduler
55 {
56 GSource source;
57 GMutex mutex;
58 DexCoroutine *running;
59 GQueue runnable;
60 GQueue blocked;
61 } DexCoroutineScheduler;
62
63 struct _DexCoroutineContext
64 {
65 DexCoroutine *coroutine;
66 guint pc;
67 DexFuture *pending;
68 };
69
70 struct _DexCoroutine
71 {
72 DexFuture parent_instance;
73 GList link;
74 DexCoroutineFunc func;
75 DexCoroutineContext context;
76 gpointer user_data;
77 GDestroyNotify user_data_destroy;
78 DexCoroutineScheduler *coroutine_scheduler;
79 DexCoroutineQueue queue : 2;
80 guint running : 1;
81 guint runnable : 1;
82 guint cancelled : 1;
83 guint exited : 1;
84 guint released : 1;
85 guint awaiting_final : 1;
86 };
87
88 static void dex_coroutine_resume (DexCoroutine *coroutine);
89 static void dex_coroutine_discard (DexFuture *future);
90 static gboolean dex_coroutine_propagate (DexFuture *future,
91 DexFuture *completed);
92 static void dex_coroutine_complete_cancelled (DexCoroutine *coroutine);
93 static void dex_coroutine_complete_error (DexCoroutine *coroutine,
94 GError *error);
95 static void dex_coroutine_complete_from_future (DexCoroutine *coroutine,
96 DexFuture *future);
97 static void dex_coroutine_wait_for (DexCoroutine *coroutine,
98 DexFuture *future,
99 gboolean awaiting_final);
100 static void dex_coroutine_clear_user_data (DexCoroutine *coroutine);
101 static void dex_coroutine_clear_pending (DexCoroutine *coroutine);
102 static void dex_coroutine_set_queue (DexCoroutineScheduler *scheduler,
103 DexCoroutine *coroutine,
104 DexCoroutineQueue queue);
105 static void dex_coroutine_detach_from_scheduler (DexCoroutine *coroutine);
106
107 DEX_DEFINE_FINAL_TYPE (DexCoroutine, dex_coroutine, DEX_TYPE_FUTURE)
108
109 #undef DEX_TYPE_COROUTINE
110 #define DEX_TYPE_COROUTINE dex_coroutine_type
111
112 static void
113 dex_coroutine_init (DexCoroutine *coroutine)
114 {
115 coroutine->context.coroutine = coroutine;
116 coroutine->link.data = coroutine;
117 }
118
119 /**
120 * dex_coroutine_suspend: (skip)
121 * @context: the coroutine context
122 * @pc: the state machine PC
123 * @future: (transfer full): the future to suspend for
124 *
125 * Since: 1.2
126 */
127 void
128 dex_coroutine_context_suspend (DexCoroutineContext *context,
129 guint pc,
130 DexFuture *future)
131 {
132 DexCoroutine *coroutine;
133
134 g_return_if_fail (context != NULL);
135 g_return_if_fail (future != NULL);
136
137 coroutine = context->coroutine;
138
139 g_return_if_fail (coroutine != NULL);
140 g_return_if_fail (DEX_IS_COROUTINE (coroutine));
141
142 g_clear_pointer (&context->pending, dex_unref);
143 context->pc = pc;
144
145 g_assert (future != DEX_FUTURE (coroutine));
146
147 context->pending = g_steal_pointer (&future);
148 }
149
150 /**
151 * dex_coroutine_resume: (skip)
152 * @context: the coroutine context
153 * @pc: (out): the state machine PC
154 * @future: (out) (transfer full): the future which resumed
155 *
156 * Since: 1.2
157 */
158 void
159 dex_coroutine_context_resume (DexCoroutineContext *context,
160 guint *pc,
161 DexFuture **future)
162 {
163 DexCoroutine *coroutine;
164
165 g_return_if_fail (context != NULL);
166 g_return_if_fail (pc != NULL);
167 g_return_if_fail (future != NULL);
168
169 coroutine = context->coroutine;
170 g_return_if_fail (coroutine != NULL);
171 g_return_if_fail (DEX_IS_COROUTINE (coroutine));
172
173 *pc = context->pc;
174 *future = g_steal_pointer (&context->pending);
175 }
176
177 static void
178 dex_coroutine_finalize (DexObject *object)
179 {
180 DexCoroutine *coroutine = DEX_COROUTINE (object);
181
182 g_assert (coroutine->link.prev == NULL);
183 g_assert (coroutine->link.next == NULL);
184 g_assert (coroutine->queue == CORO_QUEUE_NONE);
185 g_assert (coroutine->coroutine_scheduler == NULL);
186 g_assert (coroutine->link.data == coroutine);
187
188 dex_coroutine_clear_user_data (coroutine);
189 dex_coroutine_clear_pending (coroutine);
190
191 DEX_OBJECT_CLASS (dex_coroutine_parent_class)->finalize (object);
192 }
193
194 static void
195 dex_coroutine_class_init (DexCoroutineClass *coroutine_class)
196 {
197 DexObjectClass *object_class = DEX_OBJECT_CLASS (coroutine_class);
198 DexFutureClass *future_class = DEX_FUTURE_CLASS (coroutine_class);
199
200 object_class->finalize = dex_coroutine_finalize;
201 future_class->discard = dex_coroutine_discard;
202 future_class->propagate = dex_coroutine_propagate;
203 }
204
205 static void
206 dex_coroutine_clear_user_data (DexCoroutine *coroutine)
207 {
208 GDestroyNotify user_data_destroy;
209 gpointer user_data;
210
211 if (coroutine->user_data_destroy == NULL)
212 return;
213
214 user_data_destroy = coroutine->user_data_destroy;
215 user_data = g_steal_pointer (&coroutine->user_data);
216
217 coroutine->user_data_destroy = NULL;
218 coroutine->user_data = NULL;
219 user_data_destroy (user_data);
220 }
221
222 static void
223 dex_coroutine_set_queue (DexCoroutineScheduler *scheduler,
224 DexCoroutine *coroutine,
225 DexCoroutineQueue queue)
226 {
227 g_assert (scheduler != NULL);
228 g_assert (coroutine != NULL);
229 g_assert (queue < CORO_QUEUE_LAST);
230
231 if (coroutine->queue == queue)
232 return;
233
234 if (coroutine->queue == CORO_QUEUE_RUNNABLE)
235 g_queue_unlink (&scheduler->runnable, &coroutine->link);
236 else if (coroutine->queue == CORO_QUEUE_BLOCKED)
237 g_queue_unlink (&scheduler->blocked, &coroutine->link);
238
239 coroutine->queue = queue;
240
241 if (queue == CORO_QUEUE_RUNNABLE)
242 {
243 coroutine->runnable = TRUE;
244 g_queue_push_tail_link (&scheduler->runnable, &coroutine->link);
245 }
246 else if (queue == CORO_QUEUE_BLOCKED)
247 {
248 coroutine->runnable = FALSE;
249 g_queue_push_tail_link (&scheduler->blocked, &coroutine->link);
250 }
251 else
252 {
253 coroutine->runnable = FALSE;
254 coroutine->link.prev = NULL;
255 coroutine->link.next = NULL;
256 coroutine->link.data = coroutine;
257 }
258 }
259
260 static void
261 dex_coroutine_detach_from_scheduler (DexCoroutine *coroutine)
262 {
263 DexCoroutineScheduler *scheduler = coroutine->coroutine_scheduler;
264
265 if (scheduler == NULL)
266 return;
267
268 g_mutex_lock (&scheduler->mutex);
269 if (coroutine->queue == CORO_QUEUE_RUNNABLE ||
270 coroutine->queue == CORO_QUEUE_BLOCKED)
271 dex_coroutine_set_queue (scheduler, coroutine, CORO_QUEUE_NONE);
272 coroutine->coroutine_scheduler = NULL;
273 g_mutex_unlock (&scheduler->mutex);
274 }
275
276 static void
277 dex_coroutine_clear_pending (DexCoroutine *coroutine)
278 {
279 g_clear_pointer (&coroutine->context.pending, dex_unref);
280 }
281
282 static void
283 dex_coroutine_complete_error (DexCoroutine *coroutine,
284 GError *error)
285 {
286 g_return_if_fail (coroutine != NULL);
287
288 coroutine->awaiting_final = FALSE;
289 coroutine->exited = TRUE;
290
291 dex_coroutine_clear_user_data (coroutine);
292 dex_coroutine_clear_pending (coroutine);
293 dex_coroutine_detach_from_scheduler (coroutine);
294
295 if (error == NULL)
296 error = g_error_new_literal (DEX_ERROR,
297 DEX_ERROR_FIBER_EXITED,
298 "Coroutine exited without a result");
299
300 dex_future_complete (DEX_FUTURE (coroutine), NULL, error);
301 }
302
303 static void
304 dex_coroutine_complete_from_future (DexCoroutine *coroutine,
305 DexFuture *future)
306 {
307 g_assert (DEX_IS_COROUTINE (coroutine));
308 g_assert (DEX_IS_FUTURE (future));
309
310 coroutine->awaiting_final = FALSE;
311 coroutine->exited = TRUE;
312
313 dex_coroutine_clear_user_data (coroutine);
314 dex_coroutine_clear_pending (coroutine);
315 dex_coroutine_detach_from_scheduler (coroutine);
316
317 dex_future_complete_from (DEX_FUTURE (coroutine), future);
318 }
319
320 static void
321 dex_coroutine_complete_cancelled (DexCoroutine *coroutine)
322 {
323 g_assert (coroutine != NULL);
324
325 dex_coroutine_complete_error (coroutine,
326 g_error_new_literal (DEX_ERROR,
327 DEX_ERROR_FIBER_CANCELLED,
328 "Coroutine was cancelled"));
329 }
330
331 static void
332 dex_coroutine_wait_for (DexCoroutine *coroutine,
333 DexFuture *future,
334 gboolean awaiting_final)
335 {
336 DexCoroutineScheduler *scheduler;
337
338 g_assert (coroutine != NULL);
339 g_assert (DEX_IS_COROUTINE (coroutine));
340 g_assert (future != NULL);
341 g_assert (!coroutine->exited);
342 g_assert (!coroutine->cancelled);
343
344 dex_clear (&coroutine->context.pending);
345 coroutine->context.pending = g_steal_pointer (&future);
346
347 coroutine->awaiting_final = awaiting_final;
348
349 if ((scheduler = coroutine->coroutine_scheduler) != NULL)
350 {
351 GSource *source;
352 DexThreadStorage *thread_storage;
353
354 g_mutex_lock (&scheduler->mutex);
355 dex_coroutine_set_queue (scheduler, coroutine, CORO_QUEUE_BLOCKED);
356 thread_storage = dex_thread_storage_get ();
357 source = thread_storage->coroutine_scheduler == scheduler
358 ? NULL
359 : g_source_ref ((GSource *)scheduler);
360 g_mutex_unlock (&scheduler->mutex);
361
362 if (source != NULL)
363 {
364 g_main_context_wakeup (g_source_get_context (source));
365 g_source_unref (source);
366 }
367 }
368
369 dex_future_chain (coroutine->context.pending, DEX_FUTURE (coroutine));
370 }
371
372 static void
373 dex_coroutine_resume (DexCoroutine *coroutine)
374 {
375 DexFuture *future;
376
377 g_assert (coroutine != NULL);
378
379 if (coroutine->cancelled)
380 {
381 dex_coroutine_complete_cancelled (coroutine);
382 return;
383 }
384
385 g_assert (!coroutine->context.pending || coroutine->context.pc > 0);
386
387 if ((future = coroutine->func (&coroutine->context, coroutine->user_data)))
388 {
389 if (dex_future_is_pending (future))
390 {
391 if (coroutine->cancelled)
392 {
393 dex_unref (future);
394 dex_coroutine_complete_cancelled (coroutine);
395 }
396 else
397 {
398 dex_coroutine_wait_for (coroutine, g_steal_pointer (&future), TRUE);
399 }
400 }
401 else
402 {
403 DexFuture *completed = g_steal_pointer (&future);
404 dex_coroutine_complete_from_future (coroutine, completed);
405 dex_unref (completed);
406 }
407
408 return;
409 }
410
411 if (coroutine->context.pending == NULL)
412 dex_coroutine_complete_error (coroutine, NULL);
413 else if (dex_future_is_pending (coroutine->context.pending))
414 dex_coroutine_wait_for (coroutine, g_steal_pointer (&coroutine->context.pending), FALSE);
415 else
416 {
417 DexFuture *completed = g_steal_pointer (&coroutine->context.pending);
418 dex_coroutine_complete_from_future (coroutine, completed);
419 dex_unref (completed);
420 }
421 }
422
423 static gboolean
424 dex_coroutine_scheduler_check (GSource *source)
425 {
426 DexCoroutineScheduler *scheduler = (DexCoroutineScheduler *)source;
427 gboolean ret;
428
429 g_mutex_lock (&scheduler->mutex);
430 g_assert (scheduler->runnable.length == 0 || scheduler->runnable.head != NULL);
431 g_assert (scheduler->runnable.length > 0 || scheduler->runnable.head == NULL);
432 ret = scheduler->runnable.head != NULL;
433 g_mutex_unlock (&scheduler->mutex);
434
435 return ret;
436 }
437
438 static gboolean
439 dex_coroutine_scheduler_prepare (GSource *source,
440 int *timeout)
441 {
442 *timeout = -1;
443
444 return dex_coroutine_scheduler_check (source);
445 }
446
447 static gboolean
448 dex_coroutine_scheduler_iteration (DexCoroutineScheduler *scheduler)
449 {
450 DexCoroutine *coroutine = NULL;
451 gboolean ret;
452 gboolean release = FALSE;
453
454 g_mutex_lock (&scheduler->mutex);
455 if ((coroutine = g_queue_peek_head (&scheduler->runnable)))
456 {
457 g_queue_unlink (&scheduler->runnable, &coroutine->link);
458
459 g_assert (coroutine->queue == CORO_QUEUE_RUNNABLE);
460 g_assert (coroutine->link.data == coroutine);
461
462 coroutine->queue = CORO_QUEUE_NONE;
463 coroutine->running = TRUE;
464 coroutine->coroutine_scheduler = scheduler;
465 scheduler->running = coroutine;
466 }
467 g_mutex_unlock (&scheduler->mutex);
468
469 if (coroutine == NULL)
470 return FALSE;
471
472 dex_coroutine_resume (coroutine);
473
474 g_mutex_lock (&scheduler->mutex);
475 coroutine->running = FALSE;
476 scheduler->running = NULL;
477
478 if (coroutine->exited)
479 {
480 coroutine->coroutine_scheduler = NULL;
481 coroutine->released = TRUE;
482 release = TRUE;
483 }
484
485 ret = scheduler->runnable.length > 0;
486 g_mutex_unlock (&scheduler->mutex);
487
488 if (release)
489 dex_unref (coroutine);
490
491 return ret;
492 }
493
494 static gboolean
495 dex_coroutine_scheduler_dispatch (GSource *source,
496 GSourceFunc callback,
497 gpointer callback_data)
498 {
499 DexCoroutineScheduler *scheduler = (DexCoroutineScheduler *)source;
500 guint max_iterations;
501 DexThreadStorage *thread_storage;
502 DexCoroutineScheduler *previous_scheduler;
503
504 g_assert (scheduler != NULL);
505
506 g_mutex_lock (&scheduler->mutex);
507 max_iterations = MAX (1, scheduler->runnable.length);
508 g_mutex_unlock (&scheduler->mutex);
509
510 thread_storage = dex_thread_storage_get ();
511 previous_scheduler = thread_storage->coroutine_scheduler;
512 thread_storage->coroutine_scheduler = scheduler;
513 while (max_iterations && dex_coroutine_scheduler_iteration (scheduler))
514 max_iterations--;
515 thread_storage->coroutine_scheduler = previous_scheduler;
516
517 return G_SOURCE_CONTINUE;
518 }
519
520 static void
521 dex_coroutine_scheduler_finalize (GSource *source)
522 {
523 DexCoroutineScheduler *scheduler = (DexCoroutineScheduler *)source;
524
525 while (scheduler->runnable.length > 0)
526 {
527 DexCoroutine *routine = g_queue_peek_head (&scheduler->runnable);
528 g_queue_unlink (&scheduler->runnable, &routine->link);
529 dex_unref (routine);
530 }
531
532 while (scheduler->blocked.length > 0)
533 {
534 DexCoroutine *routine = g_queue_peek_head (&scheduler->blocked);
535 g_queue_unlink (&scheduler->blocked, &routine->link);
536 dex_unref (routine);
537 }
538
539 g_mutex_clear (&scheduler->mutex);
540 }
541
542 static GSourceFuncs source_funcs = {
543 .check = dex_coroutine_scheduler_check,
544 .prepare = dex_coroutine_scheduler_prepare,
545 .dispatch = dex_coroutine_scheduler_dispatch,
546 .finalize = dex_coroutine_scheduler_finalize,
547 };
548
549 /**
550 * dex_coroutine_scheduler_new:
551 *
552 * Creates a coroutine scheduler source.
553 *
554 * Returns: (transfer full):
555 */
556 DexCoroutineScheduler *
557 dex_coroutine_scheduler_new (void)
558 {
559 DexCoroutineScheduler *scheduler;
560
561 scheduler = (DexCoroutineScheduler *)g_source_new (&source_funcs, sizeof *scheduler);
562 _g_source_set_static_name ((GSource *)scheduler, "[dex-coroutine-scheduler]");
563 g_mutex_init (&scheduler->mutex);
564
565 return scheduler;
566 }
567
568 void
569 dex_coroutine_scheduler_register (DexCoroutineScheduler *scheduler,
570 DexCoroutine *coroutine)
571 {
572 g_return_if_fail (scheduler != NULL);
573 g_return_if_fail (DEX_IS_COROUTINE (coroutine));
574
575 dex_ref (coroutine);
576
577 g_mutex_lock (&scheduler->mutex);
578
579 g_assert (!coroutine->running);
580 g_assert (!coroutine->exited);
581 g_assert (coroutine->queue == CORO_QUEUE_NONE);
582 g_assert (coroutine->coroutine_scheduler == NULL);
583
584 coroutine->coroutine_scheduler = scheduler;
585 dex_coroutine_set_queue (scheduler, coroutine, CORO_QUEUE_RUNNABLE);
586
587 g_mutex_unlock (&scheduler->mutex);
588
589 if (dex_thread_storage_get ()->coroutine_scheduler != scheduler)
590 g_main_context_wakeup (g_source_get_context ((GSource *)scheduler));
591 }
592
593 static void
594 dex_coroutine_discard (DexFuture *future)
595 {
596 DexCoroutine *coroutine = DEX_COROUTINE (future);
597 DexFuture *pending = NULL;
598
599 g_return_if_fail (coroutine != NULL);
600
601 dex_object_lock (coroutine);
602
603 if (coroutine->exited)
604 {
605 dex_object_unlock (coroutine);
606 return;
607 }
608
609 coroutine->cancelled = TRUE;
610 pending = g_steal_pointer (&coroutine->context.pending);
611
612 if (coroutine->running)
613 {
614 dex_object_unlock (coroutine);
615 return;
616 }
617
618 dex_coroutine_detach_from_scheduler (coroutine);
619
620 coroutine->awaiting_final = FALSE;
621 coroutine->exited = TRUE;
622 dex_object_unlock (coroutine);
623
624 if (pending != NULL)
625 {
626 if (dex_future_is_pending (pending))
627 dex_future_discard (pending, DEX_FUTURE (coroutine));
628 dex_unref (pending);
629 }
630
631 dex_coroutine_complete_cancelled (coroutine);
632 }
633
634 static gboolean
635 dex_coroutine_propagate (DexFuture *future,
636 DexFuture *completed)
637 {
638 DexCoroutine *coroutine = DEX_COROUTINE (future);
639
640 g_assert (coroutine != NULL);
641 g_assert (completed != NULL);
642
643 if (coroutine->cancelled)
644 {
645 dex_coroutine_complete_cancelled (coroutine);
646 return TRUE;
647 }
648
649 if (!coroutine->awaiting_final)
650 {
651 if (coroutine->coroutine_scheduler != NULL)
652 {
653 GSource *source;
654 DexThreadStorage *thread_storage;
655
656 g_mutex_lock (&coroutine->coroutine_scheduler->mutex);
657 dex_coroutine_set_queue (coroutine->coroutine_scheduler,
658 coroutine,
659 CORO_QUEUE_RUNNABLE);
660 thread_storage = dex_thread_storage_get ();
661 source = thread_storage->coroutine_scheduler == coroutine->coroutine_scheduler
662 ? NULL
663 : g_source_ref ((GSource *)coroutine->coroutine_scheduler);
664 g_mutex_unlock (&coroutine->coroutine_scheduler->mutex);
665
666 if (source != NULL)
667 {
668 g_main_context_wakeup (g_source_get_context (source));
669 g_source_unref (source);
670 }
671 }
672
673 return TRUE;
674 }
675
676 dex_clear (&coroutine->context.pending);
677
678 coroutine->awaiting_final = FALSE;
679 coroutine->exited = TRUE;
680
681 dex_coroutine_detach_from_scheduler (coroutine);
682
683 if (coroutine->cancelled)
684 dex_coroutine_complete_cancelled (coroutine);
685 else
686 dex_coroutine_complete_from_future (coroutine, completed);
687
688 return TRUE;
689 }
690
691 DexCoroutine *
692 dex_coroutine_new (DexCoroutineFunc func,
693 gpointer user_data,
694 GDestroyNotify user_data_destroy)
695 {
696 DexCoroutine *coroutine;
697
698 g_return_val_if_fail (func != NULL, NULL);
699
700 coroutine = (DexCoroutine *)dex_object_create_instance (DEX_TYPE_COROUTINE);
701 coroutine->func = func;
702 coroutine->user_data = user_data;
703 coroutine->user_data_destroy = user_data_destroy;
704
705 return coroutine;
706 }