phasync
phasync / awaitFlag

Method

phasync::awaitFlag

Suspends the current coroutine until $signal is raised with phasync::raiseFlag.

Description#

public static phasync::awaitFlag(
    object $signal,
    float $timeout = PHP_FLOAT_MAX,
    bool $throw = true
): bool

The wait does not keep $signal alive: hold a reference to it elsewhere for as long as the coroutine waits. When the last other reference goes, the coroutine is woken with a CancelledException, because nothing can raise the flag any more. Waiting on an object nothing else references is undefined.

Parameters#

$signal object
The object a raiser will pass to raiseFlag()
$timeout float = PHP_FLOAT_MAX
Seconds to wait at most; the loop checks these timeouts every 10 ms, so a shorter one ends after up to 10 ms (sleep() is exact)
$throw bool = true
When false, a timeout returns false instead of throwing TimeoutException, and never constructs one: for a wait that is expected to time out often, where building the exception would be the expensive part

Return values#

True once the flag is raised; false only when throw: false and $timeout passed first bool

Errors / Exceptions#

ExceptionWhen
TimeoutExceptionIf the flag is not raised in time and throw is true (the default)
CancelledExceptionIf the flag was released, or the coroutine cancelled, while waiting
LogicExceptionOutside a coroutine

Examples#

Example 1
$done = new stdClass();
phasync::go(function () use ($done) {
    phasync::sleep(0.1);
    phasync::raiseFlag($done);
});
phasync::awaitFlag($done, 1.0);   // returns after 0.1 seconds

See also#