Debian Codegraph @

src/dex-future-set.c

raw symbols SCIP
1 /*
2 * dex-future-set.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-error.h"
27 #include "dex-future-set-private.h"
28
29 /**
30 * DexFutureSet:
31 *
32 * `DexFutureSet` represents a set of [[email protected]].
33 *
34 * You may retrieve each underlying `DexFuture` using
35 * [[email protected]_future_at].
36 *
37 * The [[email protected]] of of the `DexFutureSet` depends on how the set
38 * was created using [[email protected]], [[email protected]], and similar
39 * methods.
40 */
41
42 typedef struct _DexFutureSet
43 {
44 DexFuture parent_instance;
45 DexFuture **futures;
46 guint n_futures;
47 guint n_resolved;
48 guint n_rejected;
49 DexFutureSetFlags flags : 4;
50 guint padding : 28;
51 /* If n_futures <= 2, we use this instead of mallocing. We could
52 * potentially get a 3rd without breaking 2-cachelines (128 bytes)
53 * if we used a bit above to manage a union of futures/embedded.
54 */
55 DexFuture *embedded[2];
56 } DexFutureSet;
57
58 typedef struct _DexFutureSetClass
59 {
60 DexFutureClass parent_class;
61 } DexFutureSetClass;
62
63 DEX_DEFINE_FINAL_TYPE (DexFutureSet, dex_future_set, DEX_TYPE_FUTURE)
64
65 #undef DEX_TYPE_FUTURE_SET
66 #define DEX_TYPE_FUTURE_SET dex_future_set_type
67
68 static GValue success_value = G_VALUE_INIT;
69
70 static gboolean
71 dex_future_set_propagate (DexFuture *future,
72 DexFuture *completed)
73 {
74 DexFutureSet *future_set = DEX_FUTURE_SET (future);
75 const GValue *resolved = NULL;
76 DexFutureStatus status;
77 gboolean do_discard = FALSE;
78 GError *rejected = NULL;
79 guint n_active = 0;
80
81 g_assert (DEX_IS_FUTURE_SET (future_set));
82 g_assert (DEX_IS_FUTURE (completed));
83 g_assert (future != completed);
84
85 dex_object_lock (future_set);
86
87 /* Short-circuit if we've already returned a value */
88 if (future->status != DEX_FUTURE_STATUS_PENDING)
89 {
90 dex_object_unlock (future_set);
91 return TRUE;
92 }
93
94 switch ((int)(status = dex_future_get_status (completed)))
95 {
96 case DEX_FUTURE_STATUS_RESOLVED:
97 future_set->n_resolved++;
98 break;
99
100 case DEX_FUTURE_STATUS_REJECTED:
101 future_set->n_rejected++;
102 break;
103
104 default:
105 g_assert_not_reached ();
106 }
107
108 n_active = future_set->n_futures - future_set->n_rejected - future_set->n_resolved;
109 resolved = dex_future_get_value (completed, &rejected);
110
111 g_assert (future_set->n_rejected <= future_set->n_futures);
112 g_assert (future_set->n_resolved <= future_set->n_futures);
113 g_assert (future_set->n_resolved + future_set->n_rejected <= future_set->n_futures);
114 g_assert (rejected != NULL || resolved != NULL);
115
116 dex_object_unlock (future_set);
117
118 if (n_active == 0)
119 {
120 do_discard = TRUE;
121
122 if (resolved != NULL)
123 {
124 if (future_set->flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE)
125 dex_future_complete (future, resolved, NULL);
126 else
127 dex_future_complete (future, &success_value, NULL);
128 }
129 else
130 {
131 if (future_set->flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT)
132 dex_future_complete (future, NULL, g_steal_pointer (&rejected));
133 else
134 dex_future_complete (future,
135 NULL,
136 g_error_new_literal (DEX_ERROR,
137 DEX_ERROR_DEPENDENCY_FAILED,
138 "Too many futures failed"));
139 }
140 }
141 else if (future_set->flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST)
142 {
143 do_discard = TRUE;
144
145 if (resolved && (future_set->flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE) != 0)
146 dex_future_complete (future, resolved, NULL);
147 else if (rejected && (future_set->flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT) != 0)
148 dex_future_complete (future, NULL, g_steal_pointer (&rejected));
149 else
150 do_discard = FALSE;
151 }
152
153 g_clear_error (&rejected);
154
155 if (do_discard)
156 {
157 if (dex_future_get_status (future) != DEX_FUTURE_STATUS_PENDING)
158 {
159 for (guint i = 0; i < future_set->n_futures; i++)
160 dex_future_discard (future_set->futures[i], future);
161 }
162 }
163
164 return TRUE;
165 }
166
167 static void
168 dex_future_set_finalize (DexObject *object)
169 {
170 DexFutureSet *future_set = DEX_FUTURE_SET (object);
171
172 for (guint i = 0; i < future_set->n_futures; i++)
173 {
174 DexFuture *future = g_steal_pointer (&future_set->futures[i]);
175
176 if (future != NULL)
177 {
178 dex_future_discard (future, DEX_FUTURE (future_set));
179 dex_unref (future);
180 }
181 }
182
183 if (future_set->futures != future_set->embedded)
184 g_clear_pointer (&future_set->futures, g_free);
185
186 future_set->futures = NULL;
187 future_set->n_futures = 0;
188 future_set->flags = 0;
189 future_set->n_resolved = 0;
190 future_set->n_rejected = 0;
191
192 DEX_OBJECT_CLASS (dex_future_set_parent_class)->finalize (object);
193 }
194
195 static void
196 dex_future_set_class_init (DexFutureSetClass *future_set_class)
197 {
198 DexObjectClass *object_class = DEX_OBJECT_CLASS (future_set_class);
199 DexFutureClass *future_class = DEX_FUTURE_CLASS (future_set_class);
200
201 object_class->finalize = dex_future_set_finalize;
202
203 future_class->propagate = dex_future_set_propagate;
204
205 g_value_init (&success_value, G_TYPE_BOOLEAN);
206 g_value_set_boolean (&success_value, TRUE);
207 }
208
209 static void
210 dex_future_set_init (DexFutureSet *future_set)
211 {
212 }
213
214 /**
215 * dex_future_set_new: (skip)
216 * @futures: (transfer none) (array length=n_futures): an array of futures to chain
217 * @n_futures: number of futures
218 * @flags: the flags for how the future set should resolve/reject
219 *
220 * Creates a new future set (also a future) which will resovle or reject
221 * based on the completion from sub [[email protected]].
222 *
223 * There must be at least 1 future provided in @futures.
224 *
225 * Returns: the new [[email protected]]
226 */
227 DexFutureSet *
228 dex_future_set_new (DexFuture * const *futures,
229 guint n_futures,
230 DexFutureSetFlags flags)
231 {
232 DexFutureSet *future_set;
233
234 g_return_val_if_fail (futures != NULL, NULL);
235 g_return_val_if_fail (n_futures > 0, NULL);
236 g_return_val_if_fail ((flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST) == 0 ||
237 (flags & (DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE|DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT)) != 0,
238 NULL);
239
240 /* Setup our new DexFuture to contain the results */
241 future_set = (DexFutureSet *)dex_object_create_instance (DEX_TYPE_FUTURE_SET);
242 future_set->n_futures = n_futures;
243 future_set->flags = flags;
244
245 if (n_futures <= G_N_ELEMENTS (future_set->embedded))
246 future_set->futures = future_set->embedded;
247 else
248 future_set->futures = g_new0 (DexFuture *, n_futures);
249
250 /* Ref all futures before potentially calling out to chain them */
251 for (guint i = 0; i < n_futures; i++)
252 future_set->futures[i] = dex_ref (futures[i]);
253
254 /* Now start chaining futures, even if we progress from pending while
255 * we iterate this list (as we're safe against multiple resolves).
256 */
257 for (guint i = 0; i < n_futures; i++)
258 dex_future_chain (future_set->futures[i], DEX_FUTURE (future_set));
259
260 return future_set;
261 }
262
263 DexFutureSet *
264 dex_future_set_new_va (DexFuture *first_future,
265 va_list *args,
266 DexFutureSetFlags flags)
267 {
268 DexFutureSet *future_set;
269 DexFuture *future = first_future;
270 guint capacity = 8;
271
272 g_return_val_if_fail (DEX_IS_FUTURE (first_future), NULL);
273 g_return_val_if_fail ((flags & DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST) == 0 ||
274 (flags & (DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE|DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT)) != 0,
275 NULL);
276
277 future_set = (DexFutureSet *)dex_object_create_instance (DEX_TYPE_FUTURE_SET);
278 future_set->flags = flags;
279 future_set->futures = future_set->embedded;
280
281 g_assert (capacity > G_N_ELEMENTS (future_set->embedded));
282
283 while (future != NULL)
284 {
285 if G_UNLIKELY (future_set->n_futures == G_N_ELEMENTS (future_set->embedded))
286 {
287 future_set->futures = g_new0 (DexFuture *, capacity);
288 for (guint j = 0; j < G_N_ELEMENTS (future_set->embedded); j++)
289 future_set->futures[j] = future_set->embedded[j];
290 }
291 else if G_UNLIKELY (future_set->n_futures + 1 > capacity)
292 {
293 capacity *= 2;
294 future_set->futures = g_realloc_n (future_set->futures, capacity, sizeof (DexFuture *));
295 }
296
297 future_set->futures[future_set->n_futures++] = future;
298 future = va_arg (*args, DexFuture *);
299 }
300
301 /* Now start chaining futures, even if we progress from pending while
302 * we iterate this list (as we're safe against multiple resolves).
303 */
304 for (guint i = 0; i < future_set->n_futures; i++)
305 dex_future_chain (future_set->futures[i], DEX_FUTURE (future_set));
306
307 return future_set;
308 }
309
310 /**
311 * dex_future_set_get_size:
312 * @future_set: a [[email protected]]
313 *
314 * Gets the number of futures associated with the [[email protected]]. You
315 * may use [[email protected]_future_at] to obtain the individual
316 * [[email protected]].
317 *
318 * Returns: the number of [[email protected]] in @future_set.
319 */
320 guint
321 dex_future_set_get_size (DexFutureSet *future_set)
322 {
323 g_return_val_if_fail (DEX_IS_FUTURE_SET (future_set), 0);
324
325 return future_set->n_futures;
326 }
327
328 /**
329 * dex_future_set_get_future_at:
330 * @future_set: a [[email protected]]
331 * @position: the position within the @future_set
332 *
333 * Gets the [[email protected]] at @position within @future_set.
334 *
335 * It is a programmer error to request a `DexFuture` which is outside of the
336 * range of the number of futures contained within the @future_set. Use
337 * [[email protected]_size] to determine the number of `DexFuture` that
338 * are contained within the [[email protected]].
339 *
340 * Returns: (transfer none): the [[email protected]] at @position, or %NULL
341 */
342 DexFuture *
343 dex_future_set_get_future_at (DexFutureSet *future_set,
344 guint position)
345 {
346 g_return_val_if_fail (DEX_IS_FUTURE_SET (future_set), NULL);
347 g_return_val_if_fail (position < future_set->n_futures, NULL);
348
349 return future_set->futures[position];
350 }
351
352 /**
353 * dex_future_set_get_value_at:
354 * @future_set: a [[email protected]]
355 * @position: the [[email protected]] position within the set
356 * @error: location for a [[email protected]], or %NULL
357 *
358 * Gets the result from a [[email protected]] that is part of the
359 * [[email protected]].
360 *
361 * Returns: (transfer none): a [[email protected]] if successful; otherwise
362 * %NULL and @error is set.
363 */
364 const GValue *
365 dex_future_set_get_value_at (DexFutureSet *future_set,
366 guint position,
367 GError **error)
368 {
369 g_return_val_if_fail (DEX_IS_FUTURE_SET (future_set), NULL);
370 g_return_val_if_fail (position < future_set->n_futures, NULL);
371
372 return dex_future_get_value (future_set->futures[position], error);
373 }