Debian Codegraph @

src/dex-task-group.c

raw symbols SCIP
1 #include "config.h"
2
3 #include <gio/gio.h>
4
5 #include "dex-error.h"
6 #include "dex-task-group.h"
7 #include "dex-future-private.h"
8 #include "dex-thread-storage-private.h"
9
10 typedef struct _DexTaskGroup
11 {
12 DexFuture parent_instance;
13 GQueue futures;
14 DexScheduler *scheduler;
15 GError *first_error;
16 gpointer prev_thread_default;
17 DexTaskGroupFlags flags;
18 guint n_pending;
19 guint n_resolved;
20 guint n_rejected;
21 guint closed : 1;
22 guint cancelled : 1;
23 guint pushed : 1;
24 } DexTaskGroup;
25
26 typedef struct _DexTaskGroupClass
27 {
28 DexFutureClass parent_class;
29 } DexTaskGroupClass;
30
31 /**
32 * DexTaskGroup:
33 *
34 * A structured scope for related [[email protected]] instances.
35 *
36 * A task group owns the futures added to it until the group is closed or
37 * cancelled. The group itself is also a [[email protected]], so callers can
38 * await the scope to learn when all tracked futures have finished.
39 *
40 * Use [[email protected]] to attach futures explicitly, or pass `NULL`
41 * to [[email protected]] after pushing a thread-default group with
42 * [[email protected]_thread_default]. Close the group with
43 * [[email protected]] once no more futures will be added.
44 *
45 * Cancellation is stronger than ordinary discard-driven cleanup: calling
46 * [[email protected]] cancels all tracked children, including nested
47 * task groups, and completes the task group with a cancellation error. When
48 * [[email protected]] is used instead, the task group resolves only
49 * after every tracked future has resolved or rejected.
50 *
51 * Developer note: always pair [[email protected]_thread_default] with
52 * [[email protected]_thread_default] on the same thread, and prefer
53 * [[email protected]] over relying on finalization to finish work.
54 *
55 * Since: 1.2
56 */
57 DEX_DEFINE_FINAL_TYPE (DexTaskGroup, dex_task_group, DEX_TYPE_FUTURE)
58
59 #undef DEX_TYPE_TASK_GROUP
60 #define DEX_TYPE_TASK_GROUP dex_task_group_type
61
62 static void dex_task_group_cancel_child (DexTaskGroup *group,
63 DexFuture *future);
64 static void dex_task_group_complete_cancelled (DexTaskGroup *group,
65 const GError *no_copy,
66 GError *copied);
67 static void dex_task_group_discard (DexFuture *future);
68
69 static const GError *static_cancelled;
70
71 static gboolean
72 dex_task_group_propagate (DexFuture *future,
73 DexFuture *completed)
74 {
75 DexTaskGroup *group = DEX_TASK_GROUP (future);
76 gboolean should_complete = FALSE;
77 gboolean should_cancel_pending = FALSE;
78 gboolean should_discard_child = FALSE;
79 gboolean resolved = FALSE;
80 GError *error = NULL;
81
82 dex_object_lock (group);
83
84 if (group->n_pending > 0)
85 {
86 group->n_pending--;
87 should_discard_child = TRUE;
88
89 if (!group->cancelled)
90 {
91 if (dex_future_is_resolved (completed))
92 group->n_resolved++;
93 else
94 {
95 group->n_rejected++;
96
97 if (group->first_error == NULL)
98 {
99 dex_future_get_value (completed, &group->first_error);
100
101 if (group->first_error != NULL)
102 error = g_error_copy (group->first_error);
103 }
104
105 if ((group->flags & DEX_TASK_GROUP_FLAGS_CANCEL_ON_ERROR) != 0 &&
106 !group->closed)
107 {
108 group->closed = TRUE;
109 group->cancelled = TRUE;
110 should_cancel_pending = TRUE;
111 }
112 }
113 }
114
115 if (group->closed && group->n_pending == 0)
116 {
117 should_complete = TRUE;
118 resolved = (group->n_rejected == 0);
119
120 if (!resolved && group->first_error != NULL && error == NULL)
121 error = g_error_copy (group->first_error);
122 }
123 }
124
125 dex_object_unlock (group);
126
127 if (should_discard_child)
128 {
129 g_queue_unlink (&group->futures, &completed->task_group_link);
130 dex_future_discard (completed, DEX_FUTURE (group));
131 dex_unref (completed);
132 }
133
134 if (should_cancel_pending)
135 {
136 for (GList *iter = group->futures.head; iter != NULL; iter = iter->next)
137 dex_task_group_cancel_child (group, iter->data);
138
139 dex_task_group_complete_cancelled (group,
140 static_cancelled,
141 g_steal_pointer (&error));
142 return TRUE;
143 }
144
145 if (should_complete)
146 {
147 if (resolved)
148 {
149 GValue value = G_VALUE_INIT;
150 g_value_init (&value, G_TYPE_BOOLEAN);
151 g_value_set_boolean (&value, TRUE);
152 dex_future_complete (future, &value, NULL);
153 g_value_unset (&value);
154 }
155 else
156 {
157 if (error == NULL)
158 error = g_error_new_literal (DEX_ERROR,
159 DEX_ERROR_DEPENDENCY_FAILED,
160 "One or more task group children failed");
161 dex_future_complete (future, NULL, g_steal_pointer (&error));
162 }
163 }
164
165 g_clear_error (&error);
166
167 return TRUE;
168 }
169
170 static void
171 dex_task_group_finalize (DexObject *object)
172 {
173 DexTaskGroup *group = DEX_TASK_GROUP (object);
174 DexThreadStorage *storage;
175
176 storage = dex_thread_storage_peek ();
177 if (group->pushed && storage != NULL && storage->task_group == group)
178 {
179 g_critical ("DexTaskGroup %p was finalized while still pushed as the thread-default task group; "
180 "call dex_task_group_pop_thread_default() before unreffing the group",
181 group);
182 dex_unref (storage->task_group);
183 storage->task_group = group->prev_thread_default;
184 group->prev_thread_default = NULL;
185 group->pushed = FALSE;
186 }
187
188 if (group->futures.head != NULL)
189 {
190 while (group->futures.head != NULL)
191 {
192 DexFuture *future = g_queue_peek_head (&group->futures);
193
194 g_queue_unlink (&group->futures, &future->task_group_link);
195 dex_future_discard (future, DEX_FUTURE (group));
196 dex_unref (future);
197 }
198 }
199
200 g_clear_error (&group->first_error);
201
202 DEX_OBJECT_CLASS (dex_task_group_parent_class)->finalize (object);
203 }
204
205 static void
206 dex_task_group_class_init (DexTaskGroupClass *klass)
207 {
208 DexObjectClass *object_class = DEX_OBJECT_CLASS (klass);
209 DexFutureClass *future_class = DEX_FUTURE_CLASS (klass);
210
211 object_class->finalize = dex_task_group_finalize;
212 future_class->propagate = dex_task_group_propagate;
213 future_class->discard = dex_task_group_discard;
214
215 static_cancelled = g_error_new_literal (G_IO_ERROR,
216 G_IO_ERROR_CANCELLED,
217 "Task group was cancelled");
218 }
219
220 static void
221 dex_task_group_init (DexTaskGroup *group)
222 {
223 }
224
225 static void
226 dex_task_group_discard (DexFuture *future)
227 {
228 dex_task_group_cancel (DEX_TASK_GROUP (future));
229 }
230
231 DexTaskGroup *
232 dex_task_group_new (DexTaskGroupFlags flags)
233 {
234 DexTaskGroup *group = (DexTaskGroup *)dex_object_create_instance (DEX_TYPE_TASK_GROUP);
235 group->flags = flags;
236 return group;
237 }
238
239 /**
240 * dex_task_group_add:
241 * @group: (nullable): a [[email protected]]
242 * @future: (transfer full): a [[email protected]]
243 *
244 * Adds @future to @group.
245 *
246 * If @group is `null`, then the future will be disowned.
247 * Otherwise, @future will be added to @group.
248 *
249 * Returns: `false` if the group is closed or cancelled;
250 * otherwise `true`.
251 *
252 * Since: 1.2
253 */
254 gboolean
255 dex_task_group_add (DexTaskGroup *group,
256 DexFuture *future)
257 {
258 g_return_val_if_fail (DEX_IS_FUTURE (future), FALSE);
259
260 if (group == NULL)
261 {
262 DexThreadStorage *storage = dex_thread_storage_peek ();
263 group = storage != NULL ? storage->task_group : NULL;
264
265 if (group == NULL)
266 {
267 dex_future_disown (g_steal_pointer (&future));
268 return TRUE;
269 }
270 }
271
272 g_return_val_if_fail (DEX_IS_TASK_GROUP (group), FALSE);
273 g_return_val_if_fail (future != DEX_FUTURE (group), FALSE);
274
275 dex_object_lock (group);
276 if (group->closed || group->cancelled)
277 {
278 dex_object_unlock (group);
279 dex_future_disown (g_steal_pointer (&future));
280 return FALSE;
281 }
282
283 /* take ownership of @future */
284 g_queue_push_tail_link (&group->futures, &future->task_group_link);
285 group->n_pending++;
286 dex_object_unlock (group);
287 dex_future_chain (future, DEX_FUTURE (group));
288
289 return TRUE;
290 }
291
292 /**
293 * dex_task_group_close:
294 * @group: a [[email protected]]
295 *
296 * Close the group to new tasks.
297 *
298 * Returns: (transfer full): a [[email protected]] that resolves when all the
299 * collected futures have resolved or rejected.
300 *
301 * Since: 1.2
302 */
303 DexFuture *
304 dex_task_group_close (DexTaskGroup *group)
305 {
306 gboolean resolved = FALSE;
307 GError *error = NULL;
308
309 dex_return_error_if_fail (DEX_IS_TASK_GROUP (group));
310
311 dex_object_lock (group);
312 group->closed = TRUE;
313 if (group->n_pending == 0 && DEX_FUTURE (group)->status == DEX_FUTURE_STATUS_PENDING)
314 {
315 resolved = (group->n_rejected == 0);
316 if (!resolved && group->first_error != NULL)
317 error = g_error_copy (group->first_error);
318 }
319 dex_object_unlock (group);
320
321 if (error != NULL || resolved)
322 {
323 if (resolved)
324 {
325 GValue value = G_VALUE_INIT;
326 g_value_init (&value, G_TYPE_BOOLEAN);
327 g_value_set_boolean (&value, TRUE);
328 dex_future_complete (DEX_FUTURE (group), &value, NULL);
329 g_value_unset (&value);
330 }
331 else
332 dex_future_complete (DEX_FUTURE (group), NULL, g_steal_pointer (&error));
333 }
334
335 return DEX_FUTURE (dex_ref (group));
336 }
337
338 static void
339 dex_task_group_cancel_child (DexTaskGroup *group,
340 DexFuture *future)
341 {
342 if (DEX_FUTURE_GET_CLASS (future)->discard != NULL)
343 DEX_FUTURE_GET_CLASS (future)->discard (future);
344
345 dex_future_complete (future, NULL, g_error_copy (static_cancelled));
346 dex_future_discard (future, DEX_FUTURE (group));
347 }
348
349 static void
350 dex_task_group_complete_cancelled (DexTaskGroup *group,
351 const GError *no_copy,
352 GError *copied)
353 {
354 g_assert (DEX_IS_TASK_GROUP (group));
355 g_assert (no_copy != NULL || copied != NULL);
356
357 if (dex_future_get_status (DEX_FUTURE (group)) == DEX_FUTURE_STATUS_PENDING)
358 {
359 if (copied != NULL)
360 dex_future_complete (DEX_FUTURE (group), NULL, g_steal_pointer (&copied));
361 else
362 dex_future_complete (DEX_FUTURE (group), NULL, g_error_copy (no_copy));
363 }
364
365 g_clear_error (&copied);
366 }
367
368 void
369 dex_task_group_cancel (DexTaskGroup *group)
370 {
371 g_return_if_fail (DEX_IS_TASK_GROUP (group));
372
373 dex_object_lock (group);
374 if (group->cancelled)
375 {
376 dex_object_unlock (group);
377 return;
378 }
379
380 group->n_pending = 0;
381 group->cancelled = TRUE;
382 group->closed = TRUE;
383
384 dex_object_unlock (group);
385
386 for (GList *iter = group->futures.head; iter != NULL; iter = iter->next)
387 dex_task_group_cancel_child (group, iter->data);
388
389 dex_task_group_complete_cancelled (group, static_cancelled, NULL);
390 }
391
392 void
393 dex_task_group_push_thread_default (DexTaskGroup *group)
394 {
395 DexThreadStorage *storage;
396
397 g_return_if_fail (DEX_IS_TASK_GROUP (group));
398
399 storage = dex_thread_storage_get ();
400
401 group->prev_thread_default = storage->task_group;
402 storage->task_group = dex_ref (group);
403 group->pushed = TRUE;
404 }
405
406 void
407 dex_task_group_pop_thread_default (DexTaskGroup *group)
408 {
409 DexThreadStorage *storage;
410
411 g_return_if_fail (DEX_IS_TASK_GROUP (group));
412 g_return_if_fail (group->pushed);
413
414 storage = dex_thread_storage_get ();
415 g_return_if_fail (storage->task_group == group);
416
417 dex_unref (storage->task_group);
418 storage->task_group = group->prev_thread_default;
419 group->prev_thread_default = NULL;
420 group->pushed = FALSE;
421 }