Primitive lock objects.

A primitive lock is a synchronization primitive that is not owned by a particular coroutine when locked. A primitive lock is in one of two states, ‘locked’ or ‘unlocked’.

It is created in the unlocked state. It has two basic methods, $(D_PSYMBOL acquire()) and $(D_PSYMBOL release()). When the state is unlocked, $(D_PSYMBOL acquire()) changes the state to locked and returns immediately. When the state is locked, $(D_PSYMBOL acquire()) blocks until a call to $(D_PSYMBOL release()) in another coroutine changes it to unlocked, then the $(D_PSYMBOL acquire()) call resets it to locked and returns. The $(D_PSYMBOL release()) method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, an $(D_PSYMBOL Exception) will be thrown.

When more than one coroutine is blocked in $(D_PSYMBOL acquire()) waiting for the state to turn to unlocked, only one coroutine proceeds when a $(D_PSYMBOL release()) call resets the state to unlocked; first coroutine which is blocked in $(D_PSYMBOL acquire()) is being processed.

$(D_PSYMBOL acquire()) is a coroutine.

This class is not thread safe.


lock = new Lock(); ... lock.acquire(); scope (exit) lock.release(); ...

Lock objects can be tested for locking state:

if (!lock.locked) lock.acquire(); else; // lock is acquired ...



bool acquire()

Acquire a lock.

void release()

Release a lock.


bool locked [@property getter]

Return $(D_KEYWORD true) if lock is acquired.