class Coroutine

Constants

DETACHED_CONTEXT_KEY

Properties

static protected bool $enableReportException
static protected callable[] $afterCreatedCallbacks

Methods

static int
id()

Returns the current coroutine ID.

static void
afterCreated(callable $callback)

Register a callback to be called after a coroutine is created.

static void
defer(callable $callable)

Register a callback to be executed when the coroutine exits.

static void
sleep(float $seconds)

Sleep for the given number of seconds.

static int
parentId(int|null $coroutineId = null)

Returns the parent coroutine ID.

static int
pid(int|null $coroutineId = null)

Alias of Coroutine::parentId().

static int
create(callable $callable)

Create a new coroutine.

static int
createOwned(callable $callable, Closure $wrapper, bool $detached = false)

Create a coroutine whose lifecycle is owned by a wrapper.

static int
fork(callable $callable, array $keys = [])

Create a coroutine with a copy of the parent coroutine context.

static int
forkOwned(callable $callable, Closure $wrapper, array $keys = [], bool $detached = false)

Create an owned coroutine with a copy of the parent coroutine context.

static bool
join(array $coroutineIds, float $timeout = -1)

Wait for the given coroutines to finish.

static bool
inCoroutine()

Determine if currently running in a coroutine.

static array
stats()

Get coroutine statistics.

static bool
exists(int $id)

Determine if a coroutine with the given ID exists.

static iterable
list()

Get a list of all coroutine IDs.

static void
enableReportException(bool $enableReportException)

Enable or disable exception reporting in coroutines.

static void
flushState()

Flush all static state.

static void
printLog(Throwable $throwable)

Report an exception through the exception handler.

static void
reportUncaught(Throwable $throwable)

Report an exception without allowing cancellation to escape a terminal coroutine boundary.

Details

at line 33
static int id()

Returns the current coroutine ID.

Returns -1 when running in non-coroutine context.

Return Value

int

at line 47
static void afterCreated(callable $callback)

Register a callback to be called after a coroutine is created.

Boot-only. The callback persists in a static property for the worker lifetime and runs for every subsequently created coroutine. Callbacks run synchronously during child startup and must not suspend. Hooks that inherit parent context must honor DETACHED_CONTEXT_KEY while preserving values explicitly installed in the child.

Parameters

callable $callback

Return Value

void

at line 55
static void defer(callable $callable)

Register a callback to be executed when the coroutine exits.

Parameters

callable $callable

Return Value

void

at line 69
static void sleep(float $seconds)

Sleep for the given number of seconds.

Parameters

float $seconds

Return Value

void

at line 82
static int parentId(int|null $coroutineId = null)

Returns the parent coroutine ID.

Returns 0 when running in the top level coroutine.

Parameters

int|null $coroutineId

Return Value

int

Exceptions

RunningInNonCoroutineException
CoroutineDestroyedException

at line 93
static int pid(int|null $coroutineId = null)

Alias of Coroutine::parentId().

Parameters

int|null $coroutineId

Return Value

int

Exceptions

RunningInNonCoroutineException
CoroutineDestroyedException

at line 101
static int create(callable $callable)

Create a new coroutine.

Parameters

callable $callable

Return Value

int

at line 116
static int createOwned(callable $callable, Closure $wrapper, bool $detached = false)

Create a coroutine whose lifecycle is owned by a wrapper.

The wrapper runs at native child entry and must invoke the supplied runner exactly once. Outside the runner it must not wait for I/O or other work. Finalize ownership before notifying waiters, which may run immediately. Detachment prevents implicit parent-context propagation during startup.

Parameters

callable $callable
Closure $wrapper
bool $detached

Return Value

int

at line 126
static int fork(callable $callable, array $keys = [])

Create a coroutine with a copy of the parent coroutine context.

Parameters

callable $callable
array $keys

Context keys to copy (empty = all keys)

Return Value

int

at line 147
static int forkOwned(callable $callable, Closure $wrapper, array $keys = [], bool $detached = false)

Create an owned coroutine with a copy of the parent coroutine context.

The wrapper runs at native child entry and must invoke the supplied runner exactly once. Outside the runner it must not wait for I/O or other work. Finalize ownership before notifying waiters, which may run immediately. Detachment prevents parent fallback without discarding the copied context.

Parameters

callable $callable
Closure $wrapper
array $keys

Context keys to copy (empty = all keys)

bool $detached

Return Value

int

at line 214
static bool join(array $coroutineIds, float $timeout = -1)

Wait for the given coroutines to finish.

A false return may mean that no supplied coroutine remained active or that the timeout elapsed. It is not a general failure signal.

Parameters

array $coroutineIds
float $timeout

Return Value

bool

at line 222
static bool inCoroutine()

Determine if currently running in a coroutine.

Return Value

bool

at line 230
static array stats()

Get coroutine statistics.

Return Value

array

at line 238
static bool exists(int $id)

Determine if a coroutine with the given ID exists.

Parameters

int $id

Return Value

bool

at line 248
static iterable list()

Get a list of all coroutine IDs.

Return Value

iterable

at line 259
static void enableReportException(bool $enableReportException)

Enable or disable exception reporting in coroutines.

Boot-only. The flag persists in a static property for the worker lifetime and applies to every coroutine spawned across the process.

Parameters

bool $enableReportException

Return Value

void

at line 267
static void flushState()

Flush all static state.

Return Value

void

at line 278
static protected void printLog(Throwable $throwable)

Report an exception through the exception handler.

Parameters

Throwable $throwable

Return Value

void

Exceptions

CanceledException

at line 310
static protected void reportUncaught(Throwable $throwable)

Report an exception without allowing cancellation to escape a terminal coroutine boundary.

Parameters

Throwable $throwable

Return Value

void