summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-04-17 15:47:33 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-04-17 16:15:14 -0300
commit3ee910942cc4bcabd2b6b8a0aef3ab4f6604cfa3 (patch)
tree3ef4a863f2d78adf727960610d124a68e5f50cd8
parent75c741ff2b14fb433f8ee16d7acd82a3b38c2a92 (diff)
eina: Add progress notify callback feature for Promise Ownersdevs/felipealmeida/promises
Add a way for users of the promise owner to get notified when a promise progress is registered
-rw-r--r--src/lib/eina/eina_promise.c64
-rw-r--r--src/lib/eina/eina_promise.h27
-rw-r--r--src/tests/eina/eina_test_promise.c50
3 files changed, 141 insertions, 0 deletions
diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c
index 459f686e84..768f2596ca 100644
--- a/src/lib/eina/eina_promise.c
+++ b/src/lib/eina/eina_promise.c
@@ -9,6 +9,7 @@
9typedef struct _Eina_Promise_Then_Cb _Eina_Promise_Then_Cb; 9typedef struct _Eina_Promise_Then_Cb _Eina_Promise_Then_Cb;
10typedef struct _Eina_Promise_Progress_Cb _Eina_Promise_Progress_Cb; 10typedef struct _Eina_Promise_Progress_Cb _Eina_Promise_Progress_Cb;
11typedef struct _Eina_Promise_Cancel_Cb _Eina_Promise_Cancel_Cb; 11typedef struct _Eina_Promise_Cancel_Cb _Eina_Promise_Cancel_Cb;
12typedef struct _Eina_Promise_Owner_Progress_Notify_Data _Eina_Promise_Owner_Progress_Notify_Data;
12typedef struct _Eina_Promise_Default _Eina_Promise_Default; 13typedef struct _Eina_Promise_Default _Eina_Promise_Default;
13typedef struct _Eina_Promise_Default_Owner _Eina_Promise_Default_Owner; 14typedef struct _Eina_Promise_Default_Owner _Eina_Promise_Default_Owner;
14typedef struct _Eina_Promise_Iterator _Eina_Promise_Iterator; 15typedef struct _Eina_Promise_Iterator _Eina_Promise_Iterator;
@@ -40,6 +41,15 @@ struct _Eina_Promise_Cancel_Cb
40 void* data; 41 void* data;
41}; 42};
42 43
44struct _Eina_Promise_Owner_Progress_Notify_Data
45{
46 EINA_INLIST;
47
48 Eina_Promise_Progress_Notify_Cb callback;
49 Eina_Promise_Free_Cb free_cb;
50 void* data;
51};
52
43struct _Eina_Promise_Default 53struct _Eina_Promise_Default
44{ 54{
45 Eina_Promise vtable; 55 Eina_Promise vtable;
@@ -49,6 +59,7 @@ struct _Eina_Promise_Default
49 Eina_Inlist *then_callbacks; 59 Eina_Inlist *then_callbacks;
50 Eina_Inlist *progress_callbacks; 60 Eina_Inlist *progress_callbacks;
51 Eina_Inlist *cancel_callbacks; 61 Eina_Inlist *cancel_callbacks;
62 Eina_Inlist *progress_notify_callbacks;
52 Eina_Promise_Free_Cb value_free_cb; 63 Eina_Promise_Free_Cb value_free_cb;
53 64
54 int ref; 65 int ref;
@@ -89,6 +100,21 @@ static void _eina_promise_unref(_Eina_Promise_Default* promise);
89 100
90static void _eina_promise_iterator_setup(_Eina_Promise_Iterator* iterator, Eina_Array* promises); 101static void _eina_promise_iterator_setup(_Eina_Promise_Iterator* iterator, Eina_Array* promises);
91 102
103static void _eina_promise_free_callback_list(Eina_Inlist** list, void(*free_cb)(void* node))
104{
105 struct node
106 {
107 EINA_INLIST;
108 } *node;
109 Eina_Inlist *list2;
110
111 EINA_INLIST_FOREACH_SAFE(*list, list2, node)
112 {
113 free_cb(node);
114 }
115 *list = NULL;
116}
117
92static void 118static void
93_eina_promise_then_calls(_Eina_Promise_Default_Owner* promise) 119_eina_promise_then_calls(_Eina_Promise_Default_Owner* promise)
94{ 120{
@@ -188,6 +214,7 @@ _eina_promise_then(_Eina_Promise_Default* p, Eina_Promise_Cb callback,
188{ 214{
189 _Eina_Promise_Default_Owner* promise; 215 _Eina_Promise_Default_Owner* promise;
190 _Eina_Promise_Then_Cb* cb; 216 _Eina_Promise_Then_Cb* cb;
217 _Eina_Promise_Owner_Progress_Notify_Data* notify_data;
191 218
192 promise = EINA_PROMISE_GET_OWNER(p); 219 promise = EINA_PROMISE_GET_OWNER(p);
193 220
@@ -198,6 +225,12 @@ _eina_promise_then(_Eina_Promise_Default* p, Eina_Promise_Cb callback,
198 cb->data = data; 225 cb->data = data;
199 promise->promise.then_callbacks = eina_inlist_append(promise->promise.then_callbacks, EINA_INLIST_GET(cb)); 226 promise->promise.then_callbacks = eina_inlist_append(promise->promise.then_callbacks, EINA_INLIST_GET(cb));
200 227
228 EINA_INLIST_FOREACH(promise->promise.progress_notify_callbacks, notify_data)
229 {
230 (*notify_data->callback)(notify_data->data, &promise->owner_vtable);
231 }
232 _eina_promise_free_callback_list(&promise->promise.progress_notify_callbacks, &free);
233
201 if (!promise->promise.is_first_then) 234 if (!promise->promise.is_first_then)
202 { 235 {
203 _eina_promise_ref(p); 236 _eina_promise_ref(p);
@@ -263,11 +296,19 @@ static void
263_eina_promise_progress_cb_add(_Eina_Promise_Default* promise, Eina_Promise_Progress_Cb callback, void* data) 296_eina_promise_progress_cb_add(_Eina_Promise_Default* promise, Eina_Promise_Progress_Cb callback, void* data)
264{ 297{
265 _Eina_Promise_Progress_Cb* cb; 298 _Eina_Promise_Progress_Cb* cb;
299 _Eina_Promise_Owner_Progress_Notify_Data* notify_data;
300 _Eina_Promise_Default_Owner* owner = EINA_PROMISE_GET_OWNER(promise);
266 301
267 cb = malloc(sizeof(struct _Eina_Promise_Progress_Cb)); 302 cb = malloc(sizeof(struct _Eina_Promise_Progress_Cb));
268 cb->callback = callback; 303 cb->callback = callback;
269 cb->data = data; 304 cb->data = data;
270 promise->progress_callbacks = eina_inlist_append(promise->progress_callbacks, EINA_INLIST_GET(cb)); 305 promise->progress_callbacks = eina_inlist_append(promise->progress_callbacks, EINA_INLIST_GET(cb));
306
307 EINA_INLIST_FOREACH(owner->promise.progress_notify_callbacks, notify_data)
308 {
309 (*notify_data->callback)(notify_data->data, &owner->owner_vtable);
310 }
311 _eina_promise_free_callback_list(&owner->promise.progress_notify_callbacks, &free);
271} 312}
272 313
273static void 314static void
@@ -356,6 +397,20 @@ _eina_promise_owner_progress(_Eina_Promise_Default_Owner* promise, void* data)
356 } 397 }
357} 398}
358 399
400static void
401_eina_promise_owner_progress_notify(_Eina_Promise_Default_Owner* promise, Eina_Promise_Progress_Notify_Cb notify,
402 void* data, Eina_Promise_Free_Cb free_cb)
403{
404 _Eina_Promise_Owner_Progress_Notify_Data* cb
405 = malloc(sizeof(struct _Eina_Promise_Owner_Progress_Notify_Data));
406
407 cb->callback = notify;
408 cb->free_cb = free_cb;
409 cb->data = data;
410 promise->promise.progress_notify_callbacks =
411 eina_inlist_append(promise->promise.progress_notify_callbacks, EINA_INLIST_GET(cb));
412}
413
359Eina_Promise_Owner * 414Eina_Promise_Owner *
360eina_promise_default_add(int value_size) 415eina_promise_default_add(int value_size)
361{ 416{
@@ -378,6 +433,7 @@ eina_promise_default_add(int value_size)
378 p->promise.ref = 1; 433 p->promise.ref = 1;
379 memset(&p->promise.then_callbacks, 0, sizeof(p->promise.then_callbacks)); 434 memset(&p->promise.then_callbacks, 0, sizeof(p->promise.then_callbacks));
380 memset(&p->promise.progress_callbacks, 0, sizeof(p->promise.progress_callbacks)); 435 memset(&p->promise.progress_callbacks, 0, sizeof(p->promise.progress_callbacks));
436 memset(&p->promise.progress_notify_callbacks, 0, sizeof(p->promise.progress_notify_callbacks));
381 memset(&p->promise.cancel_callbacks, 0, sizeof(p->promise.cancel_callbacks)); 437 memset(&p->promise.cancel_callbacks, 0, sizeof(p->promise.cancel_callbacks));
382 p->promise.value_size = value_size; 438 p->promise.value_size = value_size;
383 p->promise.value_free_cb = NULL; 439 p->promise.value_free_cb = NULL;
@@ -392,6 +448,7 @@ eina_promise_default_add(int value_size)
392 p->owner_vtable.pending_is = EINA_FUNC_PROMISE_OWNER_PENDING_IS(_eina_promise_owner_pending_is); 448 p->owner_vtable.pending_is = EINA_FUNC_PROMISE_OWNER_PENDING_IS(_eina_promise_owner_pending_is);
393 p->owner_vtable.cancelled_is = EINA_FUNC_PROMISE_OWNER_CANCELLED_IS(_eina_promise_owner_cancelled_is); 449 p->owner_vtable.cancelled_is = EINA_FUNC_PROMISE_OWNER_CANCELLED_IS(_eina_promise_owner_cancelled_is);
394 p->owner_vtable.progress = EINA_FUNC_PROMISE_OWNER_PROGRESS(_eina_promise_owner_progress); 450 p->owner_vtable.progress = EINA_FUNC_PROMISE_OWNER_PROGRESS(_eina_promise_owner_progress);
451 p->owner_vtable.progress_notify = EINA_FUNC_PROMISE_OWNER_PROGRESS_NOTIFY(_eina_promise_owner_progress_notify);
395 452
396 return &p->owner_vtable; 453 return &p->owner_vtable;
397} 454}
@@ -643,3 +700,10 @@ eina_promise_owner_progress(Eina_Promise_Owner const* promise, void* progress)
643{ 700{
644 promise->progress(promise, progress); 701 promise->progress(promise, progress);
645} 702}
703
704EAPI void
705eina_promise_owner_progress_notify(Eina_Promise_Owner* promise, Eina_Promise_Progress_Notify_Cb progress_cb,
706 void* data, Eina_Promise_Free_Cb free_cb)
707{
708 promise->progress_notify(promise, progress_cb, data, free_cb);
709}
diff --git a/src/lib/eina/eina_promise.h b/src/lib/eina/eina_promise.h
index 9093b92767..954e89a41f 100644
--- a/src/lib/eina/eina_promise.h
+++ b/src/lib/eina/eina_promise.h
@@ -20,6 +20,11 @@ typedef struct _Eina_Promise_Owner Eina_Promise_Owner;
20typedef void(*Eina_Promise_Free_Cb)(void* value); 20typedef void(*Eina_Promise_Free_Cb)(void* value);
21 21
22/* 22/*
23 * @brief Callback type for Promise_Owner to get notified of when someone registered a progress and/or then callback
24 */
25typedef void(*Eina_Promise_Progress_Notify_Cb)(void* data, Eina_Promise_Owner* promise);
26
27/*
23 * @brief Function callback type for when using eina_promise_then 28 * @brief Function callback type for when using eina_promise_then
24 */ 29 */
25typedef void(*Eina_Promise_Cb)(void* data, void* value); 30typedef void(*Eina_Promise_Cb)(void* data, void* value);
@@ -162,6 +167,14 @@ typedef Eina_Bool(*Eina_Promise_Owner_Progress_Cb)(Eina_Promise_Owner const* pro
162 167
163#define EINA_FUNC_PROMISE_OWNER_PROGRESS(Function) ((Eina_Promise_Owner_Progress_Cb)Function) 168#define EINA_FUNC_PROMISE_OWNER_PROGRESS(Function) ((Eina_Promise_Owner_Progress_Cb)Function)
164 169
170/*
171 * @brief Function callback type for promise owner's progress notify registration function override
172 */
173typedef Eina_Bool(*Eina_Promise_Owner_Progress_Notify_Cb)(Eina_Promise_Owner* promise,
174 Eina_Promise_Progress_Notify_Cb progress_cb, void* data, Eina_Promise_Free_Cb free_cb);
175
176#define EINA_FUNC_PROMISE_OWNER_PROGRESS_NOTIFY(Function) ((Eina_Promise_Owner_Progress_Notify_Cb)Function)
177
165 178
166#define EINA_PROMISE_VERSION 1 179#define EINA_PROMISE_VERSION 1
167 180
@@ -193,6 +206,7 @@ struct _Eina_Promise_Owner
193 Eina_Promise_Owner_Pending_Is_Cb pending_is; 206 Eina_Promise_Owner_Pending_Is_Cb pending_is;
194 Eina_Promise_Owner_Cancelled_Is_Cb cancelled_is; 207 Eina_Promise_Owner_Cancelled_Is_Cb cancelled_is;
195 Eina_Promise_Owner_Progress_Cb progress; 208 Eina_Promise_Owner_Progress_Cb progress;
209 Eina_Promise_Owner_Progress_Notify_Cb progress_notify;
196#define EINA_MAGIC_PROMISE_OWNER 0x07932A5C 210#define EINA_MAGIC_PROMISE_OWNER 0x07932A5C
197 EINA_MAGIC; 211 EINA_MAGIC;
198}; 212};
@@ -379,6 +393,19 @@ EAPI Eina_Bool eina_promise_owner_cancelled_is(Eina_Promise_Owner const* promise
379EAPI void eina_promise_owner_progress(Eina_Promise_Owner const* promise, void* progress); 393EAPI void eina_promise_owner_progress(Eina_Promise_Owner const* promise, void* progress);
380 394
381/* 395/*
396 * @brief Registers a progress notify callbacks in promise owner.
397 *
398 * The progress callbacks registered in @Eina_Promise must not free
399 * the progress data pointer. The data pointer ownership must be dealt
400 * by the @Eina_Promise_Owner's user.
401 *
402 * @param promise The promise for which to get the cancelled status
403 * @param data The data to be passed to progress
404 */
405EAPI void eina_promise_owner_progress_notify(Eina_Promise_Owner* promise,
406 Eina_Promise_Progress_Notify_Cb notify_cb, void* data, Eina_Promise_Free_Cb free_cb);
407
408/*
382 * @brief Decrement the reference count for the Eina_Promise. 409 * @brief Decrement the reference count for the Eina_Promise.
383 410
384 * The Eina_Promise, if its reference count drops to zero and is not 411 * The Eina_Promise, if its reference count drops to zero and is not
diff --git a/src/tests/eina/eina_test_promise.c b/src/tests/eina/eina_test_promise.c
index 59ed7d7b0f..696ecc26e0 100644
--- a/src/tests/eina/eina_test_promise.c
+++ b/src/tests/eina/eina_test_promise.c
@@ -236,6 +236,54 @@ START_TEST(eina_test_promise_progress)
236} 236}
237END_TEST 237END_TEST
238 238
239static void progress_notify(void* data, Eina_Promise_Owner* promise EINA_UNUSED)
240{
241 ck_assert(!*(Eina_Bool*)data);
242 *(Eina_Bool*)data = EINA_TRUE;
243}
244
245START_TEST(eina_test_promise_progress_notify1)
246{
247 Eina_Bool progress_notify_ran = EINA_FALSE;
248 Eina_Promise_Owner* owner;
249 Eina_Promise* promise;
250
251 eina_init();
252
253 owner = eina_promise_default_add(0);
254 eina_promise_owner_progress_notify(owner, &progress_notify, &progress_notify_ran, NULL);
255
256 promise = eina_promise_owner_promise_get(owner);
257 eina_promise_progress_cb_add(promise, &progress_callback, NULL); // never run
258 eina_promise_progress_cb_add(promise, &progress_callback, NULL); // never run
259
260 ck_assert(progress_notify_ran);
261
262 eina_shutdown();
263}
264END_TEST
265
266START_TEST(eina_test_promise_progress_notify2)
267{
268 Eina_Bool progress_notify_ran = EINA_FALSE;
269 Eina_Promise_Owner* owner;
270 Eina_Promise* promise;
271
272 eina_init();
273
274 owner = eina_promise_default_add(0);
275 eina_promise_owner_progress_notify(owner, &progress_notify, &progress_notify_ran, NULL);
276
277 promise = eina_promise_owner_promise_get(owner);
278 eina_promise_then(promise, NULL, &_cancel_promise_callback, NULL); // never run
279 eina_promise_then(promise, NULL, &_cancel_promise_callback, NULL); // never run
280
281 ck_assert(progress_notify_ran);
282
283 eina_shutdown();
284}
285END_TEST
286
239void 287void
240eina_test_promise(TCase *tc) 288eina_test_promise(TCase *tc)
241{ 289{
@@ -246,4 +294,6 @@ eina_test_promise(TCase *tc)
246 tcase_add_test(tc, eina_test_promise_values_all); 294 tcase_add_test(tc, eina_test_promise_values_all);
247 tcase_add_test(tc, eina_test_promise_cancel_promise); 295 tcase_add_test(tc, eina_test_promise_cancel_promise);
248 tcase_add_test(tc, eina_test_promise_progress); 296 tcase_add_test(tc, eina_test_promise_progress);
297 tcase_add_test(tc, eina_test_promise_progress_notify1);
298 tcase_add_test(tc, eina_test_promise_progress_notify2);
249} 299}