Debian Codegraph @

src/dex-future.c

raw symbols SCIP
1 /*
2 * dex-future.c
3 *
4 * Copyright 2022-2023 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-block-private.h"
27 #include "dex-fd-private.h"
28 #include "dex-error.h"
29 #include "dex-future-private.h"
30 #include "dex-future-set-private.h"
31 #include "dex-infinite-private.h"
32 #include "dex-promise.h"
33 #include "dex-scheduler.h"
34 #include "dex-static-future-private.h"
35 #include "dex-timeout-private.h"
36
37 /**
38 * DexFuture:
39 *
40 * `DexFuture` is the base class representing a future which may resolve with
41 * a value or reject with error at some point in the future.
42 *
43 * It is the basis for libdex's concurrency and parallelism model.
44 *
45 * Use futures to represent work in progress and allow consumers to build
46 * robust processing chains up front which will complete or fail as futures
47 * resolve or reject.
48 *
49 * When running on a [[email protected]], you may use [[email protected]]
50 * and similar functions to suspend the current thread and return upon
51 * completion of the dependent future.
52 */
53
54 static void dex_future_propagate (DexFuture *future,
55 DexFuture *completed);
56
57 DEX_DEFINE_DERIVABLE_TYPE (DexFuture, dex_future, DEX_TYPE_OBJECT)
58
59 #undef DEX_TYPE_FUTURE
60 #define DEX_TYPE_FUTURE dex_future_type
61
62 static gsize static_booleans_init;
63 static DexFuture *static_booleans[2];
64
65 typedef struct _DexChainedFuture
66 {
67 GList link;
68 DexWeakRef wr;
69 gpointer where_future_was;
70 guint awaiting : 1;
71 } DexChainedFuture;
72
73 static DexChainedFuture *
74 dex_chained_future_new (gpointer object)
75 {
76 DexChainedFuture *cf;
77
78 cf = g_new0 (DexChainedFuture, 1);
79 cf->link.data = cf;
80 cf->awaiting = TRUE;
81 cf->where_future_was = object;
82 dex_weak_ref_init (&cf->wr, object);
83
84 return cf;
85 }
86
87 static void
88 dex_chained_future_free (DexChainedFuture *cf)
89 {
90 g_assert (cf != NULL);
91 g_assert (cf->link.prev == NULL);
92 g_assert (cf->link.next == NULL);
93 g_assert (cf->link.data == cf);
94
95 dex_weak_ref_clear (&cf->wr);
96 cf->link.data = NULL;
97 cf->where_future_was = NULL;
98 cf->awaiting = FALSE;
99 g_free (cf);
100 }
101
102 static void
103 dex_future_notify_complete (DexFuture *future,
104 GQueue *queue)
105 {
106 g_assert (DEX_IS_FUTURE (future));
107 g_assert (queue != NULL);
108
109 /* Iterate in reverse order to give some predictable ordering based on
110 * when chained futures were attached. We've released the lock at this
111 * point to avoid any requests back on future from deadlocking.
112 */
113 while (queue->tail != NULL)
114 {
115 DexChainedFuture *cf = g_queue_pop_tail_link (queue)->data;
116 DexFuture *chained;
117
118 g_assert (cf != NULL);
119 g_assert (cf->link.data == cf);
120
121 chained = dex_weak_ref_get (&cf->wr);
122 dex_weak_ref_set (&cf->wr, NULL);
123
124 g_assert (!chained || DEX_IS_FUTURE (chained));
125
126 /* Always notify even if the future isn't awaiting as
127 * it can provide a bit more information to futures that
128 * are bringing in results until their callbacks are
129 * scheduled for execution.
130 */
131 if (chained != NULL)
132 {
133 dex_future_propagate (chained, future);
134 dex_unref (chained);
135 }
136
137 dex_chained_future_free (cf);
138 }
139 }
140
141 void
142 dex_future_complete_from (DexFuture *future,
143 DexFuture *completed)
144 {
145 GError *error = NULL;
146 const GValue *value = dex_future_get_value (completed, &error);
147 dex_future_complete (future, value, error);
148 }
149
150 void
151 dex_future_complete (DexFuture *future,
152 const GValue *resolved,
153 GError *rejected)
154 {
155 GQueue queue = G_QUEUE_INIT;
156
157 g_return_if_fail (DEX_IS_FUTURE (future));
158 g_return_if_fail (resolved != NULL || rejected != NULL);
159 g_return_if_fail (resolved == NULL || G_IS_VALUE (resolved));
160
161 dex_object_lock (DEX_OBJECT (future));
162 if (future->status == DEX_FUTURE_STATUS_PENDING)
163 {
164 if (resolved != NULL)
165 {
166 g_value_init (&future->resolved, G_VALUE_TYPE (resolved));
167 g_value_copy (resolved, &future->resolved);
168 future->status = DEX_FUTURE_STATUS_RESOLVED;
169 }
170 else
171 {
172 future->rejected = g_steal_pointer (&rejected);
173 future->status = DEX_FUTURE_STATUS_REJECTED;
174 }
175
176 queue = future->chained;
177 future->chained = (GQueue) {NULL, NULL, 0};
178 }
179 else
180 {
181 g_clear_error (&rejected);
182 }
183 dex_object_unlock (DEX_OBJECT (future));
184
185 dex_future_notify_complete (future, &queue);
186 }
187
188 void
189 dex_future_complete_steal (DexFuture *future,
190 GValue *resolved,
191 GError *rejected)
192 {
193 GQueue queue = G_QUEUE_INIT;
194
195 g_return_if_fail (DEX_IS_FUTURE (future));
196 g_return_if_fail (resolved != NULL || rejected != NULL);
197 g_return_if_fail (resolved == NULL || G_IS_VALUE (resolved));
198
199 dex_object_lock (DEX_OBJECT (future));
200 if (future->status == DEX_FUTURE_STATUS_PENDING)
201 {
202 if (resolved != NULL)
203 {
204 future->resolved = *resolved;
205 memset (resolved, 0, sizeof *resolved);
206 future->status = DEX_FUTURE_STATUS_RESOLVED;
207 g_clear_error (&rejected);
208 }
209 else
210 {
211 future->rejected = g_steal_pointer (&rejected);
212 future->status = DEX_FUTURE_STATUS_REJECTED;
213 }
214
215 queue = future->chained;
216 future->chained = (GQueue) {NULL, NULL, 0};
217 }
218 else
219 {
220 if (resolved != NULL)
221 g_value_unset (resolved);
222 g_clear_error (&rejected);
223 }
224 dex_object_unlock (DEX_OBJECT (future));
225
226 dex_future_notify_complete (future, &queue);
227 }
228
229 const GValue *
230 dex_future_get_value (DexFuture *future,
231 GError **error)
232 {
233 const GValue *ret;
234
235 g_return_val_if_fail (DEX_IS_FUTURE (future), FALSE);
236
237 dex_object_lock (DEX_OBJECT (future));
238
239 switch (future->status)
240 {
241 case DEX_FUTURE_STATUS_PENDING:
242 g_set_error_literal (error,
243 DEX_ERROR,
244 DEX_ERROR_PENDING,
245 "Future is still pending");
246 ret = NULL;
247 break;
248
249 case DEX_FUTURE_STATUS_RESOLVED:
250 ret = &future->resolved;
251 break;
252
253 case DEX_FUTURE_STATUS_REJECTED:
254 ret = NULL;
255 if (error != NULL)
256 *error = g_error_copy (future->rejected);
257 break;
258
259 default:
260 g_assert_not_reached ();
261 }
262
263 dex_object_unlock (DEX_OBJECT (future));
264
265 return ret;
266 }
267
268 DexFutureStatus
269 dex_future_get_status (DexFuture *future)
270 {
271 DexFutureStatus status;
272
273 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
274
275 dex_object_lock (DEX_OBJECT (future));
276 status = future->status;
277 dex_object_unlock (DEX_OBJECT (future));
278
279 return status;
280 }
281
282 /**
283 * dex_future_is_resolved:
284 * @future: a [[email protected]]
285 *
286 * This is a convenience function equivalent to calling
287 * [[email protected]_status] and checking for %DEX_FUTURE_STATUS_RESOLVED.
288 *
289 * Returns: %TRUE if the future has successfully resolved with a value;
290 * otherwise %FALSE
291 */
292 gboolean
293 dex_future_is_resolved (DexFuture *future)
294 {
295 gboolean ret;
296
297 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
298
299 dex_object_lock (DEX_OBJECT (future));
300 ret = future->status == DEX_FUTURE_STATUS_RESOLVED;
301 dex_object_unlock (DEX_OBJECT (future));
302
303 return ret;
304 }
305
306 /**
307 * dex_future_is_rejected:
308 * @future: a [[email protected]]
309 *
310 * This is a convenience function equivalent to calling
311 * [[email protected]_status] and checking for %DEX_FUTURE_STATUS_REJECTED.
312 *
313 * Returns: %TRUE if the future was rejected with an error; otherwise %FALSE
314 */
315 gboolean
316 dex_future_is_rejected (DexFuture *future)
317 {
318 gboolean ret;
319
320 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
321
322 dex_object_lock (DEX_OBJECT (future));
323 ret = future->status == DEX_FUTURE_STATUS_REJECTED;
324 dex_object_unlock (DEX_OBJECT (future));
325
326 return ret;
327 }
328
329 /**
330 * dex_future_is_pending:
331 * @future: a [[email protected]]
332 *
333 * This is a convenience function equivalent to calling
334 * [[email protected]_status] and checking for %DEX_FUTURE_STATUS_PENDING.
335 *
336 * Returns: %TRUE if the future is still pending; otherwise %FALSE
337 */
338 gboolean
339 dex_future_is_pending (DexFuture *future)
340 {
341 gboolean ret;
342
343 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
344
345 dex_object_lock (DEX_OBJECT (future));
346 ret = future->status == DEX_FUTURE_STATUS_PENDING;
347 dex_object_unlock (DEX_OBJECT (future));
348
349 return ret;
350 }
351
352 static void
353 dex_future_finalize (DexObject *object)
354 {
355 DexFuture *future = (DexFuture *)object;
356
357 g_assert (future->chained.length == 0);
358 g_assert (future->chained.head == NULL);
359 g_assert (future->chained.tail == NULL);
360
361 if (G_IS_VALUE (&future->resolved))
362 g_value_unset (&future->resolved);
363 g_clear_error (&future->rejected);
364
365 DEX_OBJECT_CLASS (dex_future_parent_class)->finalize (object);
366 }
367
368 static void
369 dex_future_class_init (DexFutureClass *klass)
370 {
371 DexObjectClass *object_class = DEX_OBJECT_CLASS (klass);
372
373 object_class->finalize = dex_future_finalize;
374 }
375
376 static void
377 dex_future_init (DexFuture *future)
378 {
379 future->task_group_link.data = future;
380 }
381
382 static void
383 dex_future_propagate (DexFuture *future,
384 DexFuture *completed)
385 {
386 gboolean handled;
387
388 g_return_if_fail (DEX_IS_FUTURE (future));
389 g_return_if_fail (DEX_IS_FUTURE (completed));
390
391 dex_ref (completed);
392
393 if (DEX_FUTURE_GET_CLASS (future)->propagate)
394 handled = DEX_FUTURE_GET_CLASS (future)->propagate (future, completed);
395 else
396 handled = FALSE;
397
398 if (!handled)
399 {
400 const GValue *resolved = NULL;
401 const GError *rejected = NULL;
402
403 dex_object_lock (future);
404 if (completed->status == DEX_FUTURE_STATUS_RESOLVED)
405 resolved = &completed->resolved;
406 else
407 rejected = completed->rejected;
408 dex_object_unlock (future);
409
410 dex_future_complete (future,
411 resolved,
412 rejected ? g_error_copy (rejected) : NULL);
413 }
414
415 dex_unref (completed);
416 }
417
418 void
419 dex_future_chain (DexFuture *future,
420 DexFuture *chained)
421 {
422 gboolean did_chain = FALSE;
423
424 g_return_if_fail (DEX_IS_FUTURE (future));
425 g_return_if_fail (DEX_IS_FUTURE (chained));
426
427 dex_object_lock (future);
428 if (future->status == DEX_FUTURE_STATUS_PENDING)
429 {
430 DexChainedFuture *cf = dex_chained_future_new (chained);
431 g_queue_push_tail_link (&future->chained, &cf->link);
432 did_chain = TRUE;
433 }
434 dex_object_unlock (future);
435
436 if (!did_chain)
437 dex_future_propagate (chained, future);
438 }
439
440 void
441 dex_future_discard (DexFuture *future,
442 DexFuture *chained)
443 {
444 gboolean has_awaiting = FALSE;
445 gboolean matched = FALSE;
446 GQueue discarded = G_QUEUE_INIT;
447
448 g_return_if_fail (DEX_IS_FUTURE (future));
449 g_return_if_fail (DEX_IS_FUTURE (chained));
450
451 dex_object_lock (future);
452
453 #if 0
454 /* Mark the chained future as no longer necessary to dispatch to.
455 * If so, we can possibly request that @future discard any ongoing
456 * operations so that we propagate cancellation.
457 *
458 * However, if the status has already completed, just short-circuit.
459 */
460 if (future->status != DEX_FUTURE_STATUS_PENDING)
461 {
462 dex_object_unlock (future);
463 return;
464 }
465 #endif
466
467 for (const GList *iter = future->chained.head; iter; )
468 {
469 DexChainedFuture *cf = iter->data;
470
471 iter = iter->next;
472
473 if (chained == cf->where_future_was)
474 {
475 if (cf->awaiting == TRUE)
476 {
477 matched = TRUE;
478 cf->awaiting = FALSE;
479 }
480
481 g_queue_unlink (&future->chained, &cf->link);
482 g_queue_push_tail_link (&discarded, &cf->link);
483 }
484 else
485 {
486 has_awaiting |= cf->awaiting;
487 }
488 }
489
490 dex_object_unlock (future);
491
492 /* Release chained futures outside of the @future lock */
493 while (discarded.head != NULL)
494 {
495 DexChainedFuture *cf = discarded.head->data;
496 g_queue_unlink (&discarded, &cf->link);
497 dex_chained_future_free (cf);
498 }
499
500 /* If we discarded the chained future and there are no more futures
501 * awaiting our response, then request the class discard the future,
502 * possibly cancelling anything in flight.
503 */
504 if (matched && !has_awaiting)
505 {
506 if (DEX_FUTURE_GET_CLASS (future)->discard)
507 {
508 dex_ref (future);
509 DEX_FUTURE_GET_CLASS (future)->discard (future);
510 dex_unref (future);
511 }
512 }
513 }
514
515 /**
516 * dex_future_then: (constructor)
517 * @future: (transfer full): a [[email protected]]
518 * @callback: (scope notified): a callback to execute
519 * @callback_data: closure data for @callback
520 * @callback_data_destroy: destroy notify for @callback_data
521 *
522 * Calls @callback when @future resolves.
523 *
524 * If @future rejects, then @callback will not be called.
525 *
526 * Returns: (transfer full): a [[email protected]]
527 */
528 DexFuture *
529 (dex_future_then) (DexFuture *future,
530 DexFutureCallback callback,
531 gpointer callback_data,
532 GDestroyNotify callback_data_destroy)
533 {
534 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
535 g_return_val_if_fail (callback != NULL, NULL);
536
537 return dex_block_new (future,
538 NULL,
539 DEX_BLOCK_KIND_THEN,
540 callback,
541 callback_data,
542 callback_data_destroy);
543 }
544
545 /**
546 * dex_future_then_loop: (constructor)
547 * @future: (transfer full): a [[email protected]]
548 * @callback: (scope notified) (closure callback_data) (destroy callback_data_destroy): a callback to execute
549 * @callback_data: closure data for @callback
550 * @callback_data_destroy: destroy notify for @callback_data
551 *
552 * Asynchronously calls @callback when @future resolves.
553 *
554 * This is similar to [[email protected]] except that it will call
555 * @callback multiple times as each returned [[email protected]] resolves or
556 * rejects, allowing for infinite loops.
557 *
558 * Returns: (transfer full): a [[email protected]]
559 */
560 DexFuture *
561 (dex_future_then_loop) (DexFuture *future,
562 DexFutureCallback callback,
563 gpointer callback_data,
564 GDestroyNotify callback_data_destroy)
565 {
566 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
567 g_return_val_if_fail (callback != NULL, NULL);
568
569 return dex_block_new (future,
570 NULL,
571 DEX_BLOCK_KIND_THEN | DEX_BLOCK_KIND_LOOP,
572 callback,
573 callback_data,
574 callback_data_destroy);
575 }
576
577 /**
578 * dex_future_catch_loop: (constructor)
579 * @future: (transfer full): a [[email protected]]
580 * @callback: (scope notified) (closure callback_data) (destroy callback_data_destroy): a callback to execute
581 * @callback_data: closure data for @callback
582 * @callback_data_destroy: destroy notify for @callback_data
583 *
584 * Asynchronously calls @callback when @future rejects.
585 *
586 * This is similar to [[email protected]] except that it will call
587 * @callback multiple times as each returned [[email protected]] rejects,
588 * allowing for infinite loops.
589 *
590 * Returns: (transfer full): a [[email protected]]
591 */
592 DexFuture *
593 (dex_future_catch_loop) (DexFuture *future,
594 DexFutureCallback callback,
595 gpointer callback_data,
596 GDestroyNotify callback_data_destroy)
597 {
598 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
599 g_return_val_if_fail (callback != NULL, NULL);
600
601 return dex_block_new (future,
602 NULL,
603 DEX_BLOCK_KIND_CATCH | DEX_BLOCK_KIND_LOOP,
604 callback,
605 callback_data,
606 callback_data_destroy);
607 }
608
609 /**
610 * dex_future_finally_loop: (constructor)
611 * @future: (transfer full): a [[email protected]]
612 * @callback: (scope notified) (closure callback_data) (destroy callback_data_destroy): a callback to execute
613 * @callback_data: closure data for @callback
614 * @callback_data_destroy: destroy notify for @callback_data
615 *
616 * Asynchronously calls @callback when @future rejects or resolves.
617 *
618 * This is similar to [[email protected]] except that it will call
619 * @callback multiple times as each returned [[email protected]] rejects or resolves,
620 * allowing for infinite loops.
621 *
622 * Returns: (transfer full): a [[email protected]]
623 */
624 DexFuture *
625 (dex_future_finally_loop) (DexFuture *future,
626 DexFutureCallback callback,
627 gpointer callback_data,
628 GDestroyNotify callback_data_destroy)
629 {
630 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
631 g_return_val_if_fail (callback != NULL, NULL);
632
633 return dex_block_new (future,
634 NULL,
635 DEX_BLOCK_KIND_THEN | DEX_BLOCK_KIND_CATCH | DEX_BLOCK_KIND_LOOP,
636 callback,
637 callback_data,
638 callback_data_destroy);
639 }
640
641 /**
642 * dex_future_catch: (constructor)
643 * @future: (transfer full): a [[email protected]]
644 * @callback: (scope notified) (closure callback_data) (destroy callback_data_destroy): a callback to execute
645 * @callback_data: closure data for @callback
646 * @callback_data_destroy: destroy notify for @callback_data
647 *
648 * Calls @callback when @future rejects.
649 *
650 * If @future resolves, then @callback will not be called.
651 *
652 * Returns: (transfer full): a [[email protected]]
653 */
654 DexFuture *
655 (dex_future_catch) (DexFuture *future,
656 DexFutureCallback callback,
657 gpointer callback_data,
658 GDestroyNotify callback_data_destroy)
659 {
660 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
661 g_return_val_if_fail (callback != NULL, NULL);
662
663 return dex_block_new (future,
664 NULL,
665 DEX_BLOCK_KIND_CATCH,
666 callback,
667 callback_data,
668 callback_data_destroy);
669 }
670
671 /**
672 * dex_future_finally: (constructor)
673 * @future: (transfer full): a [[email protected]]
674 * @callback: (scope notified) (closure callback_data) (destroy callback_data_destroy): a callback to execute
675 * @callback_data: closure data for @callback
676 * @callback_data_destroy: destroy notify for @callback_data
677 *
678 * Calls @callback when @future resolves or rejects.
679 *
680 * Returns: (transfer full): a [[email protected]]
681 */
682 DexFuture *
683 (dex_future_finally) (DexFuture *future,
684 DexFutureCallback callback,
685 gpointer callback_data,
686 GDestroyNotify callback_data_destroy)
687 {
688 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
689 g_return_val_if_fail (callback != NULL, NULL);
690
691 return dex_block_new (future,
692 NULL,
693 DEX_BLOCK_KIND_FINALLY,
694 callback,
695 callback_data,
696 callback_data_destroy);
697 }
698
699 /**
700 * dex_future_all: (constructor)
701 * @first_future: (transfer full): a [[email protected]]
702 * @...: a %NULL terminated list of futures
703 *
704 * Creates a new [[email protected]] that will resolve or reject when all futures
705 * either resolve or reject.
706 *
707 * This method will return a [[email protected]] which provides API to get
708 * the exact values of the dependent futures. The value of the future
709 * if resolved will be a %G_TYPE_BOOLEAN of %TRUE.
710 *
711 * Returns: (transfer full) (type DexFuture): a [[email protected]]
712 */
713 DexFuture *
714 (dex_future_all) (DexFuture *first_future,
715 ...)
716 {
717 DexFutureSet *ret;
718 va_list args;
719
720 va_start (args, first_future);
721 ret = dex_future_set_new_va (first_future, &args, DEX_FUTURE_SET_FLAGS_NONE);
722 va_end (args);
723
724 return DEX_FUTURE (ret);
725 }
726
727 /**
728 * dex_future_any: (constructor)
729 * @first_future: (transfer full): a [[email protected]]
730 * @...: a %NULL terminated list of futures
731 *
732 * Creates a new [[email protected]] that will resolve when any dependent future
733 * resolves, providing the same result as the resolved future.
734 *
735 * If no futures resolve, then the future will reject.
736 *
737 * Returns: (transfer full) (type DexFuture): a [[email protected]]
738 */
739 DexFuture *
740 (dex_future_any) (DexFuture *first_future,
741 ...)
742 {
743 DexFutureSet *ret;
744 va_list args;
745
746 va_start (args, first_future);
747 ret = dex_future_set_new_va (first_future, &args,
748 (DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST |
749 DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE));
750 va_end (args);
751
752 return DEX_FUTURE (ret);
753 }
754
755 /**
756 * dex_future_all_race: (constructor)
757 * @first_future: (transfer full): a [[email protected]]
758 * @...: a %NULL terminated list of futures
759 *
760 * Creates a new [[email protected]] that will resolve when all futures resolve
761 * or reject as soon as the first future rejects.
762 *
763 * This method will return a [[email protected]] which provides API to get
764 * the exact values of the dependent futures. The value of the future
765 * will be propagated from the resolved or rejected future.
766 *
767 * Since the futures race to complete, some futures retrieved with the
768 * [[email protected]_future_at] API will still be
769 * %DEX_FUTURE_STATUS_PENDING.
770 *
771 * Returns: (transfer full) (type DexFuture): a [[email protected]]
772 */
773 DexFuture *
774 (dex_future_all_race) (DexFuture *first_future,
775 ...)
776 {
777 DexFutureSet *ret;
778 va_list args;
779
780 va_start (args, first_future);
781 ret = dex_future_set_new_va (first_future, &args,
782 (DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST |
783 DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT));
784 va_end (args);
785
786 return DEX_FUTURE (ret);
787 }
788
789 /**
790 * dex_future_first: (constructor)
791 * @first_future: (transfer full): a [[email protected]]
792 * @...: a %NULL terminated list of futures
793 *
794 * Creates a new [[email protected]] that resolves or rejects as soon as the
795 * first dependent future resolves or rejects, sharing the same result.
796 *
797 * Returns: (transfer full) (type DexFuture): a [[email protected]]
798 */
799 DexFuture *
800 (dex_future_first) (DexFuture *first_future,
801 ...)
802 {
803 DexFutureSet *ret;
804 va_list args;
805
806 va_start (args, first_future);
807 ret = dex_future_set_new_va (first_future, &args,
808 (DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST |
809 DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE |
810 DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT));
811 va_end (args);
812
813 return DEX_FUTURE (ret);
814 }
815
816 /**
817 * dex_future_firstv: (rename-to dex_future_first) (constructor)
818 * @futures: (array length=n_futures) (transfer none): an array of futures
819 * @n_futures: the number of futures
820 *
821 * Creates a new [[email protected]] that resolves or rejects as soon as the
822 * first dependent future resolves or rejects, sharing the same result.
823 *
824 * Returns: (transfer full): a [[email protected]]
825 */
826 DexFuture *
827 (dex_future_firstv) (DexFuture * const *futures,
828 guint n_futures)
829 {
830 return DEX_FUTURE (dex_future_set_new (futures, n_futures,
831 (DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST |
832 DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE |
833 DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT)));
834 }
835
836 /**
837 * dex_future_with_timeout_seconds: (constructor)
838 * @future: (transfer full): a [[email protected]]
839 * @seconds: number of seconds
840 *
841 * Creates a new [[email protected]] that resolves or rejects with the same
842 * result as @future, unless @seconds elapses first. If the timeout elapses
843 * first, the returned future rejects with %DEX_ERROR_TIMED_OUT and discards
844 * @future.
845 *
846 * Returns: (transfer full): a [[email protected]]
847 *
848 * Since: 1.2
849 */
850 DexFuture *
851 (dex_future_with_timeout_seconds) (DexFuture *future,
852 int seconds)
853 {
854 dex_return_error_if_fail (DEX_IS_FUTURE (future));
855
856 return _dex_timeout_new_for_future_seconds (future, seconds);
857 }
858
859 /**
860 * dex_future_with_timeout_msec: (constructor)
861 * @future: (transfer full): a [[email protected]]
862 * @msec: number of milliseconds
863 *
864 * Creates a new [[email protected]] that resolves or rejects with the same
865 * result as @future, unless @msec milliseconds elapses first. If the timeout
866 * elapses first, the returned future rejects with %DEX_ERROR_TIMED_OUT and
867 * discards @future.
868 *
869 * Returns: (transfer full): a [[email protected]]
870 *
871 * Since: 1.2
872 */
873 DexFuture *
874 (dex_future_with_timeout_msec) (DexFuture *future,
875 int msec)
876 {
877 dex_return_error_if_fail (DEX_IS_FUTURE (future));
878
879 return _dex_timeout_new_for_future_msec (future, msec);
880 }
881
882 /**
883 * dex_future_with_timeout: (constructor)
884 * @future: (transfer full): a [[email protected]]
885 * @usec: number of microseconds
886 *
887 * Creates a new [[email protected]] that resolves or rejects with the same
888 * result as @future, unless @usec microseconds elapses first. If the timeout
889 * elapses first, the returned future rejects with %DEX_ERROR_TIMED_OUT and
890 * discards @future.
891 *
892 * Returns: (transfer full): a [[email protected]]
893 *
894 * Since: 1.2
895 */
896 DexFuture *
897 (dex_future_with_timeout) (DexFuture *future,
898 gint64 usec)
899 {
900 dex_return_error_if_fail (DEX_IS_FUTURE (future));
901
902 return _dex_timeout_new_for_future (future, usec);
903 }
904
905 /**
906 * dex_future_with_deadline: (constructor)
907 * @future: (transfer full): a [[email protected]]
908 * @deadline: a deadline in usec in the monotonic clock
909 *
910 * Creates a new [[email protected]] that resolves or rejects with the same
911 * result as @future, unless @deadline is reached first. If the deadline is
912 * reached first, the returned future rejects with %DEX_ERROR_TIMED_OUT and
913 * discards @future.
914 *
915 * Returns: (transfer full): a [[email protected]]
916 *
917 * Since: 1.2
918 */
919 DexFuture *
920 (dex_future_with_deadline) (DexFuture *future,
921 gint64 deadline)
922 {
923 dex_return_error_if_fail (DEX_IS_FUTURE (future));
924
925 return _dex_timeout_new_for_future_deadline (future, deadline);
926 }
927
928 /**
929 * dex_future_anyv: (rename-to dex_future_any) (constructor)
930 * @futures: (array length=n_futures) (transfer none): an array of futures
931 * @n_futures: the number of futures
932 *
933 * Creates a new [[email protected]] that resolves when the first future resolves.
934 *
935 * If all futures reject, then the [[email protected]] returned will also reject.
936 *
937 * Returns: (transfer full): a [[email protected]]
938 */
939 DexFuture *
940 (dex_future_anyv) (DexFuture * const *futures,
941 guint n_futures)
942 {
943 return DEX_FUTURE (dex_future_set_new (futures, n_futures,
944 (DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST |
945 DEX_FUTURE_SET_FLAGS_PROPAGATE_RESOLVE)));
946 }
947
948 /**
949 * dex_future_all_racev: (rename-to dex_future_all_race) (constructor)
950 * @futures: (array length=n_futures) (transfer none): an array of futures
951 * @n_futures: the number of futures
952 *
953 * Creates a new [[email protected]] that resolves when all futures resolve.
954 *
955 * If any future rejects, the resulting [[email protected]] also rejects immediately.
956 *
957 * Returns: (transfer full): a [[email protected]]
958 */
959 DexFuture *
960 (dex_future_all_racev) (DexFuture * const *futures,
961 guint n_futures)
962 {
963 return DEX_FUTURE (dex_future_set_new (futures, n_futures,
964 (DEX_FUTURE_SET_FLAGS_PROPAGATE_FIRST |
965 DEX_FUTURE_SET_FLAGS_PROPAGATE_REJECT)));
966 }
967
968 /**
969 * dex_future_allv: (rename-to dex_future_all) (constructor)
970 * @futures: (array length=n_futures) (transfer none): an array of futures
971 * @n_futures: the number of futures
972 *
973 * Creates a new [[email protected]] that resolves when all futures resolve.
974 *
975 * The resulting [[email protected]] will not resolve or reject until all futures
976 * have either resolved or rejected.
977 *
978 * Returns: (transfer full): a [[email protected]]
979 */
980 DexFuture *
981 (dex_future_allv) (DexFuture * const *futures,
982 guint n_futures)
983 {
984 return DEX_FUTURE (dex_future_set_new (futures, n_futures, DEX_FUTURE_SET_FLAGS_NONE));
985 }
986
987 /**
988 * dex_future_set_static_name: (skip)
989 * @future: a [[email protected]]
990 * @name: the name of the future
991 *
992 * Sets the name of the future with a static/internal string.
993 *
994 * @name will not be copied, so it must be static/internal which can be done
995 * either by using string literals or by using [[email protected]_string].
996 */
997 void
998 dex_future_set_static_name (DexFuture *future,
999 const char *name)
1000 {
1001 g_return_if_fail (DEX_IS_FUTURE (future));
1002
1003 dex_object_lock (future);
1004 future->name = name;
1005 dex_object_unlock (future);
1006 }
1007
1008 const char *
1009 dex_future_get_name (DexFuture *future)
1010 {
1011 const char *name;
1012
1013 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
1014
1015 dex_object_lock (future);
1016 name = future->name;
1017 dex_object_unlock (future);
1018
1019 return name;
1020 }
1021
1022 /**
1023 * dex_future_new_for_value:
1024 * @value: the resolved [[email protected]]
1025 *
1026 * Creates a read-only [[email protected]] that has resolved.
1027 *
1028 * Returns: (transfer full): a [[email protected]]
1029 */
1030 DexFuture *
1031 (dex_future_new_for_value) (const GValue *value)
1032 {
1033 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1034
1035 return dex_static_future_new_resolved (value);
1036 }
1037
1038 /**
1039 * dex_future_new_for_error: (constructor)
1040 * @error: (transfer full): a [[email protected]]
1041 *
1042 * Creates a read-only [[email protected]] that has rejected.
1043 *
1044 * Returns: (transfer full): a [[email protected]]
1045 */
1046 DexFuture *
1047 (dex_future_new_for_error) (GError *error)
1048 {
1049 g_return_val_if_fail (error != NULL, NULL);
1050
1051 return dex_static_future_new_rejected (error);
1052 }
1053
1054 /**
1055 * dex_future_new_for_boolean: (constructor)
1056 * @v_bool: the resolved value for the future
1057 *
1058 * Creates a new [[email protected]] and resolves it with @v_bool.
1059 *
1060 * Returns: (transfer full): a resolved [[email protected]]
1061 */
1062 DexFuture *
1063 (dex_future_new_for_boolean) (gboolean v_bool)
1064 {
1065 if G_UNLIKELY (g_once_init_enter (&static_booleans_init))
1066 {
1067 GValue value = G_VALUE_INIT;
1068
1069 g_value_init (&value, G_TYPE_BOOLEAN);
1070
1071 g_value_set_boolean (&value, FALSE);
1072 static_booleans[FALSE] = dex_static_future_new_resolved (&value);
1073
1074 g_value_set_boolean (&value, TRUE);
1075 static_booleans[TRUE] = dex_static_future_new_resolved (&value);
1076
1077 g_once_init_leave (&static_booleans_init, TRUE);
1078 }
1079
1080 return dex_ref (static_booleans[!!v_bool]);
1081 }
1082
1083 /**
1084 * dex_future_new_for_fd: (constructor)
1085 * @fd: the file-descriptor to take ownership of
1086 *
1087 * Creates a new future that resolves to @fd.
1088 *
1089 * This function takes ownership of @fd.
1090 *
1091 * Returns: (transfer full): a resolved [[email protected]]
1092 *
1093 * Since: 0.10
1094 */
1095 DexFuture *
1096 (dex_future_new_for_fd) (int fd)
1097 {
1098 GValue gvalue = {DEX_TYPE_FD, {{.v_pointer = g_memdup2 (&fd, sizeof fd)}, {.v_int = 0}}};
1099 return dex_static_future_new_steal (&gvalue);
1100 }
1101
1102 /**
1103 * dex_future_new_for_int: (constructor)
1104 * @v_int: the resolved value for the future
1105 *
1106 * Creates a new [[email protected]] and resolves it with @v_int.
1107 *
1108 * Returns: (transfer full): a resolved [[email protected]]
1109 */
1110 DexFuture *
1111 (dex_future_new_for_int) (int v_int)
1112 {
1113 GValue value = G_VALUE_INIT;
1114 DexFuture *future;
1115
1116 g_value_init (&value, G_TYPE_INT);
1117 g_value_set_int (&value, v_int);
1118 future = (dex_future_new_for_value) (&value);
1119 g_value_unset (&value);
1120
1121 return future;
1122 }
1123
1124 /**
1125 * dex_future_new_for_int64: (constructor)
1126 * @v_int64: the resolved value for the future
1127 *
1128 * Creates a new [[email protected]] and resolves it with @v_int64.
1129 *
1130 * Returns: (transfer full): a resolved [[email protected]]
1131 */
1132 DexFuture *
1133 (dex_future_new_for_int64) (gint64 v_int64)
1134 {
1135 GValue value = G_VALUE_INIT;
1136 DexFuture *future;
1137
1138 g_value_init (&value, G_TYPE_INT64);
1139 g_value_set_int64 (&value, v_int64);
1140 future = (dex_future_new_for_value) (&value);
1141 g_value_unset (&value);
1142
1143 return future;
1144 }
1145
1146 /**
1147 * dex_future_new_for_uint64: (constructor)
1148 * @v_uint64: the resolved value for the future
1149 *
1150 * Creates a new [[email protected]] and resolves it with @v_uint64.
1151 *
1152 * Returns: (transfer full): a resolved [[email protected]]
1153 */
1154 DexFuture *
1155 (dex_future_new_for_uint64) (guint64 v_uint64)
1156 {
1157 GValue value = G_VALUE_INIT;
1158 DexFuture *future;
1159
1160 g_value_init (&value, G_TYPE_UINT64);
1161 g_value_set_uint64 (&value, v_uint64);
1162 future = (dex_future_new_for_value) (&value);
1163 g_value_unset (&value);
1164
1165 return future;
1166 }
1167
1168 /**
1169 * dex_future_new_for_float: (constructor)
1170 * @v_float: the resolved value for the future
1171 *
1172 * Creates a new [[email protected]] and resolves it with @v_float.
1173 *
1174 * Returns: (transfer full): a resolved [[email protected]]
1175 */
1176 DexFuture *
1177 (dex_future_new_for_float) (gfloat v_float)
1178 {
1179 GValue value = G_VALUE_INIT;
1180 DexFuture *future;
1181
1182 g_value_init (&value, G_TYPE_FLOAT);
1183 g_value_set_float (&value, v_float);
1184 future = (dex_future_new_for_value) (&value);
1185 g_value_unset (&value);
1186
1187 return future;
1188 }
1189
1190 /**
1191 * dex_future_new_for_double: (constructor)
1192 * @v_double: the resolved value for the future
1193 *
1194 * Creates a new [[email protected]] and resolves it with @v_double.
1195 *
1196 * Returns: (transfer full): a resolved [[email protected]]
1197 */
1198 DexFuture *
1199 (dex_future_new_for_double) (gdouble v_double)
1200 {
1201 GValue value = G_VALUE_INIT;
1202 DexFuture *future;
1203
1204 g_value_init (&value, G_TYPE_DOUBLE);
1205 g_value_set_double (&value, v_double);
1206 future = (dex_future_new_for_value) (&value);
1207 g_value_unset (&value);
1208
1209 return future;
1210 }
1211
1212 /**
1213 * dex_future_new_for_uint: (constructor)
1214 * @v_uint: the resolved value for the future
1215 *
1216 * Creates a new [[email protected]] and resolves it with @v_uint.
1217 *
1218 * Returns: (transfer full): a resolved [[email protected]]
1219 */
1220 DexFuture *
1221 (dex_future_new_for_uint) (guint v_uint)
1222 {
1223 GValue value = G_VALUE_INIT;
1224 DexFuture *future;
1225
1226 g_value_init (&value, G_TYPE_UINT);
1227 g_value_set_uint (&value, v_uint);
1228 future = (dex_future_new_for_value) (&value);
1229 g_value_unset (&value);
1230
1231 return future;
1232 }
1233
1234 /**
1235 * dex_future_new_enum: (constructor)
1236 * @enum_type: a `GType` of %G_TYPE_ENUM
1237 * @enum_value: the enum value for the future
1238 *
1239 * Creates a new [[email protected]] and resolves it with @enum_value.
1240 *
1241 * Returns: (transfer full): a resolved [[email protected]]
1242 */
1243 DexFuture *
1244 (dex_future_new_enum) (GType enum_type,
1245 guint enum_value)
1246 {
1247 GValue gvalue = G_VALUE_INIT;
1248 DexFuture *future;
1249
1250 dex_return_error_if_fail (G_TYPE_IS_ENUM (enum_type));
1251
1252 g_value_init (&gvalue, enum_type);
1253 g_value_set_enum (&gvalue, enum_value);
1254 future = (dex_future_new_for_value) (&gvalue);
1255 g_value_unset (&gvalue);
1256
1257 return future;
1258 }
1259
1260 /**
1261 * dex_future_new_for_string: (constructor)
1262 * @string: the resolved value for the future
1263 *
1264 * Creates a new [[email protected]] and resolves it with @string.
1265 *
1266 * Returns: (transfer full): a resolved [[email protected]]
1267 */
1268 DexFuture *
1269 (dex_future_new_for_string) (const char *string)
1270 {
1271 return (dex_future_new_take_string) (g_strdup (string));
1272 }
1273
1274 /**
1275 * dex_future_new_take_string: (constructor)
1276 * @string: (transfer full): the resolved value for the future
1277 *
1278 * Creates a new [[email protected]] and resolves it with @string.
1279 *
1280 * Returns: (transfer full): a resolved [[email protected]]
1281 */
1282 DexFuture *
1283 (dex_future_new_take_string) (char *string)
1284 {
1285 GValue value = G_VALUE_INIT;
1286 DexFuture *future;
1287
1288 g_value_init (&value, G_TYPE_STRING);
1289 g_value_take_string (&value, string);
1290 future = (dex_future_new_for_value) (&value);
1291 g_value_unset (&value);
1292
1293 return future;
1294 }
1295
1296 /**
1297 * dex_future_new_take_boxed: (constructor) (skip)
1298 * @boxed_type: the GBoxed-based type
1299 * @value: (transfer full): the value for the boxed type
1300 *
1301 * Creates a new [[email protected]] that is resolved with @value.
1302 *
1303 * Returns: (transfer full): a resolved [[email protected]]
1304 */
1305 DexFuture *
1306 (dex_future_new_take_boxed) (GType boxed_type,
1307 gpointer value)
1308 {
1309 GValue gvalue = G_VALUE_INIT;
1310 DexFuture *ret;
1311
1312 g_return_val_if_fail (G_TYPE_FUNDAMENTAL (boxed_type) == G_TYPE_BOXED, NULL);
1313
1314 g_value_init (&gvalue, boxed_type);
1315 g_value_take_boxed (&gvalue, value);
1316 ret = dex_future_new_for_value (&gvalue);
1317 g_value_unset (&gvalue);
1318
1319 return ret;
1320 }
1321
1322 /**
1323 * dex_future_new_take_variant: (constructor) (skip)
1324 * @v_variant: (transfer full): the variant to take ownership of
1325 *
1326 * Creates a new [[email protected]] that is resolved with @v_variant.
1327 *
1328 * Returns: (transfer full): a resolved [[email protected]]
1329 */
1330 DexFuture *
1331 (dex_future_new_take_variant) (GVariant *v_variant)
1332 {
1333 GValue gvalue = G_VALUE_INIT;
1334 DexFuture *ret;
1335
1336 g_value_init (&gvalue, G_TYPE_VARIANT);
1337 g_value_take_variant (&gvalue, v_variant);
1338 ret = dex_future_new_for_value (&gvalue);
1339 g_value_unset (&gvalue);
1340
1341 return ret;
1342 }
1343
1344 /**
1345 * dex_future_new_for_pointer: (constructor)
1346 * @pointer: the resolved future value as a pointer
1347 *
1348 * Creates a new [[email protected]] that is resolved with @pointer as a %G_TYPE_POINTER.
1349 *
1350 * Returns: (transfer full): a resolved [[email protected]]
1351 */
1352 DexFuture *
1353 (dex_future_new_for_pointer) (gpointer pointer)
1354 {
1355 GValue gvalue = G_VALUE_INIT;
1356 DexFuture *ret;
1357
1358 g_value_init (&gvalue, G_TYPE_POINTER);
1359 g_value_set_pointer (&gvalue, pointer);
1360 ret = dex_future_new_for_value (&gvalue);
1361 g_value_unset (&gvalue);
1362
1363 return ret;
1364 }
1365
1366 /**
1367 * dex_future_new_for_object: (constructor)
1368 * @value: (type GObject): the value
1369 *
1370 * Creates a new [[email protected]] that is resolved with @value.
1371 *
1372 * Returns: (transfer full): a resolved [[email protected]]
1373 */
1374 DexFuture *
1375 (dex_future_new_for_object) (gpointer value)
1376 {
1377 GValue gvalue = G_VALUE_INIT;
1378 DexFuture *ret;
1379
1380 g_return_val_if_fail (G_IS_OBJECT (value), NULL);
1381
1382 g_value_init (&gvalue, G_OBJECT_TYPE (value));
1383 g_value_set_object (&gvalue, value);
1384 ret = dex_future_new_for_value (&gvalue);
1385 g_value_unset (&gvalue);
1386
1387 return ret;
1388 }
1389
1390 /**
1391 * dex_future_new_take_object: (constructor)
1392 * @value: (transfer full) (type GObject) (nullable): the value
1393 *
1394 * Creates a new [[email protected]] that is resolved with @value.
1395 *
1396 * Returns: (transfer full): a resolved [[email protected]]
1397 */
1398 DexFuture *
1399 (dex_future_new_take_object) (gpointer value)
1400 {
1401 GValue gvalue = G_VALUE_INIT;
1402 DexFuture *ret;
1403
1404 g_return_val_if_fail (!value || G_IS_OBJECT (value), NULL);
1405
1406 g_value_init (&gvalue, value ? G_OBJECT_TYPE (value) : G_TYPE_OBJECT);
1407 g_value_take_object (&gvalue, value);
1408 ret = dex_future_new_for_value (&gvalue);
1409 g_value_unset (&gvalue);
1410
1411 return ret;
1412 }
1413
1414 /**
1415 * dex_future_new_reject: (constructor)
1416 * @domain: the error domain
1417 * @error_code: the error code
1418 * @format: a printf-style format string
1419 *
1420 * Creates a new [[email protected]] that is rejected.
1421 *
1422 * Returns: (transfer full): a new [[email protected]]
1423 */
1424 DexFuture *
1425 (dex_future_new_reject) (GQuark domain,
1426 int error_code,
1427 const char *format,
1428 ...)
1429 {
1430 GError *error;
1431 va_list args;
1432
1433 va_start (args, format);
1434 error = g_error_new_valist (domain, error_code, format, args);
1435 va_end (args);
1436
1437 g_return_val_if_fail (error != NULL, NULL);
1438
1439 return dex_future_new_for_error (error);
1440 }
1441
1442 /**
1443 * dex_future_new_for_errno:
1444 * @errno_: the `errno` to use for rejection
1445 *
1446 * Creates a new rejected future using @errno_ as the value
1447 * of errno for the GError.
1448 *
1449 * The resulting error domain will be %G_IO_ERROR.
1450 *
1451 * Returns: (transfer full): a rejected [[email protected]].
1452 *
1453 * Since: 0.4
1454 */
1455 DexFuture *
1456 (dex_future_new_for_errno) (int errno_)
1457 {
1458 /* NOTE: We might be able to cache some common rejections
1459 * by errno to avoid re-creating them.
1460 */
1461
1462 return dex_future_new_for_error (g_error_new_literal (G_IO_ERROR,
1463 g_io_error_from_errno (errno_),
1464 g_strerror (errno_)));
1465 }
1466
1467 /**
1468 * dex_future_new_infinite:
1469 *
1470 * Creates an infinite future that will never resolve or reject. This can
1471 * be useful when you want to mock a situation of "run forever" unless
1472 * another future rejects or resolves.
1473 *
1474 * Returns: (transfer full): a [[email protected]] that will never complete or reject
1475 *
1476 * Since: 0.4
1477 */
1478 DexFuture *
1479 dex_future_new_infinite (void)
1480 {
1481 return dex_infinite_new ();
1482 }
1483
1484 static const GValue *
1485 dex_await_check (DexFuture *future,
1486 GType type,
1487 GError **error)
1488 {
1489 const GValue *value;
1490
1491 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
1492
1493 if ((value = dex_await_borrowed (future, error)))
1494 {
1495 if (!G_VALUE_HOLDS (value, type))
1496 {
1497 g_set_error (error,
1498 DEX_ERROR,
1499 DEX_ERROR_TYPE_MISMATCH,
1500 "Got type %s, expected %s",
1501 G_VALUE_TYPE_NAME (value),
1502 g_type_name (type));
1503 return NULL;
1504 }
1505 }
1506
1507 return value;
1508 }
1509
1510 /**
1511 * dex_await_pointer: (method)
1512 * @future: (transfer full): a [[email protected]]
1513 * @error: a location for a [[email protected]]
1514 *
1515 * Calls [[email protected]] and returns the value of
1516 * [[email protected]_pointer], otherwise @error is set if the future
1517 * rejected.
1518 *
1519 * Returns: (nullable): a pointer or %NULL
1520 */
1521 gpointer
1522 dex_await_pointer (DexFuture *future,
1523 GError **error)
1524 {
1525 const GValue *value;
1526 gpointer ret = NULL;
1527
1528 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
1529
1530 if ((value = dex_await_check (future, G_TYPE_POINTER, error)))
1531 ret = g_value_get_pointer (value);
1532
1533 dex_unref (future);
1534
1535 return ret;
1536 }
1537
1538 /**
1539 * dex_await_fd: (method)
1540 * @future: (transfer full): a [[email protected]]
1541 * @error: a location for a [[email protected]]
1542 *
1543 * Awaits on @future and returns the resulting file-descriptor.
1544 *
1545 * The resolved value must be of type %DEX_TYPE_FD or @error is set.
1546 *
1547 * Returns: a valid file descriptor or -1. you may get -1 without
1548 * error being set if there was no rejected future.
1549 *
1550 * Since: 0.10
1551 */
1552 int
1553 dex_await_fd (DexFuture *future,
1554 GError **error)
1555 {
1556 const GValue *value;
1557 int ret = -1;
1558
1559 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1560
1561 if ((value = dex_await_check (future, DEX_TYPE_FD, error)))
1562 ret = dex_fd_steal (g_value_get_boxed (value));
1563
1564 dex_unref (future);
1565
1566 return ret;
1567 }
1568
1569 /**
1570 * dex_await_int: (method)
1571 * @future: (transfer full): a [[email protected]]
1572 * @error: a location for a [[email protected]]
1573 *
1574 * Awaits on @future and returns the result as an int.
1575 *
1576 * The resolved value must be of type %G_TYPE_INT or @error is set.
1577 *
1578 * Returns: an int, or 0 in case of failure and @error is set.
1579 */
1580 int
1581 dex_await_int (DexFuture *future,
1582 GError **error)
1583 {
1584 const GValue *value;
1585 int ret = 0;
1586
1587 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1588
1589 if ((value = dex_await_check (future, G_TYPE_INT, error)))
1590 ret = g_value_get_int (value);
1591
1592 dex_unref (future);
1593
1594 return ret;
1595 }
1596
1597 /**
1598 * dex_await_uint: (method)
1599 * @future: (transfer full): a [[email protected]]
1600 * @error: a location for a [[email protected]]
1601 *
1602 * Awaits on @future and returns the result as an uint.
1603 *
1604 * The resolved value must be of type %G_TYPE_UINT or @error is set.
1605 *
1606 * Returns: an uint, or 0 in case of failure and @error is set.
1607 */
1608 guint
1609 dex_await_uint (DexFuture *future,
1610 GError **error)
1611 {
1612 const GValue *value;
1613 guint ret = 0;
1614
1615 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1616
1617 if ((value = dex_await_check (future, G_TYPE_UINT, error)))
1618 ret = g_value_get_uint (value);
1619
1620 dex_unref (future);
1621
1622 return ret;
1623 }
1624
1625 /**
1626 * dex_await_int64: (method)
1627 * @future: (transfer full): a [[email protected]]
1628 * @error: a location for a [[email protected]]
1629 *
1630 * Awaits on @future and returns the result as an int64.
1631 *
1632 * The resolved value must be of type %G_TYPE_INT64 or @error is set.
1633 *
1634 * Returns: an int64, or 0 in case of failure and @error is set.
1635 */
1636 gint64
1637 dex_await_int64 (DexFuture *future,
1638 GError **error)
1639 {
1640 const GValue *value;
1641 gint64 ret = 0;
1642
1643 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1644
1645 if ((value = dex_await_check (future, G_TYPE_INT64, error)))
1646 ret = g_value_get_int64 (value);
1647
1648 dex_unref (future);
1649
1650 return ret;
1651 }
1652
1653 /**
1654 * dex_await_uint64: (method)
1655 * @future: (transfer full): a [[email protected]]
1656 * @error: a location for a [[email protected]]
1657 *
1658 * Awaits on @future and returns the result as an uint64.
1659 *
1660 * The resolved value must be of type %G_TYPE_UINT64 or @error is set.
1661 *
1662 * Returns: an uint64, or 0 in case of failure and @error is set.
1663 */
1664 guint64
1665 dex_await_uint64 (DexFuture *future,
1666 GError **error)
1667 {
1668 const GValue *value;
1669 guint64 ret = 0;
1670
1671 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1672
1673 if ((value = dex_await_check (future, G_TYPE_UINT64, error)))
1674 ret = g_value_get_uint64 (value);
1675
1676 dex_unref (future);
1677
1678 return ret;
1679 }
1680
1681 /**
1682 * dex_await_double: (method)
1683 * @future: (transfer full): a [[email protected]]
1684 * @error: a location for a [[email protected]]
1685 *
1686 * Awaits on @future and returns the result as an double.
1687 *
1688 * The resolved value must be of type %G_TYPE_DOUBLE or @error is set.
1689 *
1690 * Returns: an double, or 0 in case of failure and @error is set.
1691 */
1692 double
1693 dex_await_double (DexFuture *future,
1694 GError **error)
1695 {
1696 const GValue *value;
1697 double ret = 0;
1698
1699 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1700
1701 if ((value = dex_await_check (future, G_TYPE_DOUBLE, error)))
1702 ret = g_value_get_double (value);
1703
1704 dex_unref (future);
1705
1706 return ret;
1707 }
1708
1709 /**
1710 * dex_await_float: (method)
1711 * @future: (transfer full): a [[email protected]]
1712 * @error: a location for a [[email protected]]
1713 *
1714 * Awaits on @future and returns the result as an float.
1715 *
1716 * The resolved value must be of type %G_TYPE_FLOAT or @error is set.
1717 *
1718 * Returns: an float, or 0 in case of failure and @error is set.
1719 */
1720 float
1721 dex_await_float (DexFuture *future,
1722 GError **error)
1723 {
1724 const GValue *value;
1725 float ret = 0;
1726
1727 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1728
1729 if ((value = dex_await_check (future, G_TYPE_FLOAT, error)))
1730 ret = g_value_get_float (value);
1731
1732 dex_unref (future);
1733
1734 return ret;
1735 }
1736
1737 /**
1738 * dex_await_boxed: (method)
1739 * @future: (transfer full): a [[email protected]]
1740 * @error: a location for a [[email protected]]
1741 *
1742 * Awaits on @future and returns the %G_TYPE_BOXED based result.
1743 *
1744 * Returns: (transfer full): the boxed result, or %NULL and @error is set.
1745 */
1746 gpointer
1747 dex_await_boxed (DexFuture *future,
1748 GError **error)
1749 {
1750 const GValue *value;
1751 gpointer ret = NULL;
1752
1753 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1754
1755 if ((value = dex_await_check (future, G_TYPE_BOXED, error)))
1756 ret = g_value_dup_boxed (value);
1757
1758 dex_unref (future);
1759
1760 return ret;
1761 }
1762
1763 /**
1764 * dex_await_variant: (method)
1765 * @future: (transfer full): a [[email protected]]
1766 * @error: a location for a [[email protected]]
1767 *
1768 * Awaits on @future and returns the %G_TYPE_VARIANT based result.
1769 *
1770 * Returns: (transfer full): the variant result, or %NULL and @error is set.
1771 *
1772 * Since: 0.4
1773 */
1774 GVariant *
1775 dex_await_variant (DexFuture *future,
1776 GError **error)
1777 {
1778 const GValue *value;
1779 GVariant *ret = NULL;
1780
1781 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1782
1783 if ((value = dex_await_check (future, G_TYPE_VARIANT, error)))
1784 ret = g_value_dup_variant (value);
1785
1786 dex_unref (future);
1787
1788 return ret;
1789 }
1790
1791 /**
1792 * dex_await_object: (method)
1793 * @future: (transfer full): a [[email protected]]
1794 * @error: a location for a [[email protected]]
1795 *
1796 * Awaits on @future and returns the [[email protected]]-based result.
1797 *
1798 * Returns: (type GObject) (transfer full): the object, or %NULL and @error is set.
1799 */
1800 gpointer
1801 dex_await_object (DexFuture *future,
1802 GError **error)
1803 {
1804 const GValue *value;
1805 gpointer ret = NULL;
1806
1807 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
1808
1809 if ((value = dex_await_check (future, G_TYPE_OBJECT, error)))
1810 ret = g_value_dup_object (value);
1811
1812 dex_unref (future);
1813
1814 return ret;
1815 }
1816
1817 /**
1818 * dex_await_boolean: (method)
1819 * @future: (transfer full): a [[email protected]]
1820 * @error: a location for a [[email protected]]
1821 *
1822 * Awaits on @future and returns the gboolean result.
1823 *
1824 * If the result is not a `gboolean`, @error is set.
1825 *
1826 * Returns: the `gboolean`, or %FALSE and @error is set
1827 */
1828 gboolean
1829 dex_await_boolean (DexFuture *future,
1830 GError **error)
1831 {
1832 const GValue *value;
1833 gboolean ret = FALSE;
1834
1835 g_return_val_if_fail (DEX_IS_FUTURE (future), FALSE);
1836
1837 if ((value = dex_await_check (future, G_TYPE_BOOLEAN, error)))
1838 ret = g_value_get_boolean (value);
1839
1840 dex_unref (future);
1841
1842 return ret;
1843 }
1844
1845 /**
1846 * dex_await_string: (method)
1847 * @future: (transfer full): a [[email protected]]
1848 * @error: a location for a [[email protected]]
1849 *
1850 * Awaits on @future and returns the string result.
1851 *
1852 * If the result is not a %G_TYPE_STRING, @error is set.
1853 *
1854 * Returns: (transfer full) (nullable): the string or %NULL and @error is set
1855 */
1856 char *
1857 dex_await_string (DexFuture *future,
1858 GError **error)
1859 {
1860 const GValue *value;
1861 char *ret = NULL;
1862
1863 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
1864
1865 if ((value = dex_await_check (future, G_TYPE_STRING, error)))
1866 ret = g_value_dup_string (value);
1867
1868 dex_unref (future);
1869
1870 return ret;
1871 }
1872
1873 /**
1874 * dex_await_enum: (method)
1875 * @future: (transfer full): a [[email protected]]
1876 * @error: a location for a [[email protected]]
1877 *
1878 * Awaits on @future and returns the enum result.
1879 *
1880 * If the result is not a %G_TYPE_ENUM, @error is set.
1881 *
1882 * Returns: the enum or 0 and @error is set.
1883 */
1884 guint
1885 dex_await_enum (DexFuture *future,
1886 GError **error)
1887 {
1888 const GValue *value;
1889 guint ret = 0;
1890
1891 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1892
1893 if ((value = dex_await_check (future, G_TYPE_ENUM, error)))
1894 ret = g_value_get_enum (value);
1895
1896 dex_unref (future);
1897
1898 return ret;
1899 }
1900
1901 /**
1902 * dex_await_flags: (method)
1903 * @future: (transfer full): a [[email protected]]
1904 * @error: a location for a [[email protected]]
1905 *
1906 * Awaits on @future and returns the flags result.
1907 *
1908 * If the result is not a %G_TYPE_FLAGS, @error is set.
1909 *
1910 * Returns: the flags or 0 and @error is set.
1911 */
1912 guint
1913 dex_await_flags (DexFuture *future,
1914 GError **error)
1915 {
1916 const GValue *value;
1917 guint ret = 0;
1918
1919 g_return_val_if_fail (DEX_IS_FUTURE (future), 0);
1920
1921 if ((value = dex_await_check (future, G_TYPE_FLAGS, error)))
1922 ret = g_value_get_flags (value);
1923
1924 dex_unref (future);
1925
1926 return ret;
1927 }
1928
1929 static DexFuture **
1930 pfuture_ref (DexFuture **pfuture)
1931 {
1932 return g_atomic_rc_box_acquire (pfuture);
1933 }
1934
1935 static void
1936 pfuture_finalize (DexFuture **pfuture)
1937 {
1938 g_assert (pfuture != NULL);
1939 g_assert (*pfuture != NULL);
1940 g_assert (DEX_IS_FUTURE (*pfuture));
1941
1942 dex_unref (*pfuture);
1943 }
1944
1945 static void
1946 pfuture_unref (DexFuture **pfuture)
1947 {
1948 g_atomic_rc_box_release_full (pfuture, (GDestroyNotify)pfuture_finalize);
1949 }
1950
1951 static DexFuture *
1952 dex_future_disown_cb (DexFuture *resolved,
1953 gpointer user_data)
1954 {
1955 return NULL;
1956 }
1957
1958 /**
1959 * dex_future_disown:
1960 * @future: (transfer full): a [[email protected]]
1961 *
1962 * Disowns a future, allowing it to run to completion even though there may
1963 * be no observer interested in the futures completion or rejection.
1964 *
1965 * Since: 0.4
1966 */
1967 void
1968 dex_future_disown (DexFuture *future)
1969 {
1970 DexFuture **pfuture;
1971
1972 g_return_if_fail (DEX_IS_FUTURE (future));
1973
1974 pfuture = g_atomic_rc_box_new0 (DexFuture *);
1975 *pfuture = dex_future_finally (future,
1976 dex_future_disown_cb,
1977 pfuture_ref (pfuture),
1978 (GDestroyNotify)pfuture_unref);
1979 pfuture_unref (pfuture);
1980 }
1981
1982 void
1983 dex_future_disown_full (DexFuture *future,
1984 DexScheduler *scheduler)
1985 {
1986 DexFuture **pfuture;
1987
1988 g_return_if_fail (DEX_IS_FUTURE (future));
1989
1990 pfuture = g_atomic_rc_box_new0 (DexFuture *);
1991 *pfuture = dex_block_new (future,
1992 scheduler,
1993 DEX_BLOCK_KIND_FINALLY,
1994 dex_future_disown_cb,
1995 pfuture_ref (pfuture),
1996 (GDestroyNotify)pfuture_unref);
1997 pfuture_unref (pfuture);
1998 }