blob: 48936904e04966e320e14dc5ad6e5c5b7656599b [file] [log] [blame]
Fred Drake22313571999-06-27 15:00:41 +00001\section{\module{mutex} ---
2 Mutual exclusion support}
3
4\declaremodule{standard}{mutex}
Fred Drake57657bc2000-12-01 15:25:23 +00005\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drake22313571999-06-27 15:00:41 +00006\modulesynopsis{Lock and queue for mutual exclusion.}
7
Fred Drake986eafd2001-01-09 20:54:15 +00008The \module{mutex} module defines a class that allows mutual-exclusion
Thomas Woutersf8316632000-07-16 19:01:10 +00009via acquiring and releasing locks. It does not require (or imply)
Fred Drake4886c661999-06-29 14:57:12 +000010threading or multi-tasking, though it could be useful for
Fred Drake22313571999-06-27 15:00:41 +000011those purposes.
12
13The \module{mutex} module defines the following class:
14
15\begin{classdesc}{mutex}{}
16Create a new (unlocked) mutex.
17
18A mutex has two pieces of state --- a ``locked'' bit and a queue.
19When the mutex is not locked, the queue is empty.
Fred Drake986eafd2001-01-09 20:54:15 +000020Otherwise, the queue contains zero or more
Fred Drake22313571999-06-27 15:00:41 +000021\code{(\var{function}, \var{argument})} pairs
22representing functions (or methods) waiting to acquire the lock.
23When the mutex is unlocked while the queue is not empty,
24the first queue entry is removed and its
25\code{\var{function}(\var{argument})} pair called,
26implying it now has the lock.
27
28Of course, no multi-threading is implied -- hence the funny interface
Fred Drake986eafd2001-01-09 20:54:15 +000029for \method{lock()}, where a function is called once the lock is
30acquired.
Fred Drake22313571999-06-27 15:00:41 +000031\end{classdesc}
32
33
34\subsection{Mutex Objects \label{mutex-objects}}
35
36\class{mutex} objects have following methods:
37
38\begin{methoddesc}{test}{}
39Check whether the mutex is locked.
40\end{methoddesc}
41
42\begin{methoddesc}{testandset}{}
43``Atomic'' test-and-set, grab the lock if it is not set,
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000044and return \code{True}, otherwise, return \code{False}.
Fred Drake22313571999-06-27 15:00:41 +000045\end{methoddesc}
46
47\begin{methoddesc}{lock}{function, argument}
48Execute \code{\var{function}(\var{argument})}, unless the mutex is locked.
49In the case it is locked, place the function and argument on the queue.
50See \method{unlock} for explanation of when
51\code{\var{function}(\var{argument})} is executed in that case.
52\end{methoddesc}
53
54\begin{methoddesc}{unlock}{}
55Unlock the mutex if queue is empty, otherwise execute the first element
56in the queue.
57\end{methoddesc}