summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2017-08-26 19:04:43 -0300
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2017-08-27 11:35:38 -0300
commit7dc41ab0e50ccbf6b2303ad7ab7b95bf773f7971 (patch)
tree45c46f88903f2a9f325686f93f8ed10fd11db1a7
parent1a0b921789d0671e7d493c53b059f1ca5ea426ad (diff)
Eina_Future: add eina_future_resolved()
This is a helper that creates a promise, then a future and immediately resolves the promise, this will let the future be dispatched as usual, from a clean main loop context.
-rw-r--r--src/lib/eina/eina_promise.c26
-rw-r--r--src/lib/eina/eina_promise.h37
2 files changed, 61 insertions, 2 deletions
diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c
index 2cad75b15c..588c94ff2b 100644
--- a/src/lib/eina/eina_promise.c
+++ b/src/lib/eina/eina_promise.c
@@ -611,7 +611,7 @@ _future_proxy(void *data, const Eina_Value v,
611} 611}
612 612
613static void 613static void
614_proxy_cancel(void *data EINA_UNUSED, const Eina_Promise *dead_ptr EINA_UNUSED) 614_dummy_cancel(void *data EINA_UNUSED, const Eina_Promise *dead_ptr EINA_UNUSED)
615{ 615{
616} 616}
617 617
@@ -631,7 +631,7 @@ eina_future_as_value(Eina_Future *f)
631 Eina_Future *r_future; 631 Eina_Future *r_future;
632 632
633 EINA_FUTURE_CHECK_RETURN_VAL(f, v); 633 EINA_FUTURE_CHECK_RETURN_VAL(f, v);
634 p = eina_promise_new(_scheduler_get(f), _proxy_cancel, NULL); 634 p = eina_promise_new(_scheduler_get(f), _dummy_cancel, NULL);
635 EINA_SAFETY_ON_NULL_GOTO(p, err_promise); 635 EINA_SAFETY_ON_NULL_GOTO(p, err_promise);
636 r_future = eina_future_then(f, _future_proxy, p); 636 r_future = eina_future_then(f, _future_proxy, p);
637 //If eina_future_then() fails f will be cancelled 637 //If eina_future_then() fails f will be cancelled
@@ -804,6 +804,28 @@ _eina_future_then(Eina_Future *prev, const Eina_Future_Desc desc)
804} 804}
805 805
806EAPI Eina_Future * 806EAPI Eina_Future *
807eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value)
808{
809 Eina_Promise *p;
810 Eina_Future *f;
811
812 EINA_SAFETY_ON_NULL_GOTO(scheduler, error);
813
814 p = eina_promise_new(scheduler, _dummy_cancel, NULL);
815 EINA_SAFETY_ON_NULL_GOTO(p, error);
816
817 f = eina_future_new(p);
818 EINA_SAFETY_ON_NULL_GOTO(f, error);
819
820 eina_promise_resolve(p, value);
821 return f;
822
823 error:
824 eina_value_flush(&value);
825 return NULL;
826}
827
828EAPI Eina_Future *
807eina_future_then_from_desc(Eina_Future *prev, const Eina_Future_Desc desc) 829eina_future_then_from_desc(Eina_Future *prev, const Eina_Future_Desc desc)
808{ 830{
809 EINA_FUTURE_CHECK_GOTO(prev, err_future); 831 EINA_FUTURE_CHECK_GOTO(prev, err_future);
diff --git a/src/lib/eina/eina_promise.h b/src/lib/eina/eina_promise.h
index 568dbb7f1c..febbeef323 100644
--- a/src/lib/eina/eina_promise.h
+++ b/src/lib/eina/eina_promise.h
@@ -477,6 +477,10 @@ struct _Eina_Future_Desc {
477 * } 477 * }
478 * @endcode 478 * @endcode
479 * 479 *
480 * If you already have a value and want to create a future that will
481 * resolve to it directly use the eina_future_resolved(), it has the
482 * same effect as creating a promise and immediately resolving it.
483 *
480 * @param cancel_cb A callback used to inform that the promise was canceled. Use 484 * @param cancel_cb A callback used to inform that the promise was canceled. Use
481 * this callback to @c free @p data. @p cancel_cb must not be @c NULL ! 485 * this callback to @c free @p data. @p cancel_cb must not be @c NULL !
482 * @param data Data to @p cancel_cb. 486 * @param data Data to @p cancel_cb.
@@ -675,6 +679,39 @@ EAPI Eina_Value eina_future_as_value(Eina_Future *f);
675 * @see eina_future_cancel() 679 * @see eina_future_cancel()
676 */ 680 */
677EAPI Eina_Future *eina_future_new(Eina_Promise *p); 681EAPI Eina_Future *eina_future_new(Eina_Promise *p);
682
683/**
684 * Creates a new future that is already resolved to a value.
685 *
686 * This function creates a new future with an already known value,
687 * that will be resolved and dispatched by the given @a scheduler as
688 * usual.
689 *
690 * This is a helper that behaves the same as eina_promise_new()
691 * followed by eina_future_new() and then eina_promise_resolve().
692 *
693 * Futures can also be canceled using eina_future_cancel(), which will
694 * cause the whole chain to be cancelled alongside with any pending
695 * promise.
696 *
697 * @param scheduler The scheduler to use.
698 * @param value The value to be delivered. Note that the value
699 * contents must survive this function scope, that is, do @b not use
700 * stack allocated blobs, arrays, structures or types that keeps
701 * references to memory you give. Values will be automatically cleaned
702 * up using @c eina_value_flush() once they are unused (no more future
703 * or futures returned a new value).
704 *
705 * @return The future or @c NULL on error.
706 *
707 * @see eina_promise_new()
708 * @see eina_future_new()
709 * @see eina_promise_reject()
710 * @see eina_promise_resolve()
711 * @see eina_future_cancel()
712 */
713EAPI Eina_Future *eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value);
714
678/** 715/**
679 * Register an Eina_Future_Desc to be used when the future is resolve/rejected. 716 * Register an Eina_Future_Desc to be used when the future is resolve/rejected.
680 * 717 *