summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_promise.c
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-06-03 16:52:03 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-06-03 17:22:12 -0300
commit8fec0d5139a3ff30037ba159bb62ff2f733d96a8 (patch)
tree0316d756b8150e9ef40d76668e5084fdb06af29a /src/lib/eina/eina_promise.c
parent1dcccfdf8160ca6113d66c51c226302221b9a049 (diff)
eina: Remove unnecessary indirection to promises
Now when dealing with pointer types, we will not get pointer to pointer semantics in callbacks and eina_promise_owner_value_set for Eina_Promise. It will work as expected: Eina_Promise_Owner* promise = eina_promise_add(); void* p = malloc(sizeof(T)); eina_promise_owner_value_set(promise, p, &free);
Diffstat (limited to '')
-rw-r--r--src/lib/eina/eina_promise.c116
1 files changed, 92 insertions, 24 deletions
diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c
index 2c5a14a1ad..71f0381e70 100644
--- a/src/lib/eina/eina_promise.c
+++ b/src/lib/eina/eina_promise.c
@@ -79,6 +79,7 @@ struct _Eina_Promise_Default
79 Eina_Bool is_cancelled : 1; 79 Eina_Bool is_cancelled : 1;
80 Eina_Bool is_manual_then : 1; 80 Eina_Bool is_manual_then : 1;
81 Eina_Bool is_first_then : 1; 81 Eina_Bool is_first_then : 1;
82 Eina_Bool is_pointer : 1;
82}; 83};
83 84
84struct _Eina_Promise_Default_Owner 85struct _Eina_Promise_Default_Owner
@@ -93,7 +94,6 @@ struct _Eina_Promise_Default_Owner
93 94
94struct _Eina_Promise_Iterator 95struct _Eina_Promise_Iterator
95{ 96{
96 Eina_Iterator* success_iterator;
97 struct _Eina_Promise_Success_Iterator 97 struct _Eina_Promise_Success_Iterator
98 { 98 {
99 Eina_Iterator success_iterator_impl; 99 Eina_Iterator success_iterator_impl;
@@ -173,7 +173,14 @@ _eina_promise_then_calls(_Eina_Promise_Default_Owner* promise)
173 } 173 }
174 else if (callback->callback) 174 else if (callback->callback)
175 { 175 {
176 (*callback->callback)(callback->data, &promise->value[0], &promise->promise.vtable); 176 if(promise->promise.is_pointer)
177 {
178 char* buffer = promise->value;
179 void** p = (void**)buffer;
180 (*callback->callback)(callback->data, *p, &promise->promise.vtable);
181 }
182 else
183 (*callback->callback)(callback->data, &promise->value[0], &promise->promise.vtable);
177 } 184 }
178 free(callback); 185 free(callback);
179 _eina_promise_unref(&promise->promise); 186 _eina_promise_unref(&promise->promise);
@@ -211,7 +218,16 @@ _eina_promise_del(_Eina_Promise_Default_Owner* promise)
211 } 218 }
212 219
213 if (promise->promise.value_free_cb) 220 if (promise->promise.value_free_cb)
214 promise->promise.value_free_cb((void*)&promise->value[0]); 221 {
222 if(promise->promise.is_pointer)
223 {
224 char* buffer = promise->value;
225 void** p = (void**)buffer;
226 promise->promise.value_free_cb(*p);
227 }
228 else
229 promise->promise.value_free_cb((void*)&promise->value[0]);
230 }
215 231
216 _eina_promise_free_callback_list(&promise->promise.progress_callbacks, 232 _eina_promise_free_callback_list(&promise->promise.progress_callbacks,
217 &_eina_promise_free_progress_callback_node); 233 &_eina_promise_free_progress_callback_node);
@@ -227,6 +243,14 @@ _eina_promise_owner_buffer_get(_Eina_Promise_Default_Owner* promise)
227} 243}
228 244
229static void * 245static void *
246_eina_promise_owner_pointer_buffer_get(_Eina_Promise_Default_Owner* promise)
247{
248 char* buffer = promise->value;
249 void** p = (void**)buffer;
250 return *p;
251}
252
253static void *
230_eina_promise_buffer_get(_Eina_Promise_Default const* promise) 254_eina_promise_buffer_get(_Eina_Promise_Default const* promise)
231{ 255{
232 _Eina_Promise_Default_Owner const* p = EINA_PROMISE_GET_OWNER(promise); 256 _Eina_Promise_Default_Owner const* p = EINA_PROMISE_GET_OWNER(promise);
@@ -248,13 +272,14 @@ _eina_promise_value_get(_Eina_Promise_Default const* p)
248} 272}
249 273
250static void * 274static void *
251_eina_promise_release_value_ownership(_Eina_Promise_Default* p) 275_eina_promise_pointer_value_get(_Eina_Promise_Default const* p)
252{ 276{
253 _Eina_Promise_Default_Owner* promise = EINA_PROMISE_GET_OWNER(p); 277 _Eina_Promise_Default_Owner const* promise = EINA_PROMISE_GET_OWNER(p);
254 if (p->has_finished && !p->has_errored) 278 if (p->has_finished && !p->has_errored)
255 { 279 {
256 p->value_free_cb = NULL; 280 char const* buffer = promise->value;
257 return (void*)&promise->value[0]; 281 void** p = (void**)buffer;
282 return *p;
258 } 283 }
259 else 284 else
260 { 285 {
@@ -276,6 +301,16 @@ _eina_promise_owner_value_set(_Eina_Promise_Default_Owner* promise, const void*
276} 301}
277 302
278static void 303static void
304_eina_promise_owner_pointer_value_set(_Eina_Promise_Default_Owner* promise, const void* data, Eina_Promise_Free_Cb free)
305{
306 char* buffer = promise->value;
307 void** p = (void**)buffer;
308 *p = (void*)data;
309 promise->promise.value_free_cb = free;
310 _eina_promise_finish(promise);
311}
312
313static void
279_eina_promise_then(_Eina_Promise_Default* p, Eina_Promise_Cb callback, 314_eina_promise_then(_Eina_Promise_Default* p, Eina_Promise_Cb callback,
280 Eina_Promise_Error_Cb error_cb, void* data) 315 Eina_Promise_Error_Cb error_cb, void* data)
281{ 316{
@@ -485,7 +520,7 @@ _eina_promise_owner_progress_notify(_Eina_Promise_Default_Owner* promise, Eina_P
485} 520}
486 521
487Eina_Promise_Owner * 522Eina_Promise_Owner *
488eina_promise_default_add(int value_size) 523eina_promise_value_add(int value_size)
489{ 524{
490 _Eina_Promise_Default_Owner* p; 525 _Eina_Promise_Default_Owner* p;
491 526
@@ -501,9 +536,8 @@ eina_promise_default_add(int value_size)
501 p->promise.vtable.unref = EINA_FUNC_PROMISE_UNREF(_eina_promise_unref); 536 p->promise.vtable.unref = EINA_FUNC_PROMISE_UNREF(_eina_promise_unref);
502 p->promise.vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(_eina_promise_value_size_get); 537 p->promise.vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(_eina_promise_value_size_get);
503 p->promise.vtable.buffer_get = EINA_FUNC_PROMISE_BUFFER_GET(_eina_promise_buffer_get); 538 p->promise.vtable.buffer_get = EINA_FUNC_PROMISE_BUFFER_GET(_eina_promise_buffer_get);
504 p->promise.vtable.release_value_ownership = EINA_FUNC_PROMISE_RELEASE_VALUE_OWNERSHIP(_eina_promise_release_value_ownership);
505 p->promise.has_finished = p->promise.has_errored = 539 p->promise.has_finished = p->promise.has_errored =
506 p->promise.is_cancelled = p->promise.is_manual_then = EINA_FALSE; 540 p->promise.is_cancelled = p->promise.is_manual_then = p->promise.is_pointer = EINA_FALSE;
507 p->promise.is_first_then = EINA_TRUE; 541 p->promise.is_first_then = EINA_TRUE;
508 p->promise.ref = 1; 542 p->promise.ref = 1;
509 memset(&p->promise.then_callbacks, 0, sizeof(p->promise.then_callbacks)); 543 memset(&p->promise.then_callbacks, 0, sizeof(p->promise.then_callbacks));
@@ -528,6 +562,49 @@ eina_promise_default_add(int value_size)
528 return &p->owner_vtable; 562 return &p->owner_vtable;
529} 563}
530 564
565EAPI Eina_Promise_Owner *
566eina_promise_add()
567{
568 _Eina_Promise_Default_Owner* p;
569
570 p = malloc(sizeof(_Eina_Promise_Default_Owner) + sizeof(void*));
571 p->promise.vtable.version = EINA_PROMISE_VERSION;
572 p->promise.vtable.then = EINA_FUNC_PROMISE_THEN(_eina_promise_then);
573 p->promise.vtable.value_get = EINA_FUNC_PROMISE_VALUE_GET(_eina_promise_pointer_value_get);
574 p->promise.vtable.error_get = EINA_FUNC_PROMISE_ERROR_GET(_eina_promise_error_get);
575 p->promise.vtable.pending_is = EINA_FUNC_PROMISE_PENDING_IS(_eina_promise_pending_is);
576 p->promise.vtable.progress_cb_add = EINA_FUNC_PROMISE_PROGRESS_CB_ADD(_eina_promise_progress_cb_add);
577 p->promise.vtable.cancel = EINA_FUNC_PROMISE_CANCEL(_eina_promise_cancel);
578 p->promise.vtable.ref = EINA_FUNC_PROMISE_REF(_eina_promise_ref);
579 p->promise.vtable.unref = EINA_FUNC_PROMISE_UNREF(_eina_promise_unref);
580 p->promise.vtable.value_size_get = EINA_FUNC_PROMISE_VALUE_SIZE_GET(_eina_promise_value_size_get);
581 p->promise.vtable.buffer_get = EINA_FUNC_PROMISE_BUFFER_GET(_eina_promise_buffer_get);
582 p->promise.has_finished = p->promise.has_errored =
583 p->promise.is_cancelled = p->promise.is_manual_then = EINA_FALSE;
584 p->promise.is_first_then = p->promise.is_pointer = EINA_TRUE;
585 p->promise.ref = 1;
586 memset(&p->promise.then_callbacks, 0, sizeof(p->promise.then_callbacks));
587 memset(&p->promise.progress_callbacks, 0, sizeof(p->promise.progress_callbacks));
588 memset(&p->promise.progress_notify_callbacks, 0, sizeof(p->promise.progress_notify_callbacks));
589 memset(&p->promise.cancel_callbacks, 0, sizeof(p->promise.cancel_callbacks));
590 p->promise.value_size = 0;
591 p->promise.value_free_cb = NULL;
592 p->promise.error = 0;
593
594 p->owner_vtable.version = EINA_PROMISE_VERSION;
595 p->owner_vtable.value_set = EINA_FUNC_PROMISE_OWNER_VALUE_SET(_eina_promise_owner_pointer_value_set);
596 p->owner_vtable.error_set = EINA_FUNC_PROMISE_OWNER_ERROR_SET(_eina_promise_owner_error_set);
597 p->owner_vtable.buffer_get = EINA_FUNC_PROMISE_OWNER_BUFFER_GET(_eina_promise_owner_pointer_buffer_get);
598 p->owner_vtable.value_size_get = EINA_FUNC_PROMISE_OWNER_VALUE_SIZE_GET(_eina_promise_owner_value_size_get);
599 p->owner_vtable.promise_get = EINA_FUNC_PROMISE_OWNER_PROMISE_GET(_eina_promise_owner_promise_get);
600 p->owner_vtable.pending_is = EINA_FUNC_PROMISE_OWNER_PENDING_IS(_eina_promise_owner_pending_is);
601 p->owner_vtable.cancelled_is = EINA_FUNC_PROMISE_OWNER_CANCELLED_IS(_eina_promise_owner_cancelled_is);
602 p->owner_vtable.progress = EINA_FUNC_PROMISE_OWNER_PROGRESS(_eina_promise_owner_progress);
603 p->owner_vtable.progress_notify = EINA_FUNC_PROMISE_OWNER_PROGRESS_NOTIFY(_eina_promise_owner_progress_notify);
604
605 return &p->owner_vtable;
606}
607
531void 608void
532eina_promise_owner_default_manual_then_set(Eina_Promise_Owner* owner, Eina_Bool is_manual) 609eina_promise_owner_default_manual_then_set(Eina_Promise_Owner* owner, Eina_Bool is_manual)
533{ 610{
@@ -575,8 +652,6 @@ _eina_promise_all_free(_Eina_Promise_Iterator* value)
575{ 652{
576 unsigned i = 0; 653 unsigned i = 0;
577 654
578 eina_iterator_free(value->success_iterator);
579
580 for (;i != value->data.num_promises; ++i) 655 for (;i != value->data.num_promises; ++i)
581 { 656 {
582 eina_promise_unref(value->data.promises[i]); 657 eina_promise_unref(value->data.promises[i]);
@@ -602,8 +677,8 @@ eina_promise_all(Eina_Iterator* it)
602 eina_iterator_free(it); 677 eina_iterator_free(it);
603 678
604 promise = (_Eina_Promise_Default_Owner*) 679 promise = (_Eina_Promise_Default_Owner*)
605 eina_promise_default_add(sizeof(_Eina_Promise_Iterator) + 680 eina_promise_value_add(sizeof(_Eina_Promise_Iterator) +
606 sizeof(_Eina_Promise_Default_Owner*)*eina_array_count_get(promises)); 681 sizeof(_Eina_Promise_Default_Owner*)*eina_array_count_get(promises));
607 internal_it = (_Eina_Promise_Iterator*)&promise->value[0]; 682 internal_it = (_Eina_Promise_Iterator*)&promise->value[0];
608 _eina_promise_iterator_setup(internal_it, promises); 683 _eina_promise_iterator_setup(internal_it, promises);
609 eina_array_free(promises); 684 eina_array_free(promises);
@@ -655,7 +730,6 @@ _eina_promise_iterator_setup(_Eina_Promise_Iterator* it, Eina_Array* promises_ar
655{ 730{
656 Eina_Promise** promises; 731 Eina_Promise** promises;
657 732
658 it->success_iterator = &it->data.success_iterator_impl;
659 it->data.num_promises = eina_array_count_get(promises_array); 733 it->data.num_promises = eina_array_count_get(promises_array);
660 it->data.promise_index = 0; 734 it->data.promise_index = 0;
661 it->data.promises_finished = 0; 735 it->data.promises_finished = 0;
@@ -695,7 +769,7 @@ eina_promise_progress_notification(Eina_Promise_Owner* promise)
695{ 769{
696 Eina_Promise_Owner* owner; 770 Eina_Promise_Owner* owner;
697 771
698 owner = eina_promise_default_add(0); 772 owner = eina_promise_value_add(0);
699 773
700 eina_promise_owner_progress_notify(promise, &_eina_promise_progress_notify_fulfilled, owner, 774 eina_promise_owner_progress_notify(promise, &_eina_promise_progress_notify_fulfilled, owner,
701 &_eina_promise_progress_notify_failed); 775 &_eina_promise_progress_notify_failed);
@@ -768,8 +842,8 @@ eina_promise_race(Eina_Iterator* it)
768 842
769 num_promises = eina_array_count_get(promises); 843 num_promises = eina_array_count_get(promises);
770 promise = (_Eina_Promise_Default_Owner*) 844 promise = (_Eina_Promise_Default_Owner*)
771 eina_promise_default_add(sizeof(_Eina_Promise_Race_Value_Type) + 845 eina_promise_value_add(sizeof(_Eina_Promise_Race_Value_Type) +
772 sizeof(struct _Eina_Promise_Race_Information)*num_promises); 846 sizeof(struct _Eina_Promise_Race_Information)*num_promises);
773 value = eina_promise_owner_buffer_get((Eina_Promise_Owner*)promise); 847 value = eina_promise_owner_buffer_get((Eina_Promise_Owner*)promise);
774 value->value = NULL; 848 value->value = NULL;
775 value->promise_index = -1; 849 value->promise_index = -1;
@@ -868,12 +942,6 @@ eina_promise_buffer_get(Eina_Promise* promise)
868 return promise->buffer_get(promise); 942 return promise->buffer_get(promise);
869} 943}
870 944
871EAPI void *
872eina_promise_release_value_ownership(Eina_Promise* promise)
873{
874 return promise->release_value_ownership(promise);
875}
876
877EAPI size_t 945EAPI size_t
878eina_promise_value_size_get(Eina_Promise const* promise) 946eina_promise_value_size_get(Eina_Promise const* promise)
879{ 947{