Skip to main content Link Menu Expand (external link) Document Search Copy Copied

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

Static method get

public static function get ( int $id ) : ?static

Static method getAll

public static function getAll ( ) : array<int, 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

Static method run

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

Create a coroutine and run it immediately

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

context swap happens here when success

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

Method eval

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

evaluate code in specified scope

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

Param string $string code to evaluate

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

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

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

Method isAlive

public function isAlive ( ) : bool

Method isAvailable

public function isAvailable ( ) : bool

Method isExecuting

public function isExecuting ( ) : bool

Method kill

public function kill ( ) : void

Method resume

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

Resume coroutine execution

context swap happens here when success

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

Copyright 2022 the Swow contributors