-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcoroutine.c
More file actions
1645 lines (1323 loc) · 48.3 KB
/
coroutine.c
File metadata and controls
1645 lines (1323 loc) · 48.3 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Edmond |
+----------------------------------------------------------------------+
*/
///////////////////////////////////////////////////////////
/// 1. Headers, Constants, and Declarations
///////////////////////////////////////////////////////////
#include "coroutine.h"
#include "context.h"
#include "coroutine_arginfo.h"
#include "exceptions.h"
#include "iterator.h"
#include "php_async.h"
#include "scheduler.h"
#include "scope.h"
#include "zend_common.h"
#include "zend_exceptions.h"
#include "zend_generators.h"
#include "zend_ini.h"
#include "zend_builtin_functions.h"
#define METHOD(name) PHP_METHOD(Async_Coroutine, name)
#define THIS_COROUTINE ((async_coroutine_t *) ZEND_ASYNC_OBJECT_TO_EVENT(Z_OBJ_P(ZEND_THIS)))
zend_class_entry *async_ce_coroutine = NULL;
static zend_object_handlers coroutine_handlers;
// Forward declarations for internal functions
static void coroutine_call_finally_handlers(async_coroutine_t *coroutine);
static void finally_context_dtor(finally_handlers_context_t *context);
// Forward declarations for event system
static bool coroutine_event_start(zend_async_event_t *event);
static bool coroutine_event_stop(zend_async_event_t *event);
static bool coroutine_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback);
static bool coroutine_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback);
static bool coroutine_replay(zend_async_event_t *event,
zend_async_event_callback_t *callback,
zval *result,
zend_object **exception);
static zend_string *coroutine_info(zend_async_event_t *event);
static bool coroutine_dispose(zend_async_event_t *event);
///////////////////////////////////////////////////////////
/// 2. Object Lifecycle Management
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/// Embedded Waker API
///////////////////////////////////////////////////////////
zend_async_waker_t *async_waker_new(zend_coroutine_t *coroutine)
{
if (UNEXPECTED(coroutine == NULL)) {
coroutine = ZEND_ASYNC_CURRENT_COROUTINE;
}
if (UNEXPECTED(coroutine == NULL)) {
zend_error(E_CORE_ERROR, "Cannot create waker for a coroutine that is not running");
return NULL;
}
async_coroutine_t *async_coroutine = (async_coroutine_t *) coroutine;
zend_async_waker_t *waker = &async_coroutine->waker;
ZEND_ASSERT(waker != NULL && "Embedded waker must never be NULL");
if (waker->status != ZEND_ASYNC_WAKER_NO_STATUS) {
zend_async_waker_clean(coroutine);
}
return waker;
}
void async_waker_destroy(zend_coroutine_t *coroutine)
{
if (UNEXPECTED(coroutine->waker == NULL)) {
return;
}
zend_async_waker_t *waker = coroutine->waker;
if (waker->dtor != NULL) {
waker->dtor(coroutine);
waker->dtor = NULL;
}
waker->status = ZEND_ASYNC_WAKER_NO_STATUS;
if (waker->error != NULL) {
zend_object_release(waker->error);
waker->error = NULL;
}
if (waker->triggered_events != NULL) {
zend_hash_clean(waker->triggered_events);
}
if (waker->filename != NULL) {
zend_string_release(waker->filename);
waker->filename = NULL;
waker->lineno = 0;
}
zval_ptr_dtor(&waker->result);
ZVAL_UNDEF(&waker->result);
zend_hash_clean(&waker->events);
}
static zend_object *coroutine_object_create(zend_class_entry *class_entry)
{
async_coroutine_t *coroutine = zend_object_alloc(sizeof(async_coroutine_t), class_entry);
ZVAL_UNDEF(&coroutine->coroutine.result);
ZEND_ASYNC_EVENT_SET_ZEND_OBJ(&coroutine->coroutine.event);
ZEND_ASYNC_EVENT_SET_NO_FREE_MEMORY(&coroutine->coroutine.event);
ZEND_ASYNC_EVENT_SET_ZEND_OBJ_OFFSET(&coroutine->coroutine.event, XtOffsetOf(async_coroutine_t, std));
/* Initialize embedded waker */
coroutine->coroutine.waker = &coroutine->waker;
/* Initialize waker contents (memory is already zeroed by zend_object_alloc) */
zend_async_waker_init(&coroutine->waker);
/* Initialize switch handlers */
coroutine->coroutine.switch_handlers = NULL;
zend_async_event_t *event = &coroutine->coroutine.event;
event->start = coroutine_event_start;
event->stop = coroutine_event_stop;
event->add_callback = coroutine_add_callback;
event->del_callback = coroutine_del_callback;
event->replay = coroutine_replay;
event->info = coroutine_info;
event->dispose = coroutine_dispose;
coroutine->coroutine.extended_data = NULL;
coroutine->finally_handlers = NULL;
zend_object_std_init(&coroutine->std, class_entry);
object_properties_init(&coroutine->std, class_entry);
return &coroutine->std;
}
static void coroutine_object_destroy(zend_object *object)
{
async_coroutine_t *coroutine = (async_coroutine_t *) ZEND_ASYNC_OBJECT_TO_EVENT(object);
ZEND_ASSERT(ZEND_ASYNC_WAKER_NOT_IN_QUEUE(&coroutine->waker) &&
"Coroutine waker must be dequeued before destruction");
/* Unlink fiber if still attached to prevent use-after-free. */
if (ZEND_COROUTINE_IS_FIBER(&coroutine->coroutine) && coroutine->coroutine.extended_data != NULL) {
zend_fiber *fiber = (zend_fiber *) coroutine->coroutine.extended_data;
fiber->coroutine = NULL;
coroutine->coroutine.extended_data = NULL;
}
if (coroutine->coroutine.scope != NULL) {
async_scope_notify_coroutine_finished(coroutine);
coroutine->coroutine.scope = NULL;
}
if (coroutine->coroutine.fcall) {
zend_fcall_release(coroutine->coroutine.fcall);
coroutine->coroutine.fcall = NULL;
}
if (coroutine->coroutine.context != NULL) {
// If the coroutine has a context, we need to release it.
async_context_t *context = (async_context_t *) coroutine->coroutine.context;
coroutine->coroutine.context = NULL;
async_context_dispose(context);
}
if (coroutine->coroutine.filename) {
zend_string_release_ex(coroutine->coroutine.filename, 0);
coroutine->coroutine.filename = NULL;
}
// Cleanup embedded waker contents: first clean for reuse, then final destroy
ZEND_ASYNC_WAKER_DESTROY(&coroutine->coroutine);
if (coroutine->waker.triggered_events != NULL) {
zend_hash_destroy(coroutine->waker.triggered_events);
efree(coroutine->waker.triggered_events);
coroutine->waker.triggered_events = NULL;
}
zend_hash_destroy(&coroutine->waker.events);
coroutine->coroutine.waker = NULL;
if (coroutine->coroutine.internal_context != NULL) {
zend_async_coroutine_internal_context_dispose(&coroutine->coroutine);
}
zval_ptr_dtor(&coroutine->coroutine.result);
ZVAL_UNDEF(&coroutine->coroutine.result);
if (coroutine->coroutine.exception != NULL) {
// If the coroutine has an exception, we need to release it.
zend_object *exception = coroutine->coroutine.exception;
coroutine->coroutine.exception = NULL;
OBJ_RELEASE(exception);
}
if (coroutine->deferred_cancellation != NULL) {
zend_object *deferred_cancellation = coroutine->deferred_cancellation;
coroutine->deferred_cancellation = NULL;
OBJ_RELEASE(deferred_cancellation);
}
if (coroutine->finally_handlers) {
zend_array_destroy(coroutine->finally_handlers);
coroutine->finally_handlers = NULL;
}
}
static void coroutine_free(zend_object *object)
{
async_coroutine_t *coroutine = (async_coroutine_t *) ZEND_ASYNC_OBJECT_TO_EVENT(object);
zend_async_callbacks_free(&coroutine->coroutine.event);
zend_object_std_dtor(object);
}
static HashTable *async_coroutine_object_gc(zend_object *object, zval **table, int *num)
{
async_coroutine_t *coroutine = (async_coroutine_t *) ZEND_ASYNC_OBJECT_TO_EVENT(object);
zend_get_gc_buffer *buf = zend_get_gc_buffer_create();
/* Always add basic ZVALs from coroutine structure */
zend_get_gc_buffer_add_zval(buf, &coroutine->coroutine.result);
/* Add objects that may be present */
if (coroutine->coroutine.exception) {
zend_get_gc_buffer_add_obj(buf, coroutine->coroutine.exception);
}
if (coroutine->deferred_cancellation) {
zend_get_gc_buffer_add_obj(buf, coroutine->deferred_cancellation);
}
/* Add finally handlers if present */
if (coroutine->finally_handlers) {
zval *val;
ZEND_HASH_FOREACH_VAL(coroutine->finally_handlers, val)
{
zend_get_gc_buffer_add_zval(buf, val);
}
ZEND_HASH_FOREACH_END();
}
/* Add internal context HashTable if present */
if (coroutine->coroutine.internal_context) {
zval *val;
ZEND_HASH_FOREACH_VAL(coroutine->coroutine.internal_context, val)
{
zend_get_gc_buffer_add_zval(buf, val);
}
ZEND_HASH_FOREACH_END();
}
/* Add fcall function name and parameters if present */
if (coroutine->coroutine.fcall) {
zend_get_gc_buffer_add_zval(buf, &coroutine->coroutine.fcall->fci.function_name);
/* Add function parameters */
if (coroutine->coroutine.fcall->fci.param_count > 0 && coroutine->coroutine.fcall->fci.params) {
for (uint32_t i = 0; i < coroutine->coroutine.fcall->fci.param_count; i++) {
zend_get_gc_buffer_add_zval(buf, &coroutine->coroutine.fcall->fci.params[i]);
}
}
}
/* Add waker-related ZVALs if present */
if (coroutine->coroutine.waker) {
zend_get_gc_buffer_add_zval(buf, &coroutine->waker.result);
if (coroutine->waker.error) {
zend_get_gc_buffer_add_obj(buf, coroutine->waker.error);
}
/* Add events HashTable contents */
zend_async_event_t *event;
zval zval_object;
ZEND_HASH_FOREACH_PTR(&coroutine->waker.events, event)
{
if (ZEND_ASYNC_EVENT_IS_REFERENCE(event) || ZEND_ASYNC_EVENT_IS_ZEND_OBJ(event)) {
ZVAL_OBJ(&zval_object, ZEND_ASYNC_EVENT_TO_OBJECT(event));
zend_get_gc_buffer_add_zval(buf, &zval_object);
}
}
ZEND_HASH_FOREACH_END();
zval *event_val;
/* Add triggered events if present */
if (coroutine->waker.triggered_events) {
ZEND_HASH_FOREACH_VAL(coroutine->waker.triggered_events, event_val)
{
zend_get_gc_buffer_add_zval(buf, event_val);
}
ZEND_HASH_FOREACH_END();
}
}
/* Add context ZVALs if present */
if (coroutine->coroutine.context) {
/* Cast to actual context implementation to access HashTables */
async_context_t *context = (async_context_t *) coroutine->coroutine.context;
/* Add all values from context->values HashTable */
zval *val;
ZEND_HASH_FOREACH_VAL(&context->values, val)
{
zend_get_gc_buffer_add_zval(buf, val);
}
ZEND_HASH_FOREACH_END();
/* Add all object keys from context->keys HashTable */
ZEND_HASH_FOREACH_VAL(&context->keys, val)
{
zend_get_gc_buffer_add_zval(buf, val);
}
ZEND_HASH_FOREACH_END();
}
async_fiber_context_t *fiber_context = coroutine->fiber_context;
/* Check if we should traverse execution stack (similar to fibers).
* In coroutine mode, fiber_context->context.status stays INIT,
* so we check: skip if running (current coroutine) or finished. */
if (fiber_context == NULL || !fiber_context->execute_data
|| ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine
|| ZEND_COROUTINE_IS_FINISHED(&coroutine->coroutine)) {
zend_get_gc_buffer_use(buf, table, num);
return NULL;
}
/* Traverse execution stack for suspended coroutines */
HashTable *lastSymTable = NULL;
zend_execute_data *ex = fiber_context->execute_data;
for (; ex; ex = ex->prev_execute_data) {
HashTable *symTable;
if (ZEND_CALL_INFO(ex) & ZEND_CALL_GENERATOR) {
zend_generator *generator = (zend_generator *) ex->return_value;
if (!(generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING)) {
continue;
}
symTable = zend_generator_frame_gc(buf, generator);
} else {
symTable = zend_unfinished_execution_gc_ex(
ex, ex->func && ZEND_USER_CODE(ex->func->type) ? ex->call : NULL, buf, false);
}
if (symTable) {
/*
* Skip if this is the same symbol_table as previous frame (include shares symbol_table)
* Include operators inherit the symbol_table,
* which causes the same zval to be registered twice in the garbage collector.
* This leads to a double ZVAL_DELREF attempt.
*/
if (lastSymTable && lastSymTable == symTable) {
continue;
}
if (lastSymTable) {
zval *val;
ZEND_HASH_FOREACH_VAL(lastSymTable, val)
{
if (EXPECTED(Z_TYPE_P(val) == IS_INDIRECT)) {
val = Z_INDIRECT_P(val);
}
zend_get_gc_buffer_add_zval(buf, val);
}
ZEND_HASH_FOREACH_END();
}
lastSymTable = symTable;
}
}
zend_get_gc_buffer_use(buf, table, num);
return lastSymTable;
}
zend_coroutine_t *async_new_coroutine(zend_async_scope_t *scope)
{
zend_object *object = coroutine_object_create(async_ce_coroutine);
if (UNEXPECTED(object == NULL || EG(exception))) {
if (object != NULL) {
zend_object_release(object);
}
return NULL;
}
async_coroutine_t *coroutine = (async_coroutine_t *) ZEND_ASYNC_OBJECT_TO_EVENT(object);
if (scope != NULL) {
// Add the coroutine to the scope before calling the enqueue hook
zval options;
ZVAL_UNDEF(&options);
if (!scope->before_coroutine_enqueue(&coroutine->coroutine, scope, &options)) {
zval_ptr_dtor(&options);
coroutine->coroutine.event.dispose(&coroutine->coroutine.event);
return NULL;
}
zval_ptr_dtor(&options);
}
return &coroutine->coroutine;
}
void async_register_coroutine_ce(void)
{
async_ce_coroutine = register_class_Async_Coroutine(async_ce_completable);
async_ce_coroutine->create_object = coroutine_object_create;
async_ce_coroutine->default_object_handlers = &coroutine_handlers;
coroutine_handlers = std_object_handlers;
coroutine_handlers.offset = XtOffsetOf(async_coroutine_t, std);
coroutine_handlers.clone_obj = NULL;
coroutine_handlers.dtor_obj = coroutine_object_destroy;
coroutine_handlers.free_obj = coroutine_free;
coroutine_handlers.get_gc = async_coroutine_object_gc;
}
///////////////////////////////////////////////////////////
/// 3. Core Coroutine State Management
///////////////////////////////////////////////////////////
ZEND_STACK_ALIGNED void async_coroutine_execute(async_coroutine_t *coroutine)
{
bool should_start_graceful_shutdown = false;
bool is_bailout = false;
zend_async_waker_t *waker = coroutine->coroutine.waker;
if (UNEXPECTED(waker == NULL || waker->status == ZEND_ASYNC_WAKER_IGNORED)) {
if (ZEND_COROUTINE_IS_CANCELLED(&coroutine->coroutine)) {
zend_try
{
if (EXPECTED(waker != NULL)) {
waker->status = ZEND_ASYNC_WAKER_RESULT;
zend_object *error = waker->error;
// Transfer error from the Waker to current context if it exists.
if (UNEXPECTED(error)) {
waker->error = NULL;
async_rethrow_exception(error);
}
}
async_coroutine_finalize(coroutine);
}
zend_catch
{
is_bailout = true;
should_start_graceful_shutdown = true;
}
zend_end_try();
}
coroutine->coroutine.event.dispose(&coroutine->coroutine.event);
if (EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
ZEND_ASYNC_CURRENT_COROUTINE = NULL;
}
goto exit;
}
if (UNEXPECTED(waker->status == ZEND_ASYNC_WAKER_WAITING)) {
zend_error(E_ERROR, "Attempt to resume a coroutine that has not been resolved");
coroutine->coroutine.event.dispose(&coroutine->coroutine.event);
if (EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
ZEND_ASYNC_CURRENT_COROUTINE = NULL;
}
return;
}
waker->status = ZEND_ASYNC_WAKER_RESULT;
zend_object *error = waker->error;
// The Waker object can be destroyed immediately if the result is an error.
// It will be delivered to the coroutine as an exception.
if (UNEXPECTED(error)) {
waker->error = NULL;
async_rethrow_exception(error);
}
zend_async_waker_clean(&coroutine->coroutine);
ZEND_COROUTINE_SET_STARTED(&coroutine->coroutine);
zend_try
{
if (EXPECTED(coroutine->coroutine.internal_entry == NULL)) {
ZEND_ASSERT(coroutine->coroutine.fcall != NULL && "Coroutine function call is not set");
coroutine->coroutine.fcall->fci.retval = &coroutine->coroutine.result;
zend_call_function(&coroutine->coroutine.fcall->fci, &coroutine->coroutine.fcall->fci_cache);
zval_ptr_dtor(&coroutine->coroutine.fcall->fci.function_name);
ZVAL_UNDEF(&coroutine->coroutine.fcall->fci.function_name);
coroutine->coroutine.fcall->fci.retval = NULL;
} else {
coroutine->coroutine.internal_entry();
}
}
zend_catch
{
ZEND_ASYNC_WAKER_DESTROY(&coroutine->coroutine);
should_start_graceful_shutdown = true;
is_bailout = true;
}
zend_end_try();
zend_try
{
async_coroutine_finalize(coroutine);
OBJ_RELEASE(&coroutine->std);
}
zend_catch
{
should_start_graceful_shutdown = true;
is_bailout = true;
}
zend_end_try();
if (EXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == &coroutine->coroutine)) {
ZEND_ASYNC_CURRENT_COROUTINE = NULL;
}
exit:
if (UNEXPECTED(should_start_graceful_shutdown)) {
zend_try
{
ZEND_ASYNC_SHUTDOWN();
}
zend_catch
{
zend_error(E_CORE_WARNING,
"A critical error was detected during the initiation of the graceful shutdown mode.");
zend_bailout();
}
zend_end_try();
}
if (is_bailout) {
zend_bailout();
}
}
void async_coroutine_finalize(async_coroutine_t *coroutine)
{
// Set up a fake execute_data frame so exceptions thrown during finalization
// carry correct file/line information pointing to the coroutine closure.
// Only applicable to userland coroutines with a resolved function handler.
zend_execute_data fake_frame = {0};
const zend_fcall_t *const fcall = coroutine->coroutine.fcall;
const bool has_fake_frame = fcall != NULL
&& fcall->fci_cache.function_handler != NULL
&& fcall->fci_cache.function_handler->type == ZEND_USER_FUNCTION;
if (has_fake_frame) {
fake_frame.func = fcall->fci_cache.function_handler;
fake_frame.opline = fake_frame.func->op_array.opcodes;
fake_frame.prev_execute_data = EG(current_execute_data);
EG(current_execute_data) = &fake_frame;
}
// Before finalizing the coroutine
// we check that we're properly finishing the coroutine's execution.
// The coroutine must not be in the queue!
if (UNEXPECTED(ZEND_ASYNC_WAKER_IN_QUEUE(coroutine->coroutine.waker))) {
zend_error(E_CORE_WARNING, "Attempt to finalize a coroutine that is still in the queue");
}
ZEND_COROUTINE_SET_FINISHED(&coroutine->coroutine);
/* Call switch handlers for coroutine finishing */
if (UNEXPECTED(coroutine->coroutine.switch_handlers)) {
ZEND_COROUTINE_FINISH(&coroutine->coroutine);
}
bool do_bailout = false;
zend_object **exception_ptr = &EG(exception);
zend_object *prev_exception = NULL;
zend_object **prev_exception_ptr = &prev_exception;
zend_try
{
/* Cleanup switch handlers */
zend_coroutine_switch_handlers_destroy(&coroutine->coroutine);
// call coroutines handlers
zend_object *exception = NULL;
if (UNEXPECTED(*exception_ptr)) {
if (*prev_exception_ptr) {
zend_exception_set_previous(*exception_ptr, *prev_exception_ptr);
*prev_exception_ptr = NULL;
}
exception = *exception_ptr;
GC_ADDREF(exception);
zend_clear_exception();
if (zend_is_graceful_exit(exception) || zend_is_unwind_exit(exception)) {
OBJ_RELEASE(exception);
exception = NULL;
}
}
// Hold the exception inside coroutine if it is not NULL.
if (exception != NULL) {
if (coroutine->coroutine.exception != NULL) {
if (false == instanceof_function(exception->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION))) {
zend_exception_set_previous(exception, coroutine->coroutine.exception);
coroutine->coroutine.exception = exception;
GC_ADDREF(exception);
}
} else {
coroutine->coroutine.exception = exception;
GC_ADDREF(exception);
}
} else if (coroutine->coroutine.exception != NULL) {
// If the coroutine has an exception, we keep it.
exception = coroutine->coroutine.exception;
GC_ADDREF(exception);
}
zend_exception_save_fast(exception_ptr, prev_exception_ptr);
// Mark second parameter of zend_async_callbacks_notify as ZVAL
ZEND_ASYNC_EVENT_SET_ZVAL_RESULT(&coroutine->coroutine.event);
ZEND_COROUTINE_CLR_EXCEPTION_HANDLED(&coroutine->coroutine);
ZEND_ASYNC_CALLBACKS_NOTIFY(&coroutine->coroutine.event, &coroutine->coroutine.result, exception);
zend_async_callbacks_free(&coroutine->coroutine.event);
if (coroutine->coroutine.internal_context != NULL) {
zend_async_coroutine_internal_context_dispose(&coroutine->coroutine);
}
// Call finally handlers if any
if (coroutine->finally_handlers != NULL && zend_hash_num_elements(coroutine->finally_handlers) > 0) {
coroutine_call_finally_handlers(coroutine);
}
ZEND_ASYNC_WAKER_DESTROY(&coroutine->coroutine);
if (coroutine->coroutine.extended_dispose != NULL) {
const zend_async_coroutine_dispose dispose = coroutine->coroutine.extended_dispose;
coroutine->coroutine.extended_dispose = NULL;
dispose(&coroutine->coroutine);
}
zend_exception_restore_fast(exception_ptr, prev_exception_ptr);
// If the exception was handled by any handler, we do not propagate it further.
// Cancellation-type exceptions are considered handled in all cases and are not propagated further.
if (exception != NULL &&
(ZEND_COROUTINE_IS_EXCEPTION_HANDLED(&coroutine->coroutine) ||
instanceof_function(exception->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION)))) {
OBJ_RELEASE(exception);
exception = NULL;
}
// Before the exception leads to graceful termination,
// we give one last chance to handle it using Scope handlers.
if (exception != NULL &&
ZEND_ASYNC_SCOPE_CATCH(coroutine->coroutine.scope,
&coroutine->coroutine,
NULL,
exception,
false,
ZEND_ASYNC_SCOPE_IS_DISPOSE_SAFELY(coroutine->coroutine.scope))) {
OBJ_RELEASE(exception);
exception = NULL;
}
// Notify the async scope that the coroutine has finished.
// For the Scheduler, the coroutine's Scope may be undefined.
if (EXPECTED(coroutine->coroutine.scope != NULL)) {
async_scope_notify_coroutine_finished(coroutine);
coroutine->coroutine.scope = NULL;
}
// Otherwise, we rethrow the exception.
if (exception != NULL) {
async_rethrow_exception(exception);
}
}
zend_catch
{
do_bailout = true;
}
zend_end_try();
if (has_fake_frame) {
EG(current_execute_data) = fake_frame.prev_execute_data;
}
if (UNEXPECTED(*exception_ptr && (zend_is_graceful_exit(*exception_ptr) || zend_is_unwind_exit(*exception_ptr)))) {
zend_clear_exception();
}
if (EXPECTED(ZEND_ASYNC_SCHEDULER != &coroutine->coroutine)) {
// Permanently remove the coroutine from the Scheduler.
if (UNEXPECTED(zend_hash_index_del(&ASYNC_G(coroutines), coroutine->std.handle) == FAILURE)) {
zend_error(E_CORE_ERROR, "Failed to remove coroutine from the list");
}
// Decrease the active coroutine count if the coroutine is not a zombie.
if (false == ZEND_COROUTINE_IS_ZOMBIE(&coroutine->coroutine)) {
ZEND_ASYNC_DECREASE_COROUTINE_COUNT
}
}
coroutine->fiber_context = NULL;
if (UNEXPECTED(do_bailout)) {
zend_bailout();
}
}
/**
* The function suspends the execution of the coroutine and triggers a switch to another one.
* After calling this function, you must properly clean up the coroutine waker object
* (example zend_async_waker_clean).
*
* @param from_main For main coroutine
* @param is_bailout Whether this is called during bailout
*/
bool async_coroutine_suspend(const bool from_main, const bool is_bailout)
{
if (UNEXPECTED(from_main)) {
// If the Scheduler was never used, it means no coroutines were created,
// so execution can be finished without doing anything.
if (circular_buffer_is_empty(&ASYNC_G(microtasks)) && zend_hash_num_elements(&ASYNC_G(coroutines)) == 0) {
return true;
}
return async_scheduler_main_coroutine_suspend(is_bailout);
}
return async_scheduler_coroutine_suspend();
}
bool async_coroutine_resume(zend_coroutine_t *coroutine, zend_object *error, const bool transfer_error)
{
zend_async_waker_t *waker = coroutine->waker;
if (UNEXPECTED(waker == NULL || waker->status == ZEND_ASYNC_WAKER_NO_STATUS)) {
async_throw_error("Cannot resume a coroutine that has not been suspended");
return false;
}
if (error != NULL) {
if (waker->error != NULL) {
if (false == instanceof_function(error->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION))) {
zend_exception_set_previous(error, waker->error);
waker->error = error;
if (false == transfer_error) {
GC_ADDREF(error);
}
} else {
if (transfer_error) {
OBJ_RELEASE(error);
}
}
} else {
waker->error = error;
if (false == transfer_error) {
GC_ADDREF(error);
}
}
}
if (UNEXPECTED(waker->status == ZEND_ASYNC_WAKER_QUEUED)) {
return true;
}
const bool in_scheduler_context = ZEND_ASYNC_SCHEDULER_CONTEXT;
// **Short execution path**:
// If the event handlers are running under the scheduler
// And this is the current coroutine
// There is no point in returning it to the queue,
// we will execute it immediately!
if (UNEXPECTED(in_scheduler_context && coroutine == ZEND_ASYNC_CURRENT_COROUTINE)) {
waker->status = ZEND_ASYNC_WAKER_RESULT;
return true;
}
if (UNEXPECTED(circular_buffer_push_ptr_with_resize(&ASYNC_G(coroutine_queue), coroutine)) == FAILURE) {
async_throw_error("Failed to enqueue coroutine");
return false;
}
waker->status = ZEND_ASYNC_WAKER_QUEUED;
// Add to resumed_coroutines queue for event cleanup
if (in_scheduler_context) {
circular_buffer_push_ptr_with_resize(&ASYNC_G(resumed_coroutines), coroutine);
} else if (coroutine->waker != NULL) {
ZEND_ASYNC_WAKER_CLEAN_EVENTS(coroutine->waker);
}
return true;
}
bool async_coroutine_cancel(zend_coroutine_t *zend_coroutine,
zend_object *error,
bool transfer_error,
const bool is_safely)
{
transfer_error = error != NULL ? transfer_error : false;
// If the coroutine finished, do nothing.
if (ZEND_COROUTINE_IS_FINISHED(zend_coroutine)) {
if (transfer_error && error != NULL) {
OBJ_RELEASE(error);
}
return true;
}
// An attempt to cancel a coroutine that is currently running.
// In this case, nothing actually happens immediately;
// however, the coroutine is marked as having been cancelled,
// and the cancellation exception is stored as its result.
//
// `zend_coroutine->waker->status == ZEND_ASYNC_WAKER_WAITING`
// The condition means: exclude the coroutine if it is in a waiting state.
//
// **Explanation:**
// ZEND_ASYNC_CURRENT_COROUTINE may point to a coroutine that is waiting
// for an event but has not yet switched. Canceling such a coroutine
// is performed in the usual way.
//
if (UNEXPECTED(
zend_coroutine == ZEND_ASYNC_CURRENT_COROUTINE &&
false ==
(zend_coroutine->waker != NULL && zend_coroutine->waker->status == ZEND_ASYNC_WAKER_WAITING))) {
ZEND_COROUTINE_SET_CANCELLED(zend_coroutine);
if (zend_coroutine->exception == NULL) {
zend_coroutine->exception = error;
if (error && false == transfer_error) {
GC_ADDREF(error);
}
}
if (zend_coroutine->exception == NULL) {
zend_coroutine->exception = async_new_exception(async_ce_cancellation_exception, "Coroutine cancelled");
}
return true;
}
zend_async_waker_t *waker = zend_async_waker_define(zend_coroutine);
const bool is_error_null = (error == NULL);
if (is_error_null) {
error = async_new_exception(async_ce_cancellation_exception, "Coroutine cancelled");
transfer_error = true;
if (UNEXPECTED(EG(exception))) {
return false;
}
}
// If the coroutine is currently protected from cancellation, defer the cancellation.
if (ZEND_COROUTINE_IS_PROTECTED(zend_coroutine)) {
async_coroutine_t *coroutine = (async_coroutine_t *) zend_coroutine;
if (coroutine->deferred_cancellation == NULL) {
coroutine->deferred_cancellation = error;
if (false == transfer_error) {
GC_ADDREF(error);
}
} else if (transfer_error) {
OBJ_RELEASE(error);
}
return true;
}
bool was_cancelled = ZEND_COROUTINE_IS_CANCELLED(zend_coroutine);
ZEND_COROUTINE_SET_CANCELLED(zend_coroutine);
if (false == ZEND_COROUTINE_IS_STARTED(zend_coroutine)) {
if (false == ZEND_ASYNC_WAKER_IN_QUEUE(waker)) {
//
// Situation: the coroutine is not in the queue, but a cancellation is requested.
// It might seem like we can simply remove the coroutine,
// but doing so would break the flow of the coroutine's handlers.
// Therefore, to normalize the flow,
// we place the coroutine in the queue with a status of ignored,
// so that the flow is executed correctly.
//
async_scheduler_coroutine_enqueue(zend_coroutine);
}
waker->status = ZEND_ASYNC_WAKER_IGNORED;
//
// Exception override:
// If the coroutine already has an exception
// and it's a cancellation exception, then nothing needs to be done.
// In any other case, the cancellation exception overrides the existing exception.
//
ZEND_ASYNC_WAKER_APPLY_CANCELLATION(waker, error, transfer_error);
return async_scheduler_coroutine_enqueue(zend_coroutine);
}
// In safely mode, we don't forcibly terminate the coroutine,
// but we do mark it as a Zombie.
if (is_safely) {
async_scope_mark_coroutine_zombie((async_coroutine_t *) zend_coroutine);
ZEND_ASYNC_DECREASE_COROUTINE_COUNT
if (transfer_error && error != NULL) {
OBJ_RELEASE(error);
}
return true;
}
if (was_cancelled && waker->error != NULL &&
instanceof_function(waker->error->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION))) {
if (transfer_error) {
OBJ_RELEASE(error);
}
} else {
ZEND_ASYNC_WAKER_APPLY_CANCELLATION(waker, error, transfer_error);
}
return async_scheduler_coroutine_enqueue(zend_coroutine);
}
///////////////////////////////////////////////////////////
/// 4. Event System Interface
///////////////////////////////////////////////////////////
static bool coroutine_event_start(zend_async_event_t *event)
{
return true;
}
static bool coroutine_event_stop(zend_async_event_t *event)
{
// Empty implementation - coroutines don't need explicit stop
return true;
}
static bool coroutine_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_push(event, callback);
}
static bool coroutine_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_remove(event, callback);
}