This is automatic generated document for things in Swow\Coroutine class

Class Coroutine

Coroutine class is the abstract of coroutine.

A coroutine is a subroutine actively suspend itself, and get resumed when there is no other running coroutines, it’s co-operative so it was called co-routine.

We call suspending self “yield”, when yielding form a coroutine, the coroutine suspends its execution, the scheduler will find and execute another coroutine can continue.

Swow have hooked most of IO operation functions in PHP, an IO coroutine will yield when calling these functions. When the IO operation is done, and current coroutine yield, the scheduler may continue the fore-mentioned IO coroutine. This is how Swow make IO concurrency.

Constants

public STATE_DEAD = 3 public STATE_NONE = 0 public STATE_RUNNING = 2 public STATE_WAITING = 1

Methods

Static method count

public static function count ( ) : int

Get the number of coroutines.

Returns int the number of coroutines, including the main coroutine

Static method get

public static function get ( int $id ) : (static | null)

Get a coroutine by id.

Returns (static | null) the coroutine, or null when the id is not found

Static method getAll

public static function getAll ( ) : array<int, Coroutine>

Get all coroutines.

Returns array<int, Coroutine> all coroutines, including the main coroutine

Static method getCurrent

public static function getCurrent ( ) : self|static

Get current coroutine

Static method getGlobalSwitches

public static function getGlobalSwitches ( ) : int

Static method getMain

public static function getMain ( ) : self|static

Get main (the most outter) coroutine

Static method killAll

public static function killAll ( ) : void

Static method registerDeadlockHandler

public static function registerDeadlockHandler ( callable $callable ) : Utils\Handler

Register a callback for a deadlock.

The scheduler calls the callback when all coroutines wait. The process stays blocked after the call. The callback must kill coroutines or stop the process.

Returns Utils\Handler handler for remove()

Static method run

public static function run ( callable $callable , mixed ... $data ) : static

Create a coroutine and run it immediately

An uncaught exception in the body does not reach the caller. The coroutine ends with exit status 0.

context swap happens here

Param callable $callable the coroutine body Param mixed $data arguments passed to the callable

Returns static the new created coroutine

Static method yield

public static function yield ( mixed $data ) : mixed

Yield from current coroutine

The coroutine continues only after another coroutine calls resume(). A yield in the main coroutine without a resumer stops the process.

context swap happens here when success

Returns mixed the value passed to the next resume()

Method __construct

public function __construct ( callable $callable )

Method __debugInfo

public function __debugInfo ( ) : array<string, mixed>

Returns array<string, mixed> debug information for var_dump

Method call

public function call ( callable $callable , int $level ) : mixed

Call a function in the scope of a frame.

Returns mixed the return value of the function

Method eval

public function eval ( string $string , int $level ) : mixed

evaluate code in specified scope

The code is one expression. The implementation adds “return “ before the code. A return statement or an echo statement is a parse error.

Todo string $string? why not rename it to $code See Coroutine::getTrace() for $level params usage

Param string $string expression to evaluate

Returns mixed the value of the expression

Throws CoroutineException when the code is invalid

Method getDefinedVars

public function getDefinedVars ( int $level ) : array<string, mixed>

get defined variables in specified scope

See Coroutine::getTrace() for params usage

Method getElapsed

public function getElapsed ( ) : int

Method getElapsedAsString

public function getElapsedAsString ( ) : string

Method getEndTime

public function getEndTime ( ) : int

Method getExecutedFilename

public function getExecutedFilename ( int $level ) : string

get current executed code file name

See Coroutine::getTrace() for params usage

Method getExecutedFunctionName

public function getExecutedFunctionName ( int $level ) : string

get executed function name

See Coroutine::getTrace() for params usage

Method getExecutedLineno

public function getExecutedLineno ( int $level ) : int

get current executed code line

See Coroutine::getTrace() for params usage

Method getExitStatus

public function getExitStatus ( ) : int

Get the exit status.

Returns int 0 after a normal return, a kill or an uncaught exception. The main coroutine returns the request exit status.

Method getId

public function getId ( ) : int

Get unique coroutine id

Method getPrevious

public function getPrevious ( ) : self|static

Get previous coroutine

Method getStartTime

public function getStartTime ( ) : int

Method getState

public function getState ( ) : int

Get coroutine state

Returns int the enum number: static::STATE_* constants

Method getStateName

public function getStateName ( ) : string

Method getSwitches

public function getSwitches ( ) : int

Method getTrace

public function getTrace ( int $level , int $limit , int $options ) : array<array{file: string, line: int, function: string, args: array<mixed>, class?: string}>

get trace information

positive $level value count frame from current to top, negative $level value count frame from top to current.

for example, a stack may be like:

$level frame getTrace(0, 0) getTrace(-1, 0) or getTrace(4, 0) getTrace(0, 3)
5 {main} starts here starts here FIXME: main?
4 / -1 Swow\Coroutine::run(‘a’)   ends here  
3 / -2 a()      
2 / -3 b()     starts here
1 / -4 c()      
0 / -5 Swow\Coroutine->getTraceAsString() ends here   ends here

The stack is the current PHP stack. Before the first switch it contains the caller frames.

Param int $level trace start level Param int $limit trace max length, negative or 0 meaning no limit Param int $options trace options, same as debug_backtrace options { @see https://www.php.net/manual/en/function.debug-backtrace.php }

Method getTraceAsList

public function getTraceAsList ( int $level , int $limit , int $options ) : array<string>

get trace information as string like original php strack trace format in array

return value maybe like

[
"#0 some.php(7): Swow\Coroutine->getTraceAsString()",
"#1 some.php(10): c()",
"#2 some.php(13): b()",
"#3 [internal function]: a(1, 'some str', Array)",
"#4 some.php(16): Swow\Coroutine::run('a', 1, 'some str', Array)",
"#5 {main}",
]

See Coroutine::getTrace() for params usage

Method getTraceAsString

public function getTraceAsString ( int $level , int $limit , int $options ) : string

get trace information as string like original php strack trace format

return value maybe like

#0 some.php(7): Swow\Coroutine->getTraceAsString()
#1 some.php(10): c()
#2 some.php(13): b()
#3 [internal function]: a(1, 'some str', Array)
#4 some.php(16): Swow\Coroutine::run('a', 1, 'some str', Array)
#5 {main}

See Coroutine::getTrace() for params usage

Method getTraceDepth

public function getTraceDepth ( int $limit ) : int

get trace depth

See Coroutine::getTrace() for params usage

Returns int the trace depth, -1 before the coroutine starts

Method isAlive

public function isAlive ( ) : bool

Method isAvailable

public function isAvailable ( ) : bool

Method isExecuting

public function isExecuting ( ) : bool

Check whether the coroutine started and did not end.

A suspended coroutine counts as executing.

Returns bool true when the coroutine started and did not end

Method kill

public function kill ( ) : void

Method resume

public function resume ( mixed ... $data ) : mixed

Resume coroutine execution

context swap happens here when success

Returns mixed the value of the next yield(), or the return value of the body

Method setLocalVar

public function setLocalVar ( string $name , mixed $value , int $level , bool $force ) : static

set variable in specified scope

See Coroutine::getTrace() for $level params usage

Param string $name variable name Param mixed $value variable value Param bool $force force rebuild symbol table, if you donot know this, keep its default value

Method throw

public function throw ( \Throwable $throwable ) : mixed

Throw an exception into the coroutine and resume it.

Returns mixed the return value of the body


Copyright 2022 the Swow contributors

This site uses Just the Docs, a documentation theme for Jekyll.