abstract class Lock implements Lock (View source)

Traits

Properties

protected string $name

The name of the lock.

protected int $seconds

The number of seconds the lock should be maintained.

protected string $owner

The scope identifier of this lock.

protected int $sleepMilliseconds

The number of milliseconds to wait before re-attempting to acquire a lock while blocking.

Methods

int
secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateTimeInterface|DateInterval|int $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateTimeInterface|DateInterval|int $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

string
runTimeForHumans(float $startTime, float $endTime = null)

Given a start time, format the total run time for human readability.

void
__construct(string $name, int $seconds, string|null $owner = null)

Create a new lock instance.

bool
acquire()

Attempt to acquire the lock.

bool
release()

Release the lock.

string
getCurrentOwner()

Returns the owner value written into the driver for this lock.

mixed
get(callable|null $callback = null)

Attempt to acquire the lock.

mixed
block(int $seconds, callable|null $callback = null)

Attempt to acquire the lock for the given number of seconds.

string
owner()

Returns the current owner of the lock.

bool
isOwnedByCurrentProcess()

Determines whether this lock is allowed to release the lock in the driver.

bool
isOwnedBy(string|null $owner)

Determine whether this lock is owned by the given identifier.

$this
betweenBlockedAttemptsSleepFor(int $milliseconds)

Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.

Details

protected int secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

protected int availableAt(DateTimeInterface|DateInterval|int $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

protected DateTimeInterface|int parseDateInterval(DateTimeInterface|DateInterval|int $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

DateTimeInterface|int

protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

protected string runTimeForHumans(float $startTime, float $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float $endTime

Return Value

string

void __construct(string $name, int $seconds, string|null $owner = null)

Create a new lock instance.

Parameters

string $name
int $seconds
string|null $owner

Return Value

void

abstract bool acquire()

Attempt to acquire the lock.

Return Value

bool

abstract bool release()

Release the lock.

Return Value

bool

abstract protected string getCurrentOwner()

Returns the owner value written into the driver for this lock.

Return Value

string

mixed get(callable|null $callback = null)

Attempt to acquire the lock.

Parameters

callable|null $callback

Return Value

mixed

mixed block(int $seconds, callable|null $callback = null)

Attempt to acquire the lock for the given number of seconds.

Parameters

int $seconds
callable|null $callback

Return Value

mixed

Exceptions

LockTimeoutException

string owner()

Returns the current owner of the lock.

Return Value

string

bool isOwnedByCurrentProcess()

Determines whether this lock is allowed to release the lock in the driver.

Return Value

bool

bool isOwnedBy(string|null $owner)

Determine whether this lock is owned by the given identifier.

Parameters

string|null $owner

Return Value

bool

$this betweenBlockedAttemptsSleepFor(int $milliseconds)

Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.

Parameters

int $milliseconds

Return Value

$this