Index: Lib/mutex.py =================================================================== --- Lib/mutex.py (revisiĆ³n: 60989) +++ Lib/mutex.py (copia de trabajo) @@ -13,12 +13,14 @@ """ from collections import deque +from threading import Lock class mutex: def __init__(self): """Create a new mutex -- initially unlocked.""" self.locked = 0 self.queue = deque() + self.lock = Lock() def test(self): """Test the locked bit of the mutex.""" @@ -27,11 +29,12 @@ def testandset(self): """Atomic test-and-set -- grab the lock if it is not set, return True if it succeeded.""" - if not self.locked: - self.locked = 1 - return True - else: - return False + with self.lock: + if not self.locked: + self.locked = 1 + return True + else: + return False def lock(self, function, argument): """Lock a mutex, call the function with supplied argument @@ -45,8 +48,12 @@ def unlock(self): """Unlock a mutex. If the queue is not empty, call the next function with its argument.""" - if self.queue: - function, argument = self.queue.popleft() + call = False + with self.lock: + if self.queue: + call = True + function, argument = self.queue.popleft() + else: + self.locked = 0 + if call: function(argument) - else: - self.locked = 0