Module "lock"

A mutex is a binary lock. A mutex can be created by the predicates mutex_new/1 and unslotted_new/2. A mutex need not be explicitly destroyed, it will automatically be reclaimed by the Java GC when not anymore used. Threads waiting for a lock can be interrupted.

Example:
?- mutex_new(M), lock_acquire(M), lock_release(M), lock_acquire(M).
M = 0r3f10bc2a

The predicates lock_acquire/1, lock_attempt/1 and lock_attempt_timeout/2 allow to acquire a lock. These predicates will block, fail or timeout when the lock has already been acquired by other threads. The predicate lock_release/1 allows releasing the lock.

As a convenience a lock can be acquired and released by the meta-predicate with_lock/2. A read write pair can be created by the predicates readwrite_new/1 and nonescalable_new/1. The predicates get_read/2 and get_write/2 allow retrieving the corresponding lock.

The following lock predicates are provided:

with_lock(L, G):
The predicate succeeds whenever the goal G succeeds. The lock L is acquired in the call port, and released in the deterministic exit port, in the fail port or when an exception happens.
mutex_new(M):
The predicate succeeds for a new slotted mutex M. The lock can produce condition variables.
unslotted_new(M):
The predicate succeeds for a new unslotted mutex M. The lock cannot produce condition variables.
lock_aquire(L):
The predicate succeeds after locking the lock L.
lock_attempt(L):
The predicate succeeds after locking the lock L. Otherwise the predicate fails.
lock_attempt_timeout(L, T):
The predicate succeeds after locking the lock L in the timeout T. Otherwise the predicate fails.
lock_release(L):
The predicate succeeds after unlocking the lock L.
readwrite_new(P):
The predicate succeeds for a new slotted and escalable read write pair P. The lock can produce condition variables.
nonescalable_new(P):
The predicate succeeds for a new unslotted and non-escalable read write pair P. The lock cannot produce condition variables.
get_read(P, R):
The predicate succeeds for the read lock R of the read write pair P.
get_write(P, W):
The predicate succeeds for the write lock W of the read write pair P.

Kommentare