Skip to content

Commit 0c41d8b

Browse files
author
Edmond
committed
Add Fiber::getCoroutine() method
Provides access to the underlying coroutine object when a fiber is running inside an async coroutine. Returns null for regular fibers. This allows using all coroutine methods (getId, getContext, cancel, etc.) without duplicating the API in Fiber class. Example usage: $fiber = new Fiber(fn() => /* ... */); $fiber->start(); $coroutine = $fiber->getCoroutine(); $id = $coroutine->getId(); $context = $coroutine->getContext();
1 parent 9d72984 commit 0c41d8b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Zend/zend_fibers.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,20 @@ ZEND_METHOD(Fiber, getCurrent)
17941794
RETURN_OBJ_COPY(&fiber->std);
17951795
}
17961796

1797+
ZEND_METHOD(Fiber, getCoroutine)
1798+
{
1799+
ZEND_PARSE_PARAMETERS_NONE();
1800+
1801+
zend_fiber *fiber = (zend_fiber *) Z_OBJ_P(ZEND_THIS);
1802+
1803+
if (fiber->coroutine == NULL) {
1804+
RETURN_NULL();
1805+
}
1806+
1807+
zend_object *coroutine_obj = ZEND_ASYNC_EVENT_TO_OBJECT(&fiber->coroutine->event);
1808+
RETURN_OBJ_COPY(coroutine_obj);
1809+
}
1810+
17971811
ZEND_METHOD(FiberError, __construct)
17981812
{
17991813
zend_throw_error(

Zend/zend_fibers.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function isTerminated(): bool {}
2626

2727
public function getReturn(): mixed {}
2828

29+
public function getCoroutine(): ?\Async\Coroutine {}
30+
2931
public static function getCurrent(): ?Fiber {}
3032

3133
public static function suspend(mixed $value = null): mixed {}

Zend/zend_fibers_arginfo.h

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)