Skip to content

Commit 5a25fa7

Browse files
committed
Combine zend_fiber_run and zend_fiber_execute
1 parent 45fd596 commit 5a25fa7

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

Zend/zend_fibers.c

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -334,31 +334,7 @@ static zend_always_inline zend_vm_stack zend_fiber_vm_stack_alloc(size_t size) /
334334
}
335335
/* }}} */
336336

337-
static void zend_fiber_execute(zend_fiber *fiber) /* {{{ */
338-
{
339-
fiber->fci.retval = &fiber->value;
340-
341-
zend_call_function(&fiber->fci, &fiber->fci_cache);
342-
343-
if (EG(exception)) {
344-
if (fiber->status == ZEND_FIBER_STATUS_SHUTDOWN) {
345-
if (EXPECTED(zend_is_fiber_exit(EG(exception)))) {
346-
zend_clear_exception();
347-
} else {
348-
zend_exception_error(EG(exception), E_ERROR);
349-
}
350-
} else {
351-
fiber->status = ZEND_FIBER_STATUS_THREW;
352-
}
353-
} else {
354-
fiber->status = ZEND_FIBER_STATUS_RETURNED;
355-
}
356-
357-
GC_DELREF(&fiber->std);
358-
}
359-
/* }}} */
360-
361-
static void zend_fiber_run(zend_fiber_context *context) /* {{{ */
337+
static void zend_fiber_execute(zend_fiber_context *context) /* {{{ */
362338
{
363339
zend_fiber *fiber = EG(current_fiber);
364340
ZEND_ASSERT(fiber);
@@ -382,10 +358,32 @@ static void zend_fiber_run(zend_fiber_context *context) /* {{{ */
382358
EG(jit_trace_num) = 0;
383359
EG(error_reporting) = error_reporting;
384360

385-
zend_fiber_execute(fiber);
361+
fiber->fci.retval = &fiber->value;
362+
363+
// Reference added while running, removed when suspended, and added again once resumed.
364+
GC_ADDREF(&fiber->std);
365+
366+
zend_call_function(&fiber->fci, &fiber->fci_cache);
367+
368+
if (EG(exception)) {
369+
if (fiber->status == ZEND_FIBER_STATUS_SHUTDOWN) {
370+
if (EXPECTED(zend_is_fiber_exit(EG(exception)))) {
371+
zend_clear_exception();
372+
} else {
373+
zend_exception_error(EG(exception), E_ERROR);
374+
}
375+
} else {
376+
fiber->status = ZEND_FIBER_STATUS_THREW;
377+
}
378+
} else {
379+
fiber->status = ZEND_FIBER_STATUS_RETURNED;
380+
}
386381

387382
zend_vm_stack_destroy();
388383
fiber->execute_data = NULL;
384+
385+
// Remove reference added at last resume.
386+
GC_DELREF(&fiber->std);
389387
}
390388
/* }}} */
391389

@@ -474,15 +472,13 @@ ZEND_METHOD(Fiber, start)
474472
fiber->fci.params = ZEND_CALL_ARG(execute_data, 1);
475473
fiber->fci.param_count = ZEND_CALL_NUM_ARGS(execute_data);
476474

477-
if (!zend_fiber_init_context(&fiber->context, zend_fiber_run, EG(fiber_stack_size))) {
475+
if (!zend_fiber_init_context(&fiber->context, zend_fiber_execute, EG(fiber_stack_size))) {
478476
zend_throw_error(NULL, "Could not create fiber context");
479477
return;
480478
}
481479

482480
fiber->status = ZEND_FIBER_STATUS_RUNNING;
483481

484-
GC_ADDREF(&fiber->std);
485-
486482
zend_fiber_switch_to(fiber);
487483

488484
zval_ptr_dtor(&fiber->fci.function_name);
@@ -530,6 +526,7 @@ ZEND_METHOD(Fiber, suspend)
530526
fiber->execute_data = execute_data;
531527
fiber->status = ZEND_FIBER_STATUS_SUSPENDED;
532528

529+
// Remove running reference while suspended.
533530
GC_DELREF(&fiber->std);
534531

535532
zend_fiber_suspend(fiber);
@@ -541,6 +538,7 @@ ZEND_METHOD(Fiber, suspend)
541538
}
542539

543540
fiber->status = ZEND_FIBER_STATUS_RUNNING;
541+
// Add reference while fiber is running.
544542
GC_ADDREF(&fiber->std);
545543

546544
if (!fiber->error) {

0 commit comments

Comments
 (0)