Diviner

Class PhutilLock

Definedsrc/filesystem/PhutilLock.php:30
GroupFilesystem

Base class for locks, like file locks.

libphutil provides a concrete lock in PhutilFileLock.

$lock->lock();
  do_contentious_things();
$lock->unlock();

If the lock can't be acquired because it is already held, PhutilLockException is thrown. Other exceptions indicate permanent failure unrelated to locking.

When extending this class, you should call getLock() to look up an existing lock object, and registerLock() when objects are constructed to register for automatic unlock on shutdown.

Tasks

Lock Implementation

Lock Registry

Constructing Locks

Determining Lock Status

Locking

Internals

Methods

protected this __construct($name)

parametersstring$nameGlobally unique lock name.
returnthis

Build a new lock, given a lock name. The name should be globally unique across all locks.

protected abstract void doLock($wait)

parametersfloat$waitSeconds to block waiting for the lock.
returnvoid

Acquires the lock, or throws PhutilLockException if it fails.

protected abstract void doUnlock()

returnvoid

Releases the lock.

protected static getLock($name)

parametersstring$nameLock name.
returnwild

Get a named lock, if it has been registered.

final public string getName()

returnstringGlobally unique lock name, across all locks.

Returns a globally unique name for this lock.

final public bool isLocked()

returnboolTrue if the lock is held.

Determine if the lock is currently held.

final public this lock($wait = 0)

parametersfloat$waitSeconds to block waiting for the lock. By default, do not block.
returnthis

Acquire the lock. If lock acquisition fails because the lock is held by another process, throws PhutilLockException. Other exceptions indicate that lock acquisition has failed for reasons unrelated to locking.

If the lock is already held by this process, this method throws. You can test the lock status with isLocked().

protected static registerLock(PhutilLock $lock)

parametersPhutilLock$lockLock to register.
returnwild

Register a lock for cleanup when the process exits.

final public this unlock()

returnthis

Release the lock. Throws an exception on failure, e.g. if the lock is not currently held.

public static void unlockAll()

returnvoid

On shutdown, we release all the locks. You should not call this method directly. Use unlock() to release individual locks.