phasync
Coordination and limits / LockInterface

Interface

LockInterface

An object that runs a closure while holding a lock that other coroutines wait for.

Class synopsis#

interface LockInterface {
    /* Methods */
    public LockInterface::lock(Closure $callable, ?float $timeout = null): mixed
}

Methods#

MethodDescription
lockRuns $callable while holding the lock of this object, and returns what it returns.

Examples#

Example 1
class Counter implements phasync\Util\LockInterface
{
    use phasync\Util\LockTrait;

    public int $value = 0;
}

phasync::run(function () {
    $counter = new Counter();
    $counter->lock(function () use ($counter) {
        $counter->value++;
    });
});

See also#

  • LockTraitImplements LockInterface: a lock that is reentrant for the coroutine holding it and fair for the others.
  • SynchronizedA mutex for coroutines, identified by a string or an object: only one coroutine at a time runs a closure for the same token.