-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask_group.c
More file actions
1762 lines (1451 loc) · 49.5 KB
/
task_group.c
File metadata and controls
1762 lines (1451 loc) · 49.5 KB
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Edmond |
+----------------------------------------------------------------------+
*/
#include "task_group.h"
#include "task_group_arginfo.h"
#include "task_set_arginfo.h"
#include "exceptions.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
/* Task entry states — stored as IS_PTR in unified tasks HashTable */
typedef enum
{
TASK_STATE_PENDING, /* callable waiting in queue (owns zend_fcall_t) */
TASK_STATE_RUNNING, /* coroutine executing */
TASK_STATE_ERROR /* coroutine finished with exception */
} task_state_t;
typedef struct
{
task_state_t state;
union
{
zend_fcall_t *fcall; /* TASK_STATE_PENDING (owned, freed on transition or cancel) */
zend_object *coroutine; /* TASK_STATE_RUNNING (ref-counted) */
zend_object *exception; /* TASK_STATE_ERROR (ref-counted) */
};
} task_entry_t;
/* Waiter types determine notification and lifetime semantics */
typedef enum
{
WAITER_TYPE_RACE, /* resolve future on any completion (success or error) */
WAITER_TYPE_ANY, /* resolve future on first success, reject when all failed */
WAITER_TYPE_ALL, /* resolve future when all settled, reject on errors */
WAITER_TYPE_ALL_IGNORE_ERRORS, /* resolve future when all settled, ignore errors */
WAITER_TYPE_ITERATOR /* notify via callbacks on any completion (no future) */
} task_group_waiter_type_t;
/* Waiter event — union of event (for iterator) and future (for race/any/all).
* zend_future_t has zend_async_event_t as first member, so cast to event always works.
* For RACE/ANY/ALL: created via ZEND_ASYNC_NEW_FUTURE_EX, resolved by group, returned as Future.
* For ITERATOR: uses event part only, notified via ZEND_ASYNC_CALLBACKS_NOTIFY. */
struct _task_group_waiter_event_s
{
union
{
zend_async_event_t event; /* ITERATOR: lightweight event */
zend_future_t future; /* RACE/ANY/ALL: full future */
};
async_task_group_t *group;
task_group_waiter_type_t type;
zend_async_event_dispose_t original_dispose;
};
/* Forward declarations */
static void task_group_waiter_event_remove(const task_group_waiter_event_t *waiter);
///////////////////////////////////////////////////////////
/// Waiter event vtable (ITERATOR only — lightweight event)
///////////////////////////////////////////////////////////
static bool waiter_event_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_push(event, callback);
}
static bool waiter_event_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_remove(event, callback);
}
static bool waiter_event_start(zend_async_event_t *event)
{
return true;
}
static bool waiter_event_stop(zend_async_event_t *event)
{
return true;
}
static bool waiter_iterator_dispose(zend_async_event_t *event)
{
task_group_waiter_event_t *waiter = (task_group_waiter_event_t *) event;
task_group_waiter_event_remove(waiter);
zend_async_callbacks_free(event);
efree(waiter);
return true;
}
///////////////////////////////////////////////////////////
/// Waiter future dispose (RACE/ANY/ALL — extends zend_future_t)
///////////////////////////////////////////////////////////
static bool waiter_future_dispose(zend_async_event_t *event)
{
task_group_waiter_event_t *waiter = (task_group_waiter_event_t *) event;
task_group_waiter_event_remove(waiter);
/* Call original zend_future_t dispose (frees callbacks, result, etc.) */
return waiter->original_dispose(event);
}
///////////////////////////////////////////////////////////
/// Waiter creation
///////////////////////////////////////////////////////////
static void task_group_waiter_add_to_vector(async_task_group_t *group, task_group_waiter_event_t *waiter)
{
if (group->waiter_events_length == group->waiter_events_capacity) {
const uint32_t new_cap = group->waiter_events_capacity ? group->waiter_events_capacity * 2 : 4;
group->waiter_events = safe_erealloc(group->waiter_events, new_cap, sizeof(task_group_waiter_event_t *), 0);
group->waiter_events_capacity = new_cap;
}
group->waiter_events[group->waiter_events_length++] = waiter;
}
/* Create iterator waiter — lightweight event, no future */
static task_group_waiter_event_t *task_group_waiter_iterator_new(async_task_group_t *group)
{
task_group_waiter_event_t *waiter = ecalloc(1, sizeof(task_group_waiter_event_t));
waiter->event.ref_count = 1;
waiter->event.add_callback = waiter_event_add_callback;
waiter->event.del_callback = waiter_event_del_callback;
waiter->event.start = waiter_event_start;
waiter->event.stop = waiter_event_stop;
waiter->event.dispose = waiter_iterator_dispose;
waiter->group = group;
waiter->type = WAITER_TYPE_ITERATOR;
task_group_waiter_add_to_vector(group, waiter);
return waiter;
}
/* Create future waiter for race/any/all — extends zend_future_t */
static task_group_waiter_event_t *task_group_waiter_future_new(async_task_group_t *group,
const task_group_waiter_type_t type)
{
const size_t extra_size = sizeof(task_group_waiter_event_t) - sizeof(zend_future_t);
zend_future_t *future = ZEND_ASYNC_NEW_FUTURE_EX(false, extra_size);
task_group_waiter_event_t *waiter = (task_group_waiter_event_t *) future;
waiter->group = group;
waiter->type = type;
/* Intercept dispose to remove from group vector */
waiter->original_dispose = future->event.dispose;
future->event.dispose = waiter_future_dispose;
task_group_waiter_add_to_vector(group, waiter);
return waiter;
}
static void task_group_waiter_event_remove(const task_group_waiter_event_t *waiter)
{
async_task_group_t *group = waiter->group;
if (UNEXPECTED(group == NULL)) {
return;
}
for (uint32_t i = 0; i < group->waiter_events_length; i++) {
if (group->waiter_events[i] == waiter) {
/* Swap with last */
group->waiter_events[i] = group->waiter_events[--group->waiter_events_length];
/* Adjust active iterator */
if (i < group->waiter_notify_index) {
group->waiter_notify_index--;
}
return;
}
}
}
#define METHOD(name) PHP_METHOD(Async_TaskGroup, name)
#define THIS_GROUP() ASYNC_TASK_GROUP_FROM_OBJ(Z_OBJ_P(ZEND_THIS))
zend_class_entry *async_ce_task_group = NULL;
zend_class_entry *async_ce_task_set = NULL;
static zend_object_handlers task_group_handlers;
static zend_object_handlers task_set_handlers;
/* Forward declarations */
static void task_group_try_complete(async_task_group_t *group);
static void task_group_drain(async_task_group_t *group);
static bool task_group_has_errors(const async_task_group_t *group);
static bool task_group_all_settled(const async_task_group_t *group);
static bool task_group_has_pending(const async_task_group_t *group);
static HashTable *task_group_collect_results(const async_task_group_t *group);
static zend_object *task_group_collect_composite_exception(const async_task_group_t *group);
static void task_group_on_coroutine_complete(zend_async_event_t *event,
zend_async_event_callback_t *callback,
void *result,
zend_object *exception);
///////////////////////////////////////////////////////////
/// task_entry_t lifecycle
///////////////////////////////////////////////////////////
static task_entry_t *task_entry_new_pending(zend_fcall_t *fcall)
{
task_entry_t *entry = emalloc(sizeof(task_entry_t));
entry->state = TASK_STATE_PENDING;
entry->fcall = fcall;
return entry;
}
static task_entry_t *task_entry_new_running(zend_object *coroutine)
{
task_entry_t *entry = emalloc(sizeof(task_entry_t));
entry->state = TASK_STATE_RUNNING;
entry->coroutine = coroutine;
GC_ADDREF(coroutine);
return entry;
}
static void task_entry_free(task_entry_t *entry)
{
switch (entry->state) {
case TASK_STATE_PENDING:
zend_fcall_release(entry->fcall);
break;
case TASK_STATE_RUNNING:
OBJ_RELEASE(entry->coroutine);
break;
case TASK_STATE_ERROR:
OBJ_RELEASE(entry->exception);
break;
}
efree(entry);
}
/* Custom dtor for tasks HashTable elements */
static void task_zval_dtor(zval *zv)
{
if (Z_TYPE_P(zv) == IS_PTR) {
task_entry_free((task_entry_t *) Z_PTR_P(zv));
} else {
zval_ptr_dtor(zv);
}
}
///////////////////////////////////////////////////////////
/// Helpers: check task state in unified array
///////////////////////////////////////////////////////////
static zend_always_inline bool task_is_pending(const zval *zv)
{
return Z_TYPE_P(zv) == IS_PTR && ((task_entry_t *) Z_PTR_P(zv))->state == TASK_STATE_PENDING;
}
static zend_always_inline bool task_is_error(const zval *zv)
{
return Z_TYPE_P(zv) == IS_PTR && ((task_entry_t *) Z_PTR_P(zv))->state == TASK_STATE_ERROR;
}
static zend_always_inline bool task_is_completed(const zval *zv)
{
return Z_TYPE_P(zv) != IS_PTR;
}
/* Check if all tasks in the group are settled (success or error) */
static zend_always_inline bool task_group_all_settled(const async_task_group_t *group)
{
return group->active_coroutines == 0;
}
/* Check if there are any pending tasks */
static bool task_group_has_pending(const async_task_group_t *group)
{
zval *zv;
ZEND_HASH_FOREACH_VAL(&group->tasks, zv)
{
if (task_is_pending(zv)) {
return true;
}
}
ZEND_HASH_FOREACH_END();
return false;
}
///////////////////////////////////////////////////////////
/// Extended callback — holds task key reference
///////////////////////////////////////////////////////////
typedef struct
{
zend_coroutine_event_callback_t base;
async_task_group_t *group;
zval key;
} task_group_coroutine_callback_t;
static void task_group_callback_dispose(zend_async_event_callback_t *callback, zend_async_event_t *event)
{
task_group_coroutine_callback_t *cb = (task_group_coroutine_callback_t *) callback;
zval_ptr_dtor(&cb->key);
efree(cb);
}
///////////////////////////////////////////////////////////
/// Event vtable
///////////////////////////////////////////////////////////
static bool task_group_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_push(event, callback);
}
static bool task_group_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_remove(event, callback);
}
static bool task_group_start(zend_async_event_t *event)
{
return true;
}
static bool task_group_stop(zend_async_event_t *event)
{
return true;
}
static bool task_group_dispose(zend_async_event_t *event)
{
return true;
}
static bool task_group_replay(zend_async_event_t *event,
zend_async_event_callback_t *callback,
zval *result,
zend_object **exception)
{
const async_task_group_t *group = ASYNC_TASK_GROUP_FROM_EVENT(event);
if (task_group_all_settled(group) && !task_group_has_pending(group)) {
if (callback != NULL) {
zval undef;
ZVAL_UNDEF(&undef);
callback->callback(event, callback, &undef, NULL);
return true;
}
if (result != NULL) {
ZVAL_NULL(result);
}
if (exception != NULL) {
*exception = NULL;
}
return true;
}
return false;
}
static zend_string *task_group_info(zend_async_event_t *event)
{
const async_task_group_t *group = ASYNC_TASK_GROUP_FROM_EVENT(event);
const uint32_t total = zend_hash_num_elements(&group->tasks);
const char *name = ASYNC_TASK_GROUP_IS_TASK_SET(group) ? "TaskSet" : "TaskGroup";
return zend_strpprintf(0, "%s(total=%u, active=%d)", name, total, group->active_coroutines);
}
static void task_group_event_init(async_task_group_t *group)
{
zend_async_event_t *event = &group->event;
memset(event, 0, sizeof(zend_async_event_t));
event->flags = ZEND_ASYNC_EVENT_F_ZEND_OBJ;
event->zend_object_offset = XtOffsetOf(async_task_group_t, std);
event->add_callback = task_group_add_callback;
event->del_callback = task_group_del_callback;
event->start = task_group_start;
event->stop = task_group_stop;
event->dispose = task_group_dispose;
event->replay = task_group_replay;
event->info = task_group_info;
}
///////////////////////////////////////////////////////////
/// Object lifecycle
///////////////////////////////////////////////////////////
static zend_object *task_group_create_object(zend_class_entry *ce)
{
async_task_group_t *group = zend_object_alloc(sizeof(async_task_group_t), ce);
zend_object_std_init(&group->std, ce);
group->std.handlers = &task_group_handlers;
task_group_event_init(group);
group->scope = NULL;
group->concurrency = 0;
group->active_coroutines = 0;
group->next_key = 0;
group->finally_handlers = NULL;
zend_hash_init(&group->tasks, 8, NULL, task_zval_dtor, 0);
group->waiter_events = NULL;
group->waiter_events_length = 0;
group->waiter_events_capacity = 0;
return &group->std;
}
static void task_group_dtor_object(zend_object *object)
{
async_task_group_t *group = ASYNC_TASK_GROUP_FROM_OBJ(object);
/* Report unhandled errors to scope exception handler */
if (!ZEND_ASYNC_EVENT_IS_EXCEPTION_HANDLED(&group->event) && group->scope != NULL) {
bool has_errors = false;
zend_object *composite = NULL;
zval *zv;
ZEND_HASH_FOREACH_VAL(&group->tasks, zv)
{
if (task_is_error(zv)) {
if (!has_errors) {
composite = async_new_composite_exception();
has_errors = true;
}
async_composite_exception_add_exception(composite, ((task_entry_t *) Z_PTR_P(zv))->exception, false);
}
}
ZEND_HASH_FOREACH_END();
if (has_errors) {
ZEND_ASYNC_SCOPE_CATCH(&group->scope->scope, NULL, NULL, composite, true, false);
}
}
/* Dispose owned scope (analogous to scope_destroy for Scope objects).
* Must happen in dtor, not free, because coroutines may still be alive. */
if (group->scope != NULL) {
zend_async_scope_t *scope = (zend_async_scope_t *) group->scope;
group->scope = NULL;
// The Scope is owned by the Scheduler, so it is not allowed to decrement ref_count = 1 on our own.
// However, we do own a reference to the Scope.
if (ZEND_ASYNC_EVENT_REFCOUNT(&scope->event) > 1) {
ZEND_ASYNC_EVENT_DEL_REF(&scope->event);
}
// If the Scope still cannot be completed, we must cancel all remaining coroutines.
if (false == ZEND_ASYNC_SCOPE_IS_COMPLETELY_DONE(scope)) {
zend_object *exception = async_new_exception(async_ce_cancellation_exception,
"Scope is being disposed due to TaskGroup destruction");
ZEND_ASYNC_SCOPE_CANCEL(scope, exception, true, ZEND_ASYNC_SCOPE_IS_DISPOSE_SAFELY(scope));
} else {
// Correctly finalize the Scope.
scope->try_to_dispose(scope);
}
}
zend_object_std_dtor(object);
}
static void task_group_free_object(zend_object *object)
{
async_task_group_t *group = ASYNC_TASK_GROUP_FROM_OBJ(object);
zend_hash_destroy(&group->tasks);
/* Dispose remaining waiter events */
if (group->waiter_events != NULL) {
for (uint32_t i = 0; i < group->waiter_events_length; i++) {
task_group_waiter_event_t *waiter = group->waiter_events[i];
waiter->group = NULL; /* detach so dispose won't try to remove from vector */
waiter->event.dispose(&waiter->event);
}
efree(group->waiter_events);
group->waiter_events = NULL;
group->waiter_events_length = 0;
}
/* Free finally handlers */
if (group->finally_handlers != NULL) {
zend_array_destroy(group->finally_handlers);
group->finally_handlers = NULL;
}
/* Free event callbacks */
zend_async_callbacks_free(&group->event);
/* Scope is released in dtor_obj, but guard against leaks */
ZEND_ASSERT(group->scope == NULL && "Scope should have been released in dtor");
}
static HashTable *task_group_get_gc(zend_object *object, zval **table, int *n)
{
async_task_group_t *group = ASYNC_TASK_GROUP_FROM_OBJ(object);
zend_get_gc_buffer *buf = zend_get_gc_buffer_create();
/* GC all task entries */
zval *zv;
ZEND_HASH_FOREACH_VAL(&group->tasks, zv)
{
if (Z_TYPE_P(zv) == IS_PTR) {
task_entry_t *entry = (task_entry_t *) Z_PTR_P(zv);
switch (entry->state) {
case TASK_STATE_PENDING:
if (entry->fcall != NULL) {
zend_get_gc_buffer_add_zval(buf, &entry->fcall->fci.function_name);
for (uint32_t p = 0; p < entry->fcall->fci.param_count; p++) {
zend_get_gc_buffer_add_zval(buf, &entry->fcall->fci.params[p]);
}
}
break;
case TASK_STATE_RUNNING:
zend_get_gc_buffer_add_obj(buf, entry->coroutine);
break;
case TASK_STATE_ERROR:
zend_get_gc_buffer_add_obj(buf, entry->exception);
break;
}
} else {
zend_get_gc_buffer_add_zval(buf, zv);
}
}
ZEND_HASH_FOREACH_END();
/* GC finally handlers */
if (group->finally_handlers != NULL) {
ZEND_HASH_FOREACH_VAL(group->finally_handlers, zv)
{
zend_get_gc_buffer_add_zval(buf, zv);
}
ZEND_HASH_FOREACH_END();
}
zend_get_gc_buffer_use(buf, table, n);
return NULL;
}
///////////////////////////////////////////////////////////
/// Internal functions
///////////////////////////////////////////////////////////
static void task_group_finally_handlers_dtor(finally_handlers_context_t *context)
{
async_task_group_t *group = (async_task_group_t *) context->target;
if (group != NULL) {
context->target = NULL;
OBJ_RELEASE(&group->std);
}
}
/* TaskSet: remove a delivered entry by key zval (IS_STRING or IS_LONG). */
static zend_always_inline void task_set_remove_entry_by_zval(async_task_group_t *group, const zval *key)
{
if (!ASYNC_TASK_GROUP_IS_TASK_SET(group)) {
return;
}
if (Z_TYPE_P(key) == IS_STRING) {
zend_hash_del(&group->tasks, Z_STR_P(key));
} else {
zend_hash_index_del(&group->tasks, Z_LVAL_P(key));
}
}
/* TaskSet: remove a delivered entry by hash key (for FOREACH_KEY_VAL loops). */
static zend_always_inline void
task_set_remove_entry(async_task_group_t *group, zend_string *str_key, const zend_ulong num_key)
{
if (!ASYNC_TASK_GROUP_IS_TASK_SET(group)) {
return;
}
if (str_key != NULL) {
zend_hash_del(&group->tasks, str_key);
} else {
zend_hash_index_del(&group->tasks, num_key);
}
}
/* TaskSet: remove all entries after bulk delivery (joinAll). */
static zend_always_inline void task_set_remove_all_entries(async_task_group_t *group)
{
if (ASYNC_TASK_GROUP_IS_TASK_SET(group)) {
zend_hash_clean(&group->tasks);
}
}
static void task_group_try_complete(async_task_group_t *group)
{
if (group->active_coroutines > 0 || task_group_has_pending(group)) {
return;
}
/* Resolve future waiters when all tasks are settled.
* Use index-based iteration — resolve/remove may shift the vector. */
const bool has_errors = task_group_has_errors(group);
uint32_t i = 0;
while (i < group->waiter_events_length) {
task_group_waiter_event_t *waiter = group->waiter_events[i];
switch (waiter->type) {
case WAITER_TYPE_ALL:
if (has_errors) {
zend_object *composite = task_group_collect_composite_exception(group);
ZEND_FUTURE_REJECT(&waiter->future, composite);
// ZEND_FUTURE_SET_EXCEPTION_CAUGHT(&waiter->future);
OBJ_RELEASE(composite);
} else {
HashTable *results = task_group_collect_results(group);
zval results_zv;
ZVAL_ARR(&results_zv, results);
ZEND_FUTURE_COMPLETE(&waiter->future, &results_zv);
zval_ptr_dtor(&results_zv);
}
task_set_remove_all_entries(group);
task_group_waiter_event_remove(waiter);
break;
case WAITER_TYPE_ALL_IGNORE_ERRORS: {
HashTable *results = task_group_collect_results(group);
zval results_zv;
ZVAL_ARR(&results_zv, results);
ZEND_FUTURE_COMPLETE(&waiter->future, &results_zv);
zval_ptr_dtor(&results_zv);
task_set_remove_all_entries(group);
task_group_waiter_event_remove(waiter);
continue;
}
case WAITER_TYPE_ANY: {
zend_object *composite = task_group_collect_composite_exception(group);
ZEND_FUTURE_REJECT(&waiter->future, composite);
// ZEND_FUTURE_SET_EXCEPTION_CAUGHT(&waiter->future);
OBJ_RELEASE(composite);
task_group_waiter_event_remove(waiter);
break;
}
case WAITER_TYPE_ITERATOR:
ZEND_ASYNC_CALLBACKS_NOTIFY(&waiter->event, NULL, NULL);
i++;
break;
case WAITER_TYPE_RACE:
i++;
break;
}
i++;
}
if (ASYNC_TASK_GROUP_IS_SEALED(group)) {
ASYNC_TASK_GROUP_SET_COMPLETED(group);
}
/* Notify all/await waiters — group is fully settled */
ZEND_ASYNC_CALLBACKS_NOTIFY(&group->event, NULL, NULL);
ZEND_ASYNC_EVENT_SET_CLOSED(&group->event);
/* Fire finally handlers asynchronously */
if (group->finally_handlers != NULL && zend_hash_num_elements(group->finally_handlers) > 0) {
HashTable *handlers = group->finally_handlers;
group->finally_handlers = NULL;
finally_handlers_context_t *context = ecalloc(1, sizeof(finally_handlers_context_t));
context->target = group;
context->scope = &group->scope->scope;
context->dtor = task_group_finally_handlers_dtor;
context->params_count = 1;
ZVAL_OBJ(&context->params[0], &group->std);
if (async_call_finally_handlers(handlers, context, 0)) {
GC_ADDREF(&group->std);
ZEND_ASYNC_EVENT_ADD_REF(&group->scope->scope.event);
} else {
efree(context);
zend_array_destroy(handlers);
}
}
}
static bool task_group_has_slot(const async_task_group_t *group)
{
return group->concurrency == 0 || group->active_coroutines < (int32_t) group->concurrency;
}
static void
task_group_spawn_coroutine(async_task_group_t *group, zend_fcall_t *fcall, zval *key, task_entry_t *pending_entry)
{
/* Create coroutine in group's scope */
async_coroutine_t *coroutine = (async_coroutine_t *) ZEND_ASYNC_SPAWN_WITH(&group->scope->scope);
if (UNEXPECTED(coroutine == NULL || EG(exception))) {
return;
}
/* Transfer fcall ownership to coroutine */
coroutine->coroutine.fcall = fcall;
/* Transition entry: PENDING → RUNNING */
if (pending_entry != NULL) {
/* fcall ownership transferred to coroutine, clear pointer without releasing */
pending_entry->fcall = NULL;
pending_entry->state = TASK_STATE_RUNNING;
pending_entry->coroutine = &coroutine->std;
GC_ADDREF(&coroutine->std);
} else {
/* Direct spawn — create RUNNING entry in tasks */
task_entry_t *entry = task_entry_new_running(&coroutine->std);
zval ptr_zv;
ZVAL_PTR(&ptr_zv, entry);
if (Z_TYPE_P(key) == IS_STRING) {
zend_hash_add_new(&group->tasks, Z_STR_P(key), &ptr_zv);
} else {
zend_hash_index_add_new(&group->tasks, Z_LVAL_P(key), &ptr_zv);
}
}
/* Create extended callback with key */
task_group_coroutine_callback_t *cb = ecalloc(1, sizeof(task_group_coroutine_callback_t));
cb->base.base.ref_count = 0;
cb->base.base.callback = task_group_on_coroutine_complete;
cb->base.base.dispose = task_group_callback_dispose;
cb->base.coroutine = NULL;
cb->base.event = &coroutine->coroutine.event;
cb->group = group;
ZVAL_COPY(&cb->key, key);
coroutine->coroutine.event.add_callback(&coroutine->coroutine.event, &cb->base.base);
group->active_coroutines++;
GC_ADDREF(&group->std);
}
/* Drain pending tasks using internal pointer */
static void task_group_drain(async_task_group_t *group)
{
zval *zv;
zend_hash_internal_pointer_reset(&group->tasks);
while (task_group_has_slot(group)) {
/* Scan forward to find next PENDING entry */
bool found = false;
while ((zv = zend_hash_get_current_data(&group->tasks)) != NULL) {
if (task_is_pending(zv)) {
found = true;
break;
}
zend_hash_move_forward(&group->tasks);
}
if (!found) {
break;
}
task_entry_t *entry = (task_entry_t *) Z_PTR_P(zv);
zval key_zv;
zend_hash_get_current_key_zval(&group->tasks, &key_zv);
task_group_spawn_coroutine(group, entry->fcall, &key_zv, entry);
zval_ptr_dtor(&key_zv);
zend_hash_move_forward(&group->tasks);
if (UNEXPECTED(EG(exception))) {
break;
}
}
}
static void task_group_on_coroutine_complete(zend_async_event_t *event,
zend_async_event_callback_t *callback,
void *result,
zend_object *exception)
{
const task_group_coroutine_callback_t *group_callback = (task_group_coroutine_callback_t *) callback;
async_task_group_t *group = group_callback->group;
zval *slot;
task_entry_t *old_entry;
/* Find the slot in tasks HashTable */
if (Z_TYPE(group_callback->key) == IS_STRING) {
slot = zend_hash_find(&group->tasks, Z_STR(group_callback->key));
} else {
slot = zend_hash_index_find(&group->tasks, Z_LVAL(group_callback->key));
}
if (UNEXPECTED(slot == NULL)) {
goto done;
}
old_entry = (task_entry_t *) Z_PTR_P(slot);
if (exception != NULL) {
/* Mark exception as handled on the coroutine so it won't propagate */
ZEND_ASYNC_EVENT_SET_EXCEPTION_HANDLED(event);
/* Transition: RUNNING → ERROR */
OBJ_RELEASE(old_entry->coroutine);
old_entry->state = TASK_STATE_ERROR;
old_entry->exception = exception;
GC_ADDREF(exception);
/* New error → reset EXCEPTION_HANDLED on group */
ZEND_ASYNC_EVENT_CLR_EXCEPTION_HANDLED(&group->event);
} else {
/* Transition: RUNNING → success (replace IS_PTR with result zval) */
const zval *result_zv = (zval *) result;
zval result_copy;
if (result_zv != NULL && Z_TYPE_P(result_zv) != IS_UNDEF) {
ZVAL_COPY(&result_copy, result_zv);
} else {
ZVAL_NULL(&result_copy);
}
/* Release coroutine ref (added in task_entry_new_running), then free entry */
OBJ_RELEASE(old_entry->coroutine);
efree(old_entry);
ZVAL_COPY_VALUE(slot, &result_copy);
}
done:
group->active_coroutines--;
/* Drain pending tasks */
task_group_drain(group);
/* Notify waiter events based on type.
* Use waiter_notify_index for safe iteration — remove may shift vector. */
const bool is_success = (exception == NULL);
const zval *result_zval = (zval *) result;
group->waiter_notify_index = 0;
while (group->waiter_notify_index < group->waiter_events_length) {
task_group_waiter_event_t *waiter = group->waiter_events[group->waiter_notify_index];
switch (waiter->type) {
case WAITER_TYPE_RACE:
/* Resolve future with first completion (success or error) */
if (is_success) {
ZEND_FUTURE_COMPLETE(&waiter->future, result_zval);
} else {
ZEND_FUTURE_REJECT(&waiter->future, exception);
// ZEND_FUTURE_SET_EXCEPTION_CAUGHT(&waiter->future);
}
task_set_remove_entry_by_zval(group, &group_callback->key);
task_group_waiter_event_remove(waiter);
continue; /* don't increment — remove shifted vector */
case WAITER_TYPE_ANY:
if (!is_success) {
group->waiter_notify_index++;
continue; /* skip errors, wait for success */
}
ZEND_FUTURE_COMPLETE(&waiter->future, result_zval);
task_group_waiter_event_remove(waiter);
continue;
case WAITER_TYPE_ALL:
case WAITER_TYPE_ALL_IGNORE_ERRORS:
/* Don't resolve per-task — wait for terminal state */
group->waiter_notify_index++;
continue;
case WAITER_TYPE_ITERATOR:
group->waiter_notify_index++;
ZEND_ASYNC_CALLBACKS_NOTIFY(&waiter->event, result, NULL);
continue;
}
}
/* Check terminal state */
task_group_try_complete(group);
/* Release group reference (added in spawn_coroutine) */
OBJ_RELEASE(&group->std);
}
///////////////////////////////////////////////////////////
/// Helper: build results/errors arrays from unified tasks
///////////////////////////////////////////////////////////
static HashTable *task_group_collect_results(const async_task_group_t *group)
{
HashTable *ht = zend_new_array(zend_hash_num_elements(&group->tasks));
zval *zv;
zend_string *str_key;
zend_ulong num_key;
ZEND_HASH_FOREACH_KEY_VAL(&group->tasks, num_key, str_key, zv)
{
if (task_is_completed(zv)) {
zval copy;
ZVAL_COPY(©, zv);
if (str_key != NULL) {
zend_hash_add_new(ht, str_key, ©);
} else {
zend_hash_index_add_new(ht, num_key, ©);
}
}
}
ZEND_HASH_FOREACH_END();
return ht;
}
static bool task_group_has_errors(const async_task_group_t *group)
{
zval *zv;
ZEND_HASH_FOREACH_VAL(&group->tasks, zv)
{
if (task_is_error(zv)) {
return true;
}
}
ZEND_HASH_FOREACH_END();
return false;
}
static zend_object *task_group_collect_composite_exception(const async_task_group_t *group)
{
zend_object *composite = async_new_composite_exception();
zval *zv;
ZEND_HASH_FOREACH_VAL(&group->tasks, zv)
{
if (task_is_error(zv)) {
async_composite_exception_add_exception(composite, ((task_entry_t *) Z_PTR_P(zv))->exception, false);
}
}
ZEND_HASH_FOREACH_END();
return composite;
}
static HashTable *task_group_collect_errors(const async_task_group_t *group)
{
HashTable *ht = zend_new_array(4);
zval *zv;
zend_string *str_key;
zend_ulong num_key;
ZEND_HASH_FOREACH_KEY_VAL(&group->tasks, num_key, str_key, zv)
{
if (task_is_error(zv)) {
zval err;
ZVAL_OBJ_COPY(&err, ((task_entry_t *) Z_PTR_P(zv))->exception);
if (str_key != NULL) {
zend_hash_add_new(ht, str_key, &err);
} else {
zend_hash_index_add_new(ht, num_key, &err);
}
}
}
ZEND_HASH_FOREACH_END();
return ht;
}
///////////////////////////////////////////////////////////
/// Iterator
///////////////////////////////////////////////////////////
static void task_group_iterator_dtor(zend_object_iterator *iter)
{
task_group_iterator_t *iterator = (task_group_iterator_t *) iter;
zval_ptr_dtor(&iterator->current);
zval_ptr_dtor(&iterator->current_key);
zval_ptr_dtor(&iter->data);
}
static zend_result task_group_iterator_valid(zend_object_iterator *iter)
{