next up previous contents index
Next: 7.7.1.2 Prolog Message Queues Up: 7.7.1 Predicates for Thread Previous: 7.7.1 Predicates for Thread   Contents   Index

7.7.1.1 User-defined Mutexes

 One of the simplest and most powerful primitives for thread synchronization are mutexes. The mutexes provided by the following primitives are recursive: if a thread $T$ locks a recursive mutex $M$, any calls to mutex_lock(M) made by $T$ will immediately succeed without suspending while $M$ is locked. Other threads that attempt to lock $M$ will suspend until $M$ is unlocked. To unlock $M$ after $n$ calls to mutex_lock(M), $T$ must make $n$ calls to mutex_unlock(M).

When using mutexes in XSB, programmers must not only avoid explicitly creating deadlocks, but must also ensure that a mutex is unlocked when leaving a critical area, and destroyed when it is no longer needed. Making sure that this happens for successful goals, for failed goals and for goals that raise exceptions can sometimes be complicated. The predicate with_mutex/2 handles all of these cases. We recommend using it if possible, and making use of lower-level calls to mutex_lock/1, mutex_unlock/1 and mutex_trylock/1 only when with_mutex/2 is not applicable.

with_mutex(+Mutex,?Goal)
mutex_xsb
Locks a current mutex or aliasMutex, executes Goal deterministically, then unlocks Mutex. If Goal leaves choice-points, these are destroyed. Mutex is unlocked regardless of whether Goal succeeds, fails or raises an exception. Any exception thrown by Goal is re-thrown after the mutex has been successfully unlocked.

Error Cases

mutex_create(?Mutex)
mutex_xsb
Creates a new recursive user mutex with identifier Mutex. Options allows optional parameters to be passed, currently only for aliases of the mutex.

Error Cases

mutex_destroy(+Mutex)
mutex_xsb
Destroys a current unlocked mutex with alias or id Mutex along with any memory it uses.

Error Cases

mutex_lock(+Mutex)
mutex_xsb
mutex_lock(Mutex) locks a (recursive) mutex with alias or id Mutex. Locking and unlocking mutexes should be paired carefully in order to avoid deadlocks. In particular, a programmer needs to ensure that mutexes are properly unlocked even if the protected code fails or raises an exception.

Error Cases

mutex_trylock(+Mutex)
mutex_xsb
Works as mutex_lock/1 but fails immediately if Mutex is held by another thread, rather than suspending the calling thread.

Error Cases

mutex_unlock(+Mutex)
mutex_xsb
Unlocks the mutex with alias or id Mutex when called by the same thread that locked Mutex.

Error Cases

mutex_unlock_all
mutex_xsb
mutex_unlock_all/0 unlocks all user mutexes owned by the current thread. It has no error cases.

mutex_property(?Mutex,?Property)
mutex_xsb
If Mutex is instantiated, unifies Property with current properties of the mutex; if Mutex is a variable, backtracks through all the current mutexes whose properties unify with Property. If the mutex is locked, Property will be a term of the form locked(ThreadId,NumLocks) where ThreadId is a thread id, and NumLocks is the number of times the mutex has been locked. If the mutex is unlocked, Property will be a term of the form unlocked(NumLocks) where NumLocks is the number of times the mutex has been locked.

Error Cases


next up previous contents index
Next: 7.7.1.2 Prolog Message Queues Up: 7.7.1 Predicates for Thread Previous: 7.7.1 Predicates for Thread   Contents   Index
Terrance Swift 2007-10-05