summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-25 18:14:29 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-25 21:32:03 -0300
commitde9be13d458f2739a536e5863aa8caddd3f7f715 (patch)
tree7e28d9dba5336f02ad148af679e7208377fae9f0 /src
parent0eacdc03da738bebe03ac22fe377836751877345 (diff)
eina: Add promise parameter to then calllbacks
Added promise parameter to then callbacks so callbacks can steal ownership of the value from the promise.
Diffstat (limited to 'src')
-rw-r--r--src/lib/ecore/ecore_thread_promise.c9
-rw-r--r--src/lib/eina/eina_promise.c30
-rw-r--r--src/lib/eina/eina_promise.h25
-rw-r--r--src/lib/elementary/elm_view_form.c8
-rw-r--r--src/tests/ecore/ecore_test_promise.c14
-rw-r--r--src/tests/eina/eina_test_promise.c13
6 files changed, 72 insertions, 27 deletions
diff --git a/src/lib/ecore/ecore_thread_promise.c b/src/lib/ecore/ecore_thread_promise.c
index 1dea1bc..caf312c 100644
--- a/src/lib/ecore/ecore_thread_promise.c
+++ b/src/lib/ecore/ecore_thread_promise.c
@@ -47,7 +47,7 @@ static void _ecore_promise_ref_update(_Ecore_Thread_Promise_Owner* p)
47 } 47 }
48} 48}
49 49
50static void _ecore_promise_thread_release_ref(void* data, void* value EINA_UNUSED) 50static void _ecore_promise_thread_release_ref(void* data, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
51{ 51{
52 _Ecore_Thread_Promise_Owner* p = data; 52 _Ecore_Thread_Promise_Owner* p = data;
53 p->ref_count -= p->then_count; 53 p->ref_count -= p->then_count;
@@ -197,6 +197,11 @@ static size_t _ecore_promise_value_size_get(Eina_Promise const* promise)
197 _Ecore_Thread_Promise_Owner* v = ECORE_PROMISE_GET_OWNER(promise); 197 _Ecore_Thread_Promise_Owner* v = ECORE_PROMISE_GET_OWNER(promise);
198 return v->eina_promise->value_size_get(v->eina_promise); 198 return v->eina_promise->value_size_get(v->eina_promise);
199} 199}
200static void* _ecore_promise_release_value_ownership(Eina_Promise* promise)
201{
202 _Ecore_Thread_Promise_Owner* v = ECORE_PROMISE_GET_OWNER(promise);
203 return v->eina_promise->release_value_ownership(v->eina_promise);
204}
200 205
201Ecore_Thread* ecore_thread_promise_run(Ecore_Thread_Promise_Cb func_blocking, 206Ecore_Thread* ecore_thread_promise_run(Ecore_Thread_Promise_Cb func_blocking,
202 Ecore_Thread_Promise_Cb func_cancel, 207 Ecore_Thread_Promise_Cb func_cancel,
@@ -230,6 +235,8 @@ Ecore_Thread* ecore_thread_promise_run(Ecore_Thread_Promise_Cb func_blocking,
230 priv->promise_vtable.unref = EINA_FUNC_PROMISE_UNREF(&_ecore_promise_unref); 235 priv->promise_vtable.unref = EINA_FUNC_PROMISE_UNREF(&_ecore_promise_unref);
231 priv->promise_vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(&_ecore_promise_value_size_get); 236 priv->promise_vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(&_ecore_promise_value_size_get);
232 priv->promise_vtable.buffer_get = EINA_FUNC_PROMISE_BUFFER_GET(&_ecore_promise_buffer_get); 237 priv->promise_vtable.buffer_get = EINA_FUNC_PROMISE_BUFFER_GET(&_ecore_promise_buffer_get);
238 priv->promise_vtable.release_value_ownership = EINA_FUNC_PROMISE_RELEASE_VALUE_OWNERSHIP
239 (&_ecore_promise_release_value_ownership);
233 240
234 priv->thread_callback_data.data = data; 241 priv->thread_callback_data.data = data;
235 priv->thread_callback_data.func_blocking = func_blocking; 242 priv->thread_callback_data.func_blocking = func_blocking;
diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c
index 93e7094..e8ef06d 100644
--- a/src/lib/eina/eina_promise.c
+++ b/src/lib/eina/eina_promise.c
@@ -169,11 +169,11 @@ _eina_promise_then_calls(_Eina_Promise_Default_Owner* promise)
169 if (error) 169 if (error)
170 { 170 {
171 if (callback->error_cb) 171 if (callback->error_cb)
172 (*callback->error_cb)(callback->data, &promise->promise.error); 172 (*callback->error_cb)(callback->data, promise->promise.error, &promise->promise.vtable);
173 } 173 }
174 else if (callback->callback) 174 else if (callback->callback)
175 { 175 {
176 (*callback->callback)(callback->data, &promise->value[0]); 176 (*callback->callback)(callback->data, &promise->value[0], &promise->promise.vtable);
177 } 177 }
178 free(callback); 178 free(callback);
179 _eina_promise_unref(&promise->promise); 179 _eina_promise_unref(&promise->promise);
@@ -240,8 +240,23 @@ _eina_promise_value_get(_Eina_Promise_Default const* p)
240 } 240 }
241} 241}
242 242
243static void *
244_eina_promise_release_value_ownership(_Eina_Promise_Default* p)
245{
246 _Eina_Promise_Default_Owner* promise = EINA_PROMISE_GET_OWNER(p);
247 if (promise->promise.has_finished && !promise->promise.has_errored)
248 {
249 promise->promise.value_free_cb = NULL;
250 return (void*)&promise->value[0];
251 }
252 else
253 {
254 return NULL;
255 }
256}
257
243static void 258static void
244_eina_promise_owner_value_set(_Eina_Promise_Default_Owner* promise, void* data, Eina_Promise_Free_Cb free) 259_eina_promise_owner_value_set(_Eina_Promise_Default_Owner* promise, const void* data, Eina_Promise_Free_Cb free)
245{ 260{
246 if (data && promise->promise.value_size) 261 if (data && promise->promise.value_size)
247 { 262 {
@@ -481,6 +496,7 @@ eina_promise_default_add(int value_size)
481 p->promise.vtable.ref = EINA_FUNC_PROMISE_REF(_eina_promise_ref); 496 p->promise.vtable.ref = EINA_FUNC_PROMISE_REF(_eina_promise_ref);
482 p->promise.vtable.unref = EINA_FUNC_PROMISE_UNREF(_eina_promise_unref); 497 p->promise.vtable.unref = EINA_FUNC_PROMISE_UNREF(_eina_promise_unref);
483 p->promise.vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(_eina_promise_value_size_get); 498 p->promise.vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(_eina_promise_value_size_get);
499 p->promise.vtable.release_value_ownership = EINA_FUNC_PROMISE_RELEASE_VALUE_OWNERSHIP(_eina_promise_release_value_ownership);
484 p->promise.has_finished = p->promise.has_errored = 500 p->promise.has_finished = p->promise.has_errored =
485 p->promise.is_cancelled = p->promise.is_manual_then = EINA_FALSE; 501 p->promise.is_cancelled = p->promise.is_manual_then = EINA_FALSE;
486 p->promise.is_first_then = EINA_TRUE; 502 p->promise.is_first_then = EINA_TRUE;
@@ -781,7 +797,7 @@ eina_promise_then(Eina_Promise* promise, Eina_Promise_Cb callback,
781} 797}
782 798
783EAPI void 799EAPI void
784eina_promise_owner_value_set(Eina_Promise_Owner* promise, void* value, Eina_Promise_Free_Cb free) 800eina_promise_owner_value_set(Eina_Promise_Owner* promise, const void* value, Eina_Promise_Free_Cb free)
785{ 801{
786 promise->value_set(promise, value, free); 802 promise->value_set(promise, value, free);
787} 803}
@@ -847,6 +863,12 @@ eina_promise_buffer_get(Eina_Promise* promise)
847 return promise->buffer_get(promise); 863 return promise->buffer_get(promise);
848} 864}
849 865
866EAPI void *
867eina_promise_release_value_ownership(Eina_Promise* promise)
868{
869 return promise->release_value_ownership(promise);
870}
871
850EAPI size_t 872EAPI size_t
851eina_promise_value_size_get(Eina_Promise const* promise) 873eina_promise_value_size_get(Eina_Promise const* promise)
852{ 874{
diff --git a/src/lib/eina/eina_promise.h b/src/lib/eina/eina_promise.h
index 6921c6a..8bba454 100644
--- a/src/lib/eina/eina_promise.h
+++ b/src/lib/eina/eina_promise.h
@@ -29,12 +29,12 @@ typedef void(*Eina_Promise_Progress_Notify_Cb)(void* data, Eina_Promise_Owner* p
29/* 29/*
30 * @brief Function callback type for when using eina_promise_then 30 * @brief Function callback type for when using eina_promise_then
31 */ 31 */
32typedef void(*Eina_Promise_Cb)(void* data, void* value); 32typedef void(*Eina_Promise_Cb)(void* data, void* value, Eina_Promise* promise);
33 33
34/* 34/*
35 * @brief Function callback type for when using eina_promise_then 35 * @brief Function callback type for when using eina_promise_then
36 */ 36 */
37typedef void(*Eina_Promise_Error_Cb)(void* data, Eina_Error const* error); 37typedef void(*Eina_Promise_Error_Cb)(void* data, Eina_Error error, Eina_Promise* promise);
38 38
39/* 39/*
40 * @brief Function callback type for progress information 40 * @brief Function callback type for progress information
@@ -107,6 +107,13 @@ typedef void*(*Eina_Promise_Buffer_Get_Cb)(Eina_Promise* promise);
107#define EINA_FUNC_PROMISE_BUFFER_GET(Function) ((Eina_Promise_Buffer_Get_Cb)Function) 107#define EINA_FUNC_PROMISE_BUFFER_GET(Function) ((Eina_Promise_Buffer_Get_Cb)Function)
108 108
109/* 109/*
110 * @brief Function callback type for promise's release_value_ownership function override
111 */
112typedef void*(*Eina_Promise_Release_Value_Ownership_Cb)(Eina_Promise* promise);
113
114#define EINA_FUNC_PROMISE_RELEASE_VALUE_OWNERSHIP(Function) ((Eina_Promise_Release_Value_Ownership_Cb)Function)
115
116/*
110 * @brief Function callback type for promise's value_size_get function override 117 * @brief Function callback type for promise's value_size_get function override
111 */ 118 */
112typedef size_t(*Eina_Promise_Value_Size_Get_Cb)(Eina_Promise const* promise); 119typedef size_t(*Eina_Promise_Value_Size_Get_Cb)(Eina_Promise const* promise);
@@ -138,7 +145,7 @@ typedef void*(*Eina_Promise_Owner_Promise_Get_Cb)(Eina_Promise_Owner const* prom
138/* 145/*
139 * @brief Function callback type for promise owner's value_set function override 146 * @brief Function callback type for promise owner's value_set function override
140 */ 147 */
141typedef void(*Eina_Promise_Owner_Value_Set_Cb)(Eina_Promise_Owner* promise, void* data, Eina_Promise_Free_Cb free_fun); 148typedef void(*Eina_Promise_Owner_Value_Set_Cb)(Eina_Promise_Owner* promise, const void* data, Eina_Promise_Free_Cb free_fun);
142 149
143#define EINA_FUNC_PROMISE_OWNER_VALUE_SET(Function) ((Eina_Promise_Owner_Value_Set_Cb)Function) 150#define EINA_FUNC_PROMISE_OWNER_VALUE_SET(Function) ((Eina_Promise_Owner_Value_Set_Cb)Function)
144 151
@@ -194,6 +201,7 @@ struct _Eina_Promise
194 Eina_Promise_Unref_Cb unref; 201 Eina_Promise_Unref_Cb unref;
195 Eina_Promise_Value_Size_Get_Cb value_size_get; 202 Eina_Promise_Value_Size_Get_Cb value_size_get;
196 Eina_Promise_Buffer_Get_Cb buffer_get; 203 Eina_Promise_Buffer_Get_Cb buffer_get;
204 Eina_Promise_Release_Value_Ownership_Cb release_value_ownership;
197#define EINA_MAGIC_PROMISE 0x07932A5B 205#define EINA_MAGIC_PROMISE 0x07932A5B
198 EINA_MAGIC; 206 EINA_MAGIC;
199}; 207};
@@ -269,7 +277,7 @@ EAPI Eina_Promise* eina_promise_progress_notification(Eina_Promise_Owner* promis
269 * @param value The pointer to the value that is going to be copied, or NULL. 277 * @param value The pointer to the value that is going to be copied, or NULL.
270 * @param free_cb Callback function to be used to free the value copied 278 * @param free_cb Callback function to be used to free the value copied
271 */ 279 */
272EAPI void eina_promise_owner_value_set(Eina_Promise_Owner* promise, void* value, Eina_Promise_Free_Cb free_cb); 280EAPI void eina_promise_owner_value_set(Eina_Promise_Owner* promise, const void* value, Eina_Promise_Free_Cb free_cb);
273 281
274/* 282/*
275 * @brief Returns the pointer to the value 283 * @brief Returns the pointer to the value
@@ -296,6 +304,15 @@ EAPI void* eina_promise_value_get(Eina_Promise const* promise);
296EAPI void* eina_promise_buffer_get(Eina_Promise* promise); 304EAPI void* eina_promise_buffer_get(Eina_Promise* promise);
297 305
298/* 306/*
307 * @brief Returns the pointer to the value and releases ownership of
308 * the value by the promise.
309 *
310 * @param promise The promise for which to release value ownership
311 * @return Pointer to value
312 */
313EAPI void* eina_promise_release_value_ownership(Eina_Promise* promise);
314
315/*
299 * @brief Returns the pointer to the buffer that holds the value. 316 * @brief Returns the pointer to the buffer that holds the value.
300 * 317 *
301 * This function always return the buffer pointer independently if the 318 * This function always return the buffer pointer independently if the
diff --git a/src/lib/elementary/elm_view_form.c b/src/lib/elementary/elm_view_form.c
index 612165f..8f2fb85 100644
--- a/src/lib/elementary/elm_view_form.c
+++ b/src/lib/elementary/elm_view_form.c
@@ -46,18 +46,18 @@ struct _Elm_View_Form_Promise
46 46
47 47
48static void 48static void
49_efl_promise_then_widget(Elm_View_Form_Widget *w, Eina_Value *value) 49_efl_promise_then_widget(Elm_View_Form_Widget *w, Eina_Value *value, Eina_Promise* promise EINA_UNUSED)
50{ 50{
51 w->widget_obj_set_cb(w->widget_obj, value, w->widget_propname); 51 w->widget_obj_set_cb(w->widget_obj, value, w->widget_propname);
52} 52}
53 53
54static void 54static void
55_efl_promise_error_widget(void *data EINA_UNUSED, const Eina_Error *err EINA_UNUSED) 55_efl_promise_error_widget(void *data EINA_UNUSED, Eina_Error err EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
56{ 56{
57} 57}
58 58
59static void 59static void
60_efl_model_promise_then_cb(Elm_View_Form_Promise *p, Eina_Value *value) 60_efl_model_promise_then_cb(Elm_View_Form_Promise *p, Eina_Value *value, Eina_Promise* promise EINA_UNUSED)
61{ 61{
62 EINA_SAFETY_ON_NULL_RETURN(p); 62 EINA_SAFETY_ON_NULL_RETURN(p);
63 63
@@ -78,7 +78,7 @@ _efl_model_promise_then_cb(Elm_View_Form_Promise *p, Eina_Value *value)
78} 78}
79 79
80static void 80static void
81_efl_model_promise_error_cb(Elm_View_Form_Promise *p, const Eina_Error *error EINA_UNUSED) 81_efl_model_promise_error_cb(Elm_View_Form_Promise *p, Eina_Error error EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
82{ 82{
83 EINA_SAFETY_ON_NULL_RETURN(p); 83 EINA_SAFETY_ON_NULL_RETURN(p);
84 84
diff --git a/src/tests/ecore/ecore_test_promise.c b/src/tests/ecore/ecore_test_promise.c
index 5a54281..bf2a160 100644
--- a/src/tests/ecore/ecore_test_promise.c
+++ b/src/tests/ecore/ecore_test_promise.c
@@ -11,7 +11,7 @@ void promised_thread(const void* data EINA_UNUSED, Eina_Promise_Owner* promise,
11 eina_promise_owner_value_set(promise, NULL, NULL); 11 eina_promise_owner_value_set(promise, NULL, NULL);
12} 12}
13 13
14void promise_callback(void* data EINA_UNUSED, void* value EINA_UNUSED) 14void promise_callback(void* data EINA_UNUSED, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
15{ 15{
16 ecore_main_loop_quit(); 16 ecore_main_loop_quit();
17} 17}
@@ -36,9 +36,9 @@ void promise_error_thread(const void* data EINA_UNUSED, Eina_Promise_Owner* prom
36 eina_promise_owner_error_set(promise, EINA_ERROR_OUT_OF_MEMORY); 36 eina_promise_owner_error_set(promise, EINA_ERROR_OUT_OF_MEMORY);
37} 37}
38 38
39void promise_error_callback(void* data EINA_UNUSED, Eina_Error const* error) 39void promise_error_callback(void* data EINA_UNUSED, Eina_Error error, Eina_Promise* promise EINA_UNUSED)
40{ 40{
41 ck_assert(*error == EINA_ERROR_OUT_OF_MEMORY); 41 ck_assert(error == EINA_ERROR_OUT_OF_MEMORY);
42 ecore_main_loop_quit(); 42 ecore_main_loop_quit();
43} 43}
44 44
@@ -74,7 +74,7 @@ START_TEST(ecore_test_promise_all)
74} 74}
75END_TEST 75END_TEST
76 76
77void promise_callback2(void* data, void* value EINA_UNUSED) 77void promise_callback2(void* data, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
78{ 78{
79 if(++(*(int*)data) == 2) 79 if(++(*(int*)data) == 2)
80 ecore_main_loop_quit(); 80 ecore_main_loop_quit();
@@ -201,7 +201,7 @@ void promised_block_thread(const void* data EINA_UNUSED, Eina_Promise_Owner* pro
201} 201}
202 202
203static void 203static void
204_ecore_test_promise_normal_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED) 204_ecore_test_promise_normal_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
205{ 205{
206 ecore_main_loop_quit(); 206 ecore_main_loop_quit();
207} 207}
@@ -254,7 +254,7 @@ START_TEST(ecore_test_promise_normal_lifetime_all)
254END_TEST 254END_TEST
255 255
256static void 256static void
257_ecore_test_promise_immediate_set_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED) 257_ecore_test_promise_immediate_set_lifetime_cb(void* data EINA_UNUSED, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
258{ 258{
259 ecore_main_loop_quit(); 259 ecore_main_loop_quit();
260} 260}
@@ -327,7 +327,7 @@ static void _cancel_callback(const void* data, Eina_Promise_Owner* promise EINA_
327 eina_lock_release(&v->lock); 327 eina_lock_release(&v->lock);
328} 328}
329 329
330static void _cancel_promise_callback(void* data EINA_UNUSED, Eina_Error const* value) 330static void _cancel_promise_callback(void* data EINA_UNUSED, Eina_Error value, Eina_Promise* promise EINA_UNUSED)
331{ 331{
332 ck_assert(!!value); 332 ck_assert(!!value);
333 ecore_main_loop_quit(); 333 ecore_main_loop_quit();
diff --git a/src/tests/eina/eina_test_promise.c b/src/tests/eina/eina_test_promise.c
index 2932e1c..cc7aa88 100644
--- a/src/tests/eina/eina_test_promise.c
+++ b/src/tests/eina/eina_test_promise.c
@@ -32,7 +32,7 @@
32#include "eina_suite.h" 32#include "eina_suite.h"
33 33
34static void 34static void
35_eina_test_promise_cb(void* data, void* value EINA_UNUSED) 35_eina_test_promise_cb(void* data, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
36{ 36{
37 *(Eina_Bool*)data = EINA_TRUE; 37 *(Eina_Bool*)data = EINA_TRUE;
38} 38}
@@ -122,7 +122,7 @@ START_TEST(eina_test_promise_immediate_set_lifetime_all)
122} 122}
123END_TEST 123END_TEST
124 124
125static void _eina_test_promise_value_all_cb(void* data, void* value) 125static void _eina_test_promise_value_all_cb(void* data, void* value, Eina_Promise* promise EINA_UNUSED)
126{ 126{
127 Eina_Iterator** iterator = value; 127 Eina_Iterator** iterator = value;
128 int *i, *j; 128 int *i, *j;
@@ -178,9 +178,8 @@ static void cancel_callback(void* data, Eina_Promise_Owner* promise EINA_UNUSED)
178 *(Eina_Bool*)data = EINA_TRUE; 178 *(Eina_Bool*)data = EINA_TRUE;
179} 179}
180 180
181static void _cancel_promise_callback(void* data EINA_UNUSED, Eina_Error const* value) 181static void _cancel_promise_callback(void* data EINA_UNUSED, Eina_Error error, Eina_Promise* promise EINA_UNUSED)
182{ 182{
183 ck_assert(!!value);
184 *(Eina_Bool*)data = EINA_TRUE; 183 *(Eina_Bool*)data = EINA_TRUE;
185} 184}
186 185
@@ -207,7 +206,7 @@ START_TEST(eina_test_promise_cancel_promise)
207} 206}
208END_TEST 207END_TEST
209 208
210void progress_callback(void* data, void* value) 209void progress_callback(void* data, void* value, Eina_Promise* promise EINA_UNUSED)
211{ 210{
212 int* i = value; 211 int* i = value;
213 ck_assert(*i == 1); 212 ck_assert(*i == 1);
@@ -285,13 +284,13 @@ START_TEST(eina_test_promise_progress_notify2)
285END_TEST 284END_TEST
286 285
287static void 286static void
288_eina_promise_progress_notify_fulfilled(void* data, void* value EINA_UNUSED) 287_eina_promise_progress_notify_fulfilled(void* data, void* value EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
289{ 288{
290 *(Eina_Bool*)data = EINA_TRUE; 289 *(Eina_Bool*)data = EINA_TRUE;
291} 290}
292 291
293static void 292static void
294_eina_promise_progress_notify_error(void* data EINA_UNUSED, Eina_Error const* error EINA_UNUSED) 293_eina_promise_progress_notify_error(void* data EINA_UNUSED, Eina_Error error EINA_UNUSED, Eina_Promise* promise EINA_UNUSED)
295{ 294{
296 ck_assert(EINA_FALSE); 295 ck_assert(EINA_FALSE);
297} 296}