Debian Codegraph @

src/dex-coroutine.h

raw symbols SCIP
1 /*
2 * dex-coroutine.h
3 *
4 * Copyright 2026 Christian Hergert <[email protected]>
5 *
6 * This library 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 library 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 program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #pragma once
23
24 #if !defined (DEX_INSIDE) && !defined (DEX_COMPILATION)
25 # error "Only <libdex.h> can be included directly."
26 #endif
27
28 #include "dex-future.h"
29 #include "dex-scheduler.h"
30 #include "dex-version-macros.h"
31
32 G_BEGIN_DECLS
33
34 typedef struct _DexCoroutine DexCoroutine;
35 typedef struct _DexCoroutineClass DexCoroutineClass;
36
37 #define DEX_TYPE_COROUTINE (dex_coroutine_get_type())
38 #define DEX_COROUTINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEX_TYPE_COROUTINE, DexCoroutine))
39 #define DEX_IS_COROUTINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEX_TYPE_COROUTINE))
40
41 DEX_AVAILABLE_IN_ALL
42 GType dex_coroutine_get_type (void);
43 DEX_AVAILABLE_IN_ALL
44 void dex_coroutine_context_suspend (DexCoroutineContext *context,
45 guint pc,
46 DexFuture *future);
47 DEX_AVAILABLE_IN_ALL
48 void dex_coroutine_context_resume (DexCoroutineContext *context,
49 guint *pc,
50 DexFuture **future);
51
52 /**
53 * DEX_COROUTINE_BEGIN:
54 *
55 * Used in a coroutine function body to establish coroutine state.
56 *
57 * The `context` argument is expected to be a [[email protected]].
58 *
59 * Coroutine functions are expected to return #DexFuture for completion, and
60 * return %NULL while suspended. When suspended, `pending` contains the awaited
61 * future and is owned by the coroutine state.
62 */
63 #define DEX_COROUTINE_BEGIN(context) \
64 { DexCoroutineContext *__dex_coro = (DexCoroutineContext *)(context); \
65 guint __dex_coro_pc = 0; \
66 DexFuture *__dex_coro_pending = NULL; \
67 g_return_val_if_fail (__dex_coro != NULL, NULL); \
68 dex_coroutine_context_resume (__dex_coro, &__dex_coro_pc, &__dex_coro_pending); \
69 switch (__dex_coro_pc) { case 0:
70
71 /**
72 * DEX_COROUTINE_END:
73 *
74 * Closes a #DEX_COROUTINE_BEGIN block.
75 */
76 #define DEX_COROUTINE_END \
77 default: \
78 g_assert_not_reached (); \
79 return NULL; \
80 } \
81 return NULL; }
82
83 #define _DEX_COROUTINE_SUSPEND(_out, _error, _future, _await, _id) \
84 G_GNUC_FALLTHROUGH; \
85 case _id: \
86 { \
87 gboolean __resumed = __dex_coro_pc == (_id); \
88 DexFuture *__future = __resumed ? __dex_coro_pending : (_future); \
89 if (dex_future_is_pending (__future)) \
90 { \
91 dex_coroutine_context_suspend (__dex_coro, (_id), g_steal_pointer (&__future)); \
92 return NULL; \
93 } \
94 __dex_coro_pc = 0; \
95 *(_out) = (__typeof__(*(_out))) (_await (g_steal_pointer (&__future), (_error))); \
96 }
97
98 #define _DEX_COROUTINE_SUSPEND_VOID(_error, _future, _id) \
99 G_GNUC_FALLTHROUGH; \
100 case _id: \
101 { \
102 gboolean __resumed = __dex_coro_pc == (_id); \
103 DexFuture *__future = __resumed ? __dex_coro_pending : (_future); \
104 if (dex_future_is_pending (__future)) \
105 { \
106 dex_coroutine_context_suspend (__dex_coro, (_id), g_steal_pointer (&__future)); \
107 return NULL; \
108 } \
109 __dex_coro_pc = 0; \
110 (void) dex_await (g_steal_pointer (&__future), (_error)); \
111 }
112
113 #define _DEX_COROUTINE_LABEL_ID() (__COUNTER__ + 1)
114
115 #define DEX_COROUTINE_SUSPEND(_future, _error) \
116 _DEX_COROUTINE_SUSPEND_VOID (_error, _future, _DEX_COROUTINE_LABEL_ID ())
117
118 #define DEX_COROUTINE_SUSPEND_BOOLEAN(_out, _error, _future) \
119 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_boolean, _DEX_COROUTINE_LABEL_ID ())
120
121 #define DEX_COROUTINE_SUSPEND_ENUM(_out, _error, _future) \
122 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_enum, _DEX_COROUTINE_LABEL_ID ())
123
124 #define DEX_COROUTINE_SUSPEND_FLAGS(_out, _error, _future) \
125 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_flags, _DEX_COROUTINE_LABEL_ID ())
126
127 #define DEX_COROUTINE_SUSPEND_OBJECT(_out, _error, _future) \
128 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_object, _DEX_COROUTINE_LABEL_ID ())
129
130 #define DEX_COROUTINE_SUSPEND_BOXED(_out, _error, _future) \
131 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_boxed, _DEX_COROUTINE_LABEL_ID ())
132
133 #define DEX_COROUTINE_SUSPEND_POINTER(_out, _error, _future) \
134 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_pointer, _DEX_COROUTINE_LABEL_ID ())
135
136 #define DEX_COROUTINE_SUSPEND_INT(_out, _error, _future) \
137 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_int, _DEX_COROUTINE_LABEL_ID ())
138
139 #define DEX_COROUTINE_SUSPEND_UINT(_out, _error, _future) \
140 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_uint, _DEX_COROUTINE_LABEL_ID ())
141
142 #define DEX_COROUTINE_SUSPEND_INT64(_out, _error, _future) \
143 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_int64, _DEX_COROUTINE_LABEL_ID ())
144
145 #define DEX_COROUTINE_SUSPEND_UINT64(_out, _error, _future) \
146 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_uint64, _DEX_COROUTINE_LABEL_ID ())
147
148 #define DEX_COROUTINE_SUSPEND_DOUBLE(_out, _error, _future) \
149 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_double, _DEX_COROUTINE_LABEL_ID ())
150
151 #define DEX_COROUTINE_SUSPEND_FLOAT(_out, _error, _future) \
152 _DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_float, _DEX_COROUTINE_LABEL_ID ())
153
154 G_END_DECLS