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. To balance acquires and releases of the same lock the use of setup_call_cleanup/3 is recommended. 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 and lock_attempt/[1,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. A read write pair can be created by the predi-cates readwrite_new/1 and nonescalable_new/1. The predicates get_read/2 and get_write/2 allow retrieving the corresponding lock.

Some of the locks can produce condition variables via the predicate cond_new/2. A condition variable allows a thread temporarily leaving a critical region via the predicates cond_wait/1 and cond_wait/2. The predicates cond_notify/1 and cond_notifyall/1 on the other hand let a waiting thread respectively all waiting threads enter their critical region again.

The following lock predicates are provided:

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(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.
cond_new(L, C):
The predicate succeeds in C with a new condition for the lock L.
cond_wait(C):
The predicate succeeds when the condition C was notified.
cond_wait(C, T):
The predicate succeeds when the condition C was notified in the time-out. Otherwise the predicate fails.
cond_notify(C):
The predicate succeeds in notifying one waiting thread.
cond_notifyall(C):
The predicate succeeds in notifying all waiting threads.

Comments