Debian Codegraph @

src/dex-block.c

raw symbols SCIP
1 /*
2 * dex-block.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-block-private.h"
25 #include "dex-thread-storage-private.h"
26
27 /**
28 * DexBlock:
29 *
30 * `DexBlock` represents a callback closure that can be scheduled to run
31 * within a specific [[email protected]].
32 *
33 * You create these by chaining futures together using [[email protected]],
34 * [[email protected]], [[email protected]] and similar.
35 */
36
37 typedef struct _DexBlock
38 {
39 DexFuture parent_instance;
40 DexScheduler *scheduler;
41 DexFuture *awaiting;
42 DexFutureCallback callback;
43 gpointer callback_data;
44 GDestroyNotify callback_data_destroy;
45 DexBlockKind kind : 3;
46 guint handled : 1;
47 } DexBlock;
48
49 typedef struct _DexBlockClass
50 {
51 DexFutureClass parent_class;
52 } DexBlockClass;
53
54 DEX_DEFINE_FINAL_TYPE (DexBlock, dex_block, DEX_TYPE_FUTURE)
55
56 G_DEFINE_ENUM_TYPE (DexBlockKind, dex_block_kind,
57 G_DEFINE_ENUM_VALUE (DEX_BLOCK_KIND_THEN, "then"),
58 G_DEFINE_ENUM_VALUE (DEX_BLOCK_KIND_CATCH, "catch"),
59 G_DEFINE_ENUM_VALUE (DEX_BLOCK_KIND_FINALLY, "finally"))
60
61 #undef DEX_TYPE_BLOCK
62 #define DEX_TYPE_BLOCK dex_block_type
63
64 static gboolean
65 dex_block_handles (DexBlock *block,
66 DexFuture *future)
67 {
68 g_assert (DEX_IS_BLOCK (block));
69 g_assert (DEX_IS_FUTURE (future));
70
71 switch (dex_future_get_status (future))
72 {
73 case DEX_FUTURE_STATUS_RESOLVED:
74 return (block->kind & DEX_BLOCK_KIND_THEN) != 0;
75
76 case DEX_FUTURE_STATUS_REJECTED:
77 return (block->kind & DEX_BLOCK_KIND_CATCH) != 0;
78
79 case DEX_FUTURE_STATUS_PENDING:
80 default:
81 return FALSE;
82 }
83 }
84
85 typedef struct _PropagateState
86 {
87 DexBlock *block;
88 DexFuture *completed;
89 } PropagateState;
90
91 static gboolean
92 dex_block_propagate_within_scheduler_internal (PropagateState *state)
93 {
94 DexFuture *delayed = state->block->callback (state->completed, state->block->callback_data);
95
96 /* If we got a future then we need to chain to it so that we get
97 * a second propagation callback with the resolved or rejected
98 * value for resolution.
99 */
100 if (delayed != NULL)
101 {
102 dex_object_lock (state->block);
103 state->block->awaiting = dex_ref (delayed);
104 dex_object_unlock (state->block);
105
106 dex_future_chain (delayed, DEX_FUTURE (state->block));
107 dex_unref (delayed);
108
109 return TRUE;
110 }
111 else
112 {
113 GDestroyNotify notify = NULL;
114 gpointer notify_data = NULL;
115
116 /* We can't asynchronously wait for more futures now so we should
117 * aggressively release the callback data so that any reference cycles
118 * are broken immediately.
119 */
120 dex_object_lock (state->block);
121 notify = g_steal_pointer (&state->block->callback_data_destroy);
122 notify_data = g_steal_pointer (&state->block->callback_data);
123 state->block->callback = NULL;
124 dex_object_unlock (state->block);
125
126 if (notify != NULL)
127 notify (notify_data);
128
129 return FALSE;
130 }
131 }
132
133 static void
134 dex_block_propagate_within_scheduler (gpointer data)
135 {
136 PropagateState *state = data;
137
138 g_assert (state != NULL);
139 g_assert (DEX_IS_BLOCK (state->block));
140 g_assert (DEX_IS_FUTURE (state->completed));
141
142 if (!dex_block_propagate_within_scheduler_internal (state))
143 dex_future_complete (DEX_FUTURE (state->block),
144 state->completed->rejected ? NULL : &state->completed->resolved,
145 state->completed->rejected ? g_error_copy (state->completed->rejected) : NULL);
146
147 dex_clear (&state->block);
148 dex_clear (&state->completed);
149 g_free (state);
150 }
151
152 static gboolean
153 dex_block_propagate (DexFuture *future,
154 DexFuture *completed)
155 {
156 DexBlock *block = DEX_BLOCK (future);
157 DexFuture *awaiting;
158 gboolean do_callback = FALSE;
159
160 g_assert (DEX_IS_BLOCK (block));
161 g_assert (DEX_IS_FUTURE (completed));
162 g_assert (dex_future_get_status (completed) != DEX_FUTURE_STATUS_PENDING);
163
164 /* Mark result as handled as we don't want to execute the callback
165 * again when a possible secondary DexFuture propagates completion
166 * to us. That would only happen if @callback returns a DexFuture
167 * which completes.
168 */
169 dex_object_lock (future);
170 if ((block->kind & DEX_BLOCK_KIND_LOOP) != 0)
171 do_callback = TRUE;
172 else if (!block->handled)
173 do_callback = block->handled = TRUE;
174 awaiting = g_steal_pointer (&block->awaiting);
175 dex_object_unlock (future);
176
177 /* Release the future we were waiting on */
178 dex_clear (&awaiting);
179
180 /* Run the callback, possibly getting a future back to delay further
181 * processing until it's completed.
182 */
183 if (do_callback && dex_block_handles (block, completed))
184 {
185 PropagateState state = {block, completed};
186 DexThreadStorage *storage = dex_thread_storage_get ();
187
188 /* If we are on the same scheduler that created this block, then
189 * we can execute it now.
190 */
191 if (storage->fiber_scheduler == NULL &&
192 block->scheduler == dex_scheduler_get_thread_default () &&
193 storage->sync_dispatch_depth < DEX_DISPATCH_RECURSE_MAX)
194 {
195 gboolean ret;
196
197 storage->sync_dispatch_depth++;
198 ret = dex_block_propagate_within_scheduler_internal (&state);
199 storage->sync_dispatch_depth--;
200
201 return ret;
202 }
203
204 /* Otherwise we must defer it to the scheduler */
205 dex_ref (block);
206 dex_ref (completed);
207 dex_scheduler_push (block->scheduler,
208 dex_block_propagate_within_scheduler,
209 g_memdup2 (&state, sizeof state));
210
211 return TRUE;
212 }
213
214 return FALSE;
215 }
216
217 static void
218 dex_block_finalize (DexObject *object)
219 {
220 DexBlock *block = DEX_BLOCK (object);
221
222 if (block->callback_data_destroy)
223 {
224 block->callback_data_destroy (block->callback_data);
225 block->callback_data_destroy = NULL;
226 block->callback_data = NULL;
227 block->callback = NULL;
228 }
229
230 if (block->awaiting)
231 {
232 dex_future_discard (block->awaiting, DEX_FUTURE (block));
233 dex_clear (&block->awaiting);
234 }
235
236 dex_clear (&block->scheduler);
237
238 DEX_OBJECT_CLASS (dex_block_parent_class)->finalize (object);
239 }
240
241 static void
242 dex_block_class_init (DexBlockClass *block_class)
243 {
244 DexObjectClass *object_class = DEX_OBJECT_CLASS (block_class);
245 DexFutureClass *future_class = DEX_FUTURE_CLASS (block_class);
246
247 object_class->finalize = dex_block_finalize;
248
249 future_class->propagate = dex_block_propagate;
250 }
251
252 static void
253 dex_block_init (DexBlock *block)
254 {
255 }
256
257 /**
258 * dex_block_new:
259 * @future: (transfer full): a [[email protected]] to process
260 * @scheduler: (nullable): a [[email protected]] or %NULL
261 * @kind: the kind of block
262 * @callback: (scope notified): the callback for the block
263 * @callback_data: the data for the callback
264 * @callback_data_destroy: closure destroy for @callback_data
265 *
266 * Creates a new block that will process the result of @future.
267 *
268 * The result of @callback will be assigned to the future returned
269 * from this method.
270 *
271 * Returns: (transfer full): a newly allocated [[email protected]]
272 */
273 DexFuture *
274 dex_block_new (DexFuture *future,
275 DexScheduler *scheduler,
276 DexBlockKind kind,
277 DexFutureCallback callback,
278 gpointer callback_data,
279 GDestroyNotify callback_data_destroy)
280 {
281 DexBlock *block;
282
283 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
284
285 block = (DexBlock *)dex_object_create_instance (dex_block_type);
286 block->scheduler = scheduler ? dex_ref (scheduler) : dex_scheduler_ref_thread_default ();
287 block->awaiting = future;
288 block->kind = kind;
289 block->callback = callback;
290 block->callback_data = callback_data;
291 block->callback_data_destroy = callback_data_destroy;
292
293 g_assert (block->scheduler != NULL);
294
295 dex_future_chain (future, DEX_FUTURE (block));
296
297 return DEX_FUTURE (block);
298 }
299
300 /**
301 * dex_block_get_kind:
302 * @block: a [[email protected]]
303 *
304 * Gets the kind of block.
305 *
306 * The kind of block relates to what situations the block would be
307 * executed such as for handling a future resolution, rejection, or
308 * both.
309 *
310 * Returns: the [[email protected]] associated with the @block
311 */
312 DexBlockKind
313 dex_block_get_kind (DexBlock *block)
314 {
315 g_return_val_if_fail (DEX_IS_BLOCK (block), 0);
316
317 return block->kind;
318 }
319
320 /**
321 * dex_block_get_scheduler:
322 * @block: a [[email protected]]
323 *
324 * Gets the scheduler to use when executing a block.
325 *
326 * Returns: (transfer none): the [[email protected]] associated with the @block
327 */
328 DexScheduler *
329 dex_block_get_scheduler (DexBlock *block)
330 {
331 g_return_val_if_fail (DEX_IS_BLOCK (block), NULL);
332
333 return block->scheduler;
334 }