| 
									
										
										
										
											1999-06-27 15:00:41 +00:00
										 |  |  | \section{\module{mutex} --- | 
					
						
							|  |  |  |          Mutual exclusion support} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \declaremodule{standard}{mutex} | 
					
						
							| 
									
										
										
										
											2000-12-01 15:25:23 +00:00
										 |  |  | \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il} | 
					
						
							| 
									
										
										
										
											1999-06-27 15:00:41 +00:00
										 |  |  | \modulesynopsis{Lock and queue for mutual exclusion.} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-09 20:54:15 +00:00
										 |  |  | The \module{mutex} module defines a class that allows mutual-exclusion | 
					
						
							| 
									
										
										
										
											2000-07-16 19:01:10 +00:00
										 |  |  | via acquiring and releasing locks. It does not require (or imply) | 
					
						
							| 
									
										
										
										
											1999-06-29 14:57:12 +00:00
										 |  |  | threading or multi-tasking, though it could be useful for | 
					
						
							| 
									
										
										
										
											1999-06-27 15:00:41 +00:00
										 |  |  | those purposes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The \module{mutex} module defines the following class: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{classdesc}{mutex}{} | 
					
						
							|  |  |  | Create a new (unlocked) mutex. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A mutex has two pieces of state --- a ``locked'' bit and a queue. | 
					
						
							|  |  |  | When the mutex is not locked, the queue is empty. | 
					
						
							| 
									
										
										
										
											2001-01-09 20:54:15 +00:00
										 |  |  | Otherwise, the queue contains zero or more  | 
					
						
							| 
									
										
										
										
											1999-06-27 15:00:41 +00:00
										 |  |  | \code{(\var{function}, \var{argument})} pairs | 
					
						
							|  |  |  | representing functions (or methods) waiting to acquire the lock. | 
					
						
							|  |  |  | When the mutex is unlocked while the queue is not empty, | 
					
						
							|  |  |  | the first queue entry is removed and its  | 
					
						
							|  |  |  | \code{\var{function}(\var{argument})} pair called, | 
					
						
							|  |  |  | implying it now has the lock. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Of course, no multi-threading is implied -- hence the funny interface | 
					
						
							| 
									
										
										
										
											2001-01-09 20:54:15 +00:00
										 |  |  | for \method{lock()}, where a function is called once the lock is | 
					
						
							|  |  |  | acquired. | 
					
						
							| 
									
										
										
										
											1999-06-27 15:00:41 +00:00
										 |  |  | \end{classdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \subsection{Mutex Objects \label{mutex-objects}} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \class{mutex} objects have following methods: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{test}{} | 
					
						
							|  |  |  | Check whether the mutex is locked. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{testandset}{} | 
					
						
							|  |  |  | ``Atomic'' test-and-set, grab the lock if it is not set, | 
					
						
							| 
									
										
										
										
											2002-04-05 02:21:09 +00:00
										 |  |  | and return \code{True}, otherwise, return \code{False}. | 
					
						
							| 
									
										
										
										
											1999-06-27 15:00:41 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{lock}{function, argument} | 
					
						
							|  |  |  | Execute \code{\var{function}(\var{argument})}, unless the mutex is locked. | 
					
						
							|  |  |  | In the case it is locked, place the function and argument on the queue. | 
					
						
							|  |  |  | See \method{unlock} for explanation of when | 
					
						
							|  |  |  | \code{\var{function}(\var{argument})} is executed in that case. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{unlock}{} | 
					
						
							|  |  |  | Unlock the mutex if queue is empty, otherwise execute the first element | 
					
						
							|  |  |  | in the queue. | 
					
						
							|  |  |  | \end{methoddesc} |