blob: f8acba4a35f93301bea1c528fb3286dc2384b33d [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Mutual exclusion -- for use with module sched
Guido van Rossume2e162e1991-04-21 19:32:43 +00002
Guido van Rossum54f22ed2000-02-04 15:10:34 +00003A mutex has two pieces of state -- a 'locked' bit and a queue.
4When the mutex is not locked, the queue is empty.
5Otherwise, the queue contains 0 or more (function, argument) pairs
6representing functions (or methods) waiting to acquire the lock.
7When the mutex is unlocked while the queue is not empty,
8the first queue entry is removed and its function(argument) pair called,
9implying it now has the lock.
10
11Of course, no multi-threading is implied -- hence the funny interface
12for lock, where a function is called once the lock is aquired.
13"""
Brett Cannon9d441822008-05-08 19:26:08 +000014from warnings import warnpy3k
15warnpy3k("the mutex module has been removed in Python 3.0", stacklevel=2)
16del warnpy3k
Guido van Rossum54f22ed2000-02-04 15:10:34 +000017
Raymond Hettinger756b3f32004-01-29 06:37:52 +000018from collections import deque
19
Guido van Rossumce084481991-12-26 13:06:29 +000020class mutex:
Tim Peters07e99cb2001-01-14 23:47:14 +000021 def __init__(self):
22 """Create a new mutex -- initially unlocked."""
Benjamin Petersonfd0107f2009-01-27 23:15:48 +000023 self.locked = False
Raymond Hettinger756b3f32004-01-29 06:37:52 +000024 self.queue = deque()
Guido van Rossum54f22ed2000-02-04 15:10:34 +000025
Tim Peters07e99cb2001-01-14 23:47:14 +000026 def test(self):
27 """Test the locked bit of the mutex."""
28 return self.locked
Guido van Rossum54f22ed2000-02-04 15:10:34 +000029
Tim Peters07e99cb2001-01-14 23:47:14 +000030 def testandset(self):
31 """Atomic test-and-set -- grab the lock if it is not set,
Tim Petersbc0e9102002-04-04 22:55:58 +000032 return True if it succeeded."""
Tim Peters07e99cb2001-01-14 23:47:14 +000033 if not self.locked:
Benjamin Petersonfd0107f2009-01-27 23:15:48 +000034 self.locked = True
Tim Petersbc0e9102002-04-04 22:55:58 +000035 return True
Tim Peters07e99cb2001-01-14 23:47:14 +000036 else:
Tim Petersbc0e9102002-04-04 22:55:58 +000037 return False
Guido van Rossum54f22ed2000-02-04 15:10:34 +000038
Tim Peters07e99cb2001-01-14 23:47:14 +000039 def lock(self, function, argument):
40 """Lock a mutex, call the function with supplied argument
41 when it is acquired. If the mutex is already locked, place
42 function and argument in the queue."""
43 if self.testandset():
44 function(argument)
45 else:
46 self.queue.append((function, argument))
Guido van Rossum54f22ed2000-02-04 15:10:34 +000047
Tim Peters07e99cb2001-01-14 23:47:14 +000048 def unlock(self):
49 """Unlock a mutex. If the queue is not empty, call the next
50 function with its argument."""
51 if self.queue:
Raymond Hettinger756b3f32004-01-29 06:37:52 +000052 function, argument = self.queue.popleft()
Tim Peters07e99cb2001-01-14 23:47:14 +000053 function(argument)
54 else:
Benjamin Petersonfd0107f2009-01-27 23:15:48 +000055 self.locked = False