class StripedLock

Coordinate short key-scoped critical sections across Swoole workers.

The locks must be constructed before the server forks so every worker inherits references to the same shared atomics. Every multi-stripe path acquires stripes in ascending index order and releases them in reverse so selected-stripe and all-stripe callers cannot deadlock each other.

Constants

protected STRIPE_COUNT

protected SPINS_BEFORE_BACKOFF

protected ACQUIRE_TIMEOUT_NANOSECONDS

Properties

protected Atomic> $locks

Methods

__construct()

Create a new striped lock.

mixed
withLock(string $key, callable $callback)

Run the callback while holding the stripe for the given key.

mixed
withLocks(array $keys, callable $callback)

Run the callback while holding the stripes for the given keys.

mixed
withAllLocks(callable $callback)

Run the callback while holding every stripe.

mixed
withSelectedLocks(array $locks, callable $callback)

Run the callback while holding the selected stripes in their supplied order.

Atomic
lockFor(string $key)

Get the stripe for a key.

int
lockIndexFor(string $key)

Get the stripe index for a key.

void
acquire(Atomic $lock)

Acquire a stripe.

void
release(Atomic $lock)

Release a stripe.

Details

at line 36
__construct()

Create a new striped lock.

at line 51
mixed withLock(string $key, callable $callback)

Run the callback while holding the stripe for the given key.

Parameters

string $key
callable $callback

Return Value

mixed

at line 71
mixed withLocks(array $keys, callable $callback)

Run the callback while holding the stripes for the given keys.

Parameters

array $keys
callable $callback

Return Value

mixed

at line 98
mixed withAllLocks(callable $callback)

Run the callback while holding every stripe.

Parameters

callable $callback

Return Value

mixed

at line 111
protected mixed withSelectedLocks(array $locks, callable $callback)

Run the callback while holding the selected stripes in their supplied order.

Parameters

array $locks
callable $callback

Return Value

mixed

at line 132
protected Atomic lockFor(string $key)

Get the stripe for a key.

Parameters

string $key

Return Value

Atomic

at line 140
protected int lockIndexFor(string $key)

Get the stripe index for a key.

Parameters

string $key

Return Value

int

at line 148
protected void acquire(Atomic $lock)

Acquire a stripe.

Parameters

Atomic $lock

Return Value

void

at line 172
protected void release(Atomic $lock)

Release a stripe.

Parameters

Atomic $lock

Return Value

void