-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.c
614 lines (562 loc) · 20 KB
/
promise.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "promise.h"
#include "map/map.h"
typedef struct
{
/** @type {Map<promise_handle_t, promise_t*>} */
map_handle_t promises;
void* id_seed;
} promise_manager_t;
typedef struct promise_handler_s
{
struct promise_handler_s* next;
promise_then_handler_t then;
void* then_ctx;
promise_catch_handler_t catch;
void* catch_ctx;
bool takeover_data;
bool takeover_reason;
} promise_handler_t;
typedef struct
{
promise_handler_t* first_handler;
promise_handler_t* last_handler;
/** resolve */
bool resolved;
promise_data_t resolve_data;
void(*free_data)(void* data, void* ctx);
void* free_data_ctx;
bool data_booked; /** if there is already a handler booked the data */
bool data_taken_over; /** if the data is already taken over by a handler */
/** reject */
bool rejected;
promise_data_t reject_reason;
void(*free_reason)(void* reason, void* ctx);
void* free_reason_ctx;
bool reason_booked; /** if there is already a handler booked the reason */
bool reason_taken_over; /** if the reason is already taken over by a handler */
/** internal use */
struct
{
void* data;
void(*free_data)(void*, void*);
void* free_ctx;
} internal;
} promise_t;
static void promise_free(promise_t* promise);
static void promise_free_with_ctx(void* data, void* ctx);
promise_manager_handle_t promise_manager_new()
{
promise_manager_t* manager = malloc(sizeof(promise_manager_t));
if(!manager)
goto error;
memset(manager,0,sizeof(promise_manager_t));
manager->id_seed = NULL+1;
manager->promises = map_create();
if(!manager->promises)
goto error;
return (promise_manager_handle_t)manager;
error:
promise_manager_free((promise_manager_handle_t)manager);
return NULL;
}
void promise_manager_free(promise_manager_handle_t manager_handle)
{
promise_manager_t* manager = (promise_manager_t*)manager_handle;
if(manager)
{
if(manager->promises)
map_delete(manager->promises,promise_free_with_ctx,NULL);
free(manager);
}
}
static promise_handle_t promise_new_internal(
promise_manager_handle_t manager_handle,
void* user_data, void(*free_user_data)(void*,void*),void* free_user_data_ctx)
{
promise_manager_t* manager = (promise_manager_t*)manager_handle;
promise_t* promise = NULL;
if(!manager)
goto error;
promise = malloc(sizeof(promise_t));
if(!promise)
goto error;
memset(promise,0,sizeof(promise_t));
promise->internal.data = user_data;
promise->internal.free_data = free_user_data;
promise->internal.free_ctx = free_user_data_ctx;
promise->first_handler = NULL;
promise->last_handler = NULL;
promise->resolved = false;
promise->rejected = false;
promise->free_data = NULL;
promise->free_reason = NULL;
promise->data_taken_over = false;
promise->reason_taken_over = false;
promise->data_booked = false;
promise->reason_booked = false;
promise_handle_t promise_handle = manager->id_seed++;
/** promise handle should never overlap, not handled here */
if(map_add(manager->promises,&promise_handle,sizeof(promise_handle),promise)==NULL)
goto error;
return promise_handle;
error:
promise_free(promise);
return NULL;
}
promise_handle_t promise_new(promise_manager_handle_t manager_handle)
{
return promise_new_internal(manager_handle,NULL,NULL,NULL);
}
void promise_destroy(promise_manager_handle_t manager_handle, promise_handle_t promise_handle)
{
promise_manager_t* manager = (promise_manager_t*)manager_handle;
if(!manager)
return;
promise_t* promise = map_remove(manager->promises,&promise_handle,sizeof(promise_handle));
promise_free(promise);
}
int promise_resolve(
promise_manager_handle_t manager_handle, promise_handle_t promise_handle,
promise_data_t data, void(*free_data)(void*,void*), void* ctx)
{
promise_manager_t* manager = (promise_manager_t*)manager_handle;
if(!manager)
goto error;
promise_t* promise = map_get(manager->promises,&promise_handle,sizeof(promise_handle));
if(!promise)
goto error;
if(promise->resolved || promise->rejected)
goto error;
promise->resolved = true;
promise->resolve_data = data;
promise->free_data = free_data;
promise->free_data_ctx = ctx;
if(promise->first_handler != NULL)
{
/** If there are handlers set */
promise_handler_t* handler = promise->first_handler;
promise_handler_t* takeover_handler = NULL;
while(handler)
{
promise_handler_t* next_handler = handler->next;
if(handler->takeover_data)
takeover_handler = handler; /** there can be at most one takeover handler */
else
handler->then(promise->resolve_data,handler->then_ctx,NULL,NULL);
handler = next_handler;
}
if(takeover_handler) /** call the takeover handler last */
{
promise->data_taken_over = true;
takeover_handler->then(promise->resolve_data,takeover_handler->then_ctx,free_data,ctx);
}
/** free promise and remove from promises if it is still in promises */
promise_t* old_promise = map_remove(manager->promises,&promise_handle,sizeof(promise_handle));
promise_free(old_promise);
}
return 0;
error:
return -1;
}
int promise_reject(
promise_manager_handle_t manager_handle, promise_handle_t promise_handle,
promise_data_t reason, void(*free_reason)(void*,void*), void* ctx)
{
promise_manager_t* manager = (promise_manager_t*)manager_handle;
if(!manager)
goto error;
promise_t* promise = map_get(manager->promises,&promise_handle,sizeof(promise_handle));
if(!promise)
goto error;
if(promise->resolved || promise->rejected)
goto error;
promise->rejected = true;
promise->reject_reason = reason;
promise->free_reason = free_reason;
promise->free_reason_ctx = ctx;
if(promise->first_handler != NULL)
{
/** If there are handlers set */
promise_handler_t* handler = promise->first_handler;
promise_handler_t* takeover_handler = NULL;
while(handler)
{
promise_handler_t* next_handler = handler->next;
if(handler->takeover_reason)
takeover_handler = handler;
else
handler->catch(promise->reject_reason,handler->catch_ctx,NULL,NULL);
handler = next_handler;
}
if(takeover_handler) /** call the take over handler last */
{
promise->reason_taken_over = true;
takeover_handler->catch(promise->reject_reason,takeover_handler->catch_ctx,free_reason,ctx);
}
/** free promise and remove from promises if it is still in promises */
promise_t* old_promise = map_remove(manager->promises,&promise_handle,sizeof(promise_handle));
promise_free(old_promise);
}
return 0;
error:
return -1;
}
int promise_await(
promise_manager_handle_t manager_handle, promise_handle_t promise_handle,
promise_then_handler_t then, void* then_ctx, bool takeover_data,
promise_catch_handler_t catch, void* catch_ctx, bool takeover_reason)
{
promise_manager_t* manager = (promise_manager_t*)manager_handle;
if(!manager)
goto error;
promise_t* promise = map_get(manager->promises,&promise_handle,sizeof(promise_handle));
if(!promise)
goto error;
if((!then) || (!catch))
goto error;
if(promise->data_booked && takeover_data)
goto error;
if(promise->reason_booked && takeover_reason)
goto error;
promise_handler_t* new_handler = malloc(sizeof(promise_handler_t));
if(!new_handler)
goto error;
memset(new_handler,0,sizeof(promise_handler_t));
new_handler->then = then;
new_handler->then_ctx = then_ctx;
new_handler->catch = catch;
new_handler->catch_ctx = catch_ctx;
new_handler->takeover_data = takeover_data;
new_handler->takeover_reason = takeover_reason;
new_handler->next = NULL;
if(promise->last_handler == NULL)
{
promise->first_handler = new_handler;
promise->last_handler = new_handler;
}
else
{
promise->last_handler->next = new_handler;
promise->last_handler = new_handler;
}
promise->data_booked = promise->data_booked || takeover_data;
promise->reason_booked = promise->reason_booked || takeover_reason;
if(promise->resolved)
{
/** Promise is already resolved but not handled */
promise_handler_t* handler = promise->first_handler;
promise_handler_t* takeover_handler = NULL;
while(handler)
{
promise_handler_t* next_handler = handler->next;
if(handler->takeover_data)
takeover_handler = handler;
else
handler->then(promise->resolve_data,handler->then_ctx,NULL,NULL);
handler = next_handler;
}
if(takeover_handler)
{
promise->data_taken_over = true;
takeover_handler->then(promise->resolve_data,takeover_handler->then_ctx,promise->free_data,promise->free_data_ctx);
}
/** free promise and remove from promises if it is still in promises */
promise_t* old_promise = map_remove(manager->promises,&promise_handle,sizeof(promise_handle));
promise_free(old_promise);
}
else if(promise->rejected)
{
/** Promise is already rejected but not catched */
promise_handler_t* handler = promise->first_handler;
promise_handler_t* takeover_handler = NULL;
while(handler)
{
promise_handler_t* next_handler = handler->next;
if(handler->takeover_reason)
takeover_handler = handler;
else
handler->catch(promise->reject_reason,handler->catch_ctx,NULL,NULL);
handler = next_handler;
}
if(takeover_handler)
{
promise->reason_taken_over = true;
takeover_handler->catch(promise->reject_reason,takeover_handler->catch_ctx,promise->free_reason,promise->free_reason_ctx);
}
/** free promise and remove from promises if it is still in promises */
promise_t* old_promise = map_remove(manager->promises,&promise_handle,sizeof(promise_handle));
promise_free(old_promise);
}
return 0;
error:
return -1;
}
/** static functions */
static void promise_free(promise_t* promise)
{
if(promise)
{
if(promise->free_data && (!promise->data_taken_over))
promise->free_data(promise->resolve_data.ptr,promise->free_data_ctx);
if(promise->free_reason && (!promise->reason_taken_over))
promise->free_reason(promise->reject_reason.ptr,promise->free_reason_ctx);
promise_handler_t* handler = promise->first_handler;
while(handler)
{
promise_handler_t* next = handler->next;
free(handler);
handler = next;
}
if(promise->internal.free_data)
promise->internal.free_data(promise->internal.data,promise->internal.free_ctx);
free(promise);
}
}
static void promise_free_with_ctx(void* data, void* ctx)
{
promise_t* promise = (promise_t*)data;
promise_free(promise);
}
/** promise group ****************************************/
typedef struct promise_group_sub_promise_ctx_s promise_group_sub_promise_ctx_t;
typedef struct promise_group_s promise_group_t;
struct promise_group_sub_promise_ctx_s
{
int index;
promise_handle_t promise;
promise_group_t* group;
};
struct promise_group_s
{
promise_manager_handle_t manager;
promise_handle_t promise;
int length;
promise_group_sub_promise_ctx_t* sub_promises;
promise_data_list_t* data_list;
int data_count;
};
static void promise_group_free(promise_group_t* group);
static void promise_group_free_with_ctx(void* data, void* ctx);
static promise_group_t* promise_group_new(promise_manager_t* manager, int n, promise_handle_t* promises)
{
promise_group_t* group = malloc(sizeof(promise_group_t));
if(!group)
goto error;
memset(group,0,sizeof(promise_group_t));
group->manager = manager;
group->length = n;
group->data_count = 0; /** resolve/reject data count */
group->data_list = malloc(sizeof(promise_data_list_t));
if(!group)
goto error;
memset(group->data_list,0,sizeof(promise_data_list_t));
group->data_list->length = n;
group->data_list->items = malloc(sizeof(promise_data_list_item_t)*n);
if(!group->data_list->items)
goto error;
memset(group->data_list->items,0,sizeof(promise_data_list_item_t)*n);
for(int i=0;i<n;i++)
{
group->data_list->items[i].internal.free_ptr = NULL;
group->data_list->items[i].internal.free_ctx = NULL;
}
group->sub_promises = malloc(sizeof(promise_group_sub_promise_ctx_t)*n);
if(!group->sub_promises)
goto error;
memset(group->sub_promises,0,sizeof(promise_group_sub_promise_ctx_t)*n);
for(int i=0;i<n;i++)
{
group->sub_promises[i].promise = promises[i];
group->sub_promises[i].index = i;
group->sub_promises[i].group = group;
}
group->promise = promise_new_internal(manager,group,promise_group_free_with_ctx,NULL);
if(!group->promise)
goto error;
return group;
error:
promise_group_free(group);
return NULL;
}
/** promsie_group_t data list free */
static void promise_group_free_data_list(promise_data_list_t* list)
{
if(list)
{
if(list->items)
{
for(int i=0;i<list->length;i++)
{
if(list->items[i].internal.free_ptr)
list->items[i].internal.free_ptr(list->items[i].data.ptr,list->items[i].internal.free_ctx);
}
free(list->items);
}
free(list);
}
}
static void promise_group_free_data_list_with_ctx(void* data, void* ctx)
{
promise_group_free_data_list((promise_data_list_t*)data);
}
/** promise_group_t free */
static void promise_group_free(promise_group_t* group)
{
if(group)
{
if((group->length != group->data_count) && group->data_list) /** not all resolved/rejected, free data */
promise_group_free_data_list(group->data_list);
if(group->sub_promises)
{
for(int i=0;i<group->length;i++) /** destroy remeaning sub promises */
promise_destroy(group->manager,group->sub_promises[i].promise);
free(group->sub_promises);
}
free(group);
}
}
static void promise_group_free_with_ctx(void* data, void* ctx)
{
promise_group_free((promise_group_t*)data);
}
/** promise.all ****************************************/
static void promise_all_sub_promise_then(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx);
static void promise_all_sub_promise_catch(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx);
promise_handle_t promise_all_n(promise_manager_handle_t manager, int n, promise_handle_t* promises)
{
if(!promises)
return NULL;
promise_group_t* all = promise_group_new(manager,n,promises);
if(!all)
goto error;
/** await all sub promises */
for(int i=0;i<n;i++)
{
if(promise_await(
manager,all->sub_promises[i].promise,
promise_all_sub_promise_then,&(all->sub_promises[i]),true,
promise_all_sub_promise_catch,&(all->sub_promises[i]),true)!=0)
{
goto error;
}
}
return all->promise;
error:
if(all)
promise_destroy(manager,all->promise);
return NULL;
}
promise_handle_t promise_all_v(promise_manager_handle_t manager, int n, va_list args)
{
promise_handle_t* promises = malloc(sizeof(promise_handle_t)*n);
if(!promises)
return NULL;
for(int i=0; i<n; i++)
{
promises[i] = va_arg(args, promise_handle_t);
}
promise_handle_t promise = promise_all_n(manager, n, promises);
free(promises);
return promise;
}
promise_handle_t promise_all(promise_manager_handle_t manager, int n,...)
{
promise_handle_t result = NULL;
va_list args;
va_start(args,n);
result = promise_all_v(manager,n,args);
va_end(args);
return result;
}
static void promise_all_sub_promise_then(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx)
{
promise_group_sub_promise_ctx_t* ctx = (promise_group_sub_promise_ctx_t*)user;
ctx->group->data_count++;
ctx->group->data_list->items[ctx->index].data = data;
ctx->group->data_list->items[ctx->index].internal.free_ptr = free_ptr;
ctx->group->data_list->items[ctx->index].internal.free_ctx = free_ctx;
if(ctx->group->data_count == ctx->group->length)
{
/** all resolved */
promise_data_t data_list = {.ptr = ctx->group->data_list};
promise_resolve(ctx->group->manager,ctx->group->promise,data_list,promise_group_free_data_list_with_ctx,NULL);
}
}
static void promise_all_sub_promise_catch(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx)
{
promise_group_sub_promise_ctx_t* ctx = (promise_group_sub_promise_ctx_t*)user;
/** rejct the all promise */
promise_reject(ctx->group->manager,ctx->group->promise,data,free_ptr,free_ctx);
}
/** promise.any ****************************************/
static void promise_any_sub_promise_then(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx);
static void promise_any_sub_promise_catch(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx);
promise_handle_t promise_any_n(promise_manager_handle_t manager, int n, promise_handle_t* promises)
{
if(!promises)
return NULL;
promise_group_t* any = promise_group_new(manager,n,promises);
if(!any)
goto error;
/** await all sub promises */
for(int i=0;i<n;i++)
{
if(promise_await(
manager,any->sub_promises[i].promise,
promise_any_sub_promise_then,&(any->sub_promises[i]),true,
promise_any_sub_promise_catch,&(any->sub_promises[i]),true)!=0)
{
goto error;
}
}
return any->promise;
error:
if(any)
promise_destroy(manager,any->promise);
return NULL;
}
promise_handle_t promise_any_v(promise_manager_handle_t manager, int n, va_list args)
{
promise_handle_t* promises = malloc(sizeof(promise_handle_t)*n);
if(!promises)
return NULL;
for(int i=0; i<n; i++)
{
promises[i] = va_arg(args, promise_handle_t);
}
promise_handle_t promise = promise_any_n(manager, n, promises);
free(promises);
return promise;
}
promise_handle_t promise_any(promise_manager_handle_t manager, int n,...)
{
promise_handle_t result = NULL;
va_list args;
va_start(args,n);
result = promise_any_v(manager,n,args);
va_end(args);
return result;
}
static void promise_any_sub_promise_then(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx)
{
promise_group_sub_promise_ctx_t* ctx = (promise_group_sub_promise_ctx_t*)user;
/** resolve the any promise */
promise_resolve(ctx->group->manager,ctx->group->promise,data,free_ptr,free_ctx);
}
static void promise_any_sub_promise_catch(promise_data_t data, void* user, void(*free_ptr)(void*, void*), void* free_ctx)
{
promise_group_sub_promise_ctx_t* ctx = (promise_group_sub_promise_ctx_t*)user;
ctx->group->data_count++;
ctx->group->data_list->items[ctx->index].data = data;
ctx->group->data_list->items[ctx->index].internal.free_ptr = free_ptr;
ctx->group->data_list->items[ctx->index].internal.free_ctx = free_ctx;
if(ctx->group->data_count == ctx->group->length)
{
/** all rejected */
promise_data_t data_list = {.ptr = ctx->group->data_list};
promise_reject(ctx->group->manager,ctx->group->promise,data_list,promise_group_free_data_list_with_ctx,NULL);
}
}