diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-05-08 18:55:02 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-05-08 18:55:02 (GMT) |
commit | d7cce26f7c71e3b2ba14198e93b28c4054bd7aea (patch) | |
tree | b11745b87d682e4c094021ecfbea0b8514df455b /Lib | |
parent | 39143f8156e7423842f073c868b8d03eb649ce26 (diff) | |
download | cpython-d7cce26f7c71e3b2ba14198e93b28c4054bd7aea.zip cpython-d7cce26f7c71e3b2ba14198e93b28c4054bd7aea.tar.gz cpython-d7cce26f7c71e3b2ba14198e93b28c4054bd7aea.tar.bz2 |
Remove the mutex module.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/mutex.py | 52 | ||||
-rw-r--r-- | Lib/test/test_mutex.py | 33 |
2 files changed, 0 insertions, 85 deletions
diff --git a/Lib/mutex.py b/Lib/mutex.py deleted file mode 100644 index 5d35bdf..0000000 --- a/Lib/mutex.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Mutual exclusion -- for use with module sched - -A mutex has two pieces of state -- a 'locked' bit and a queue. -When the mutex is not locked, the queue is empty. -Otherwise, the queue contains 0 or more (function, 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 function(argument) pair called, -implying it now has the lock. - -Of course, no multi-threading is implied -- hence the funny interface -for lock, where a function is called once the lock is aquired. -""" - -from collections import deque - -class mutex: - def __init__(self): - """Create a new mutex -- initially unlocked.""" - self.locked = 0 - self.queue = deque() - - def test(self): - """Test the locked bit of the mutex.""" - return self.locked - - 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 - - def lock(self, function, argument): - """Lock a mutex, call the function with supplied argument - when it is acquired. If the mutex is already locked, place - function and argument in the queue.""" - if self.testandset(): - function(argument) - else: - self.queue.append((function, argument)) - - 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() - function(argument) - else: - self.locked = 0 diff --git a/Lib/test/test_mutex.py b/Lib/test/test_mutex.py deleted file mode 100644 index 6318c70..0000000 --- a/Lib/test/test_mutex.py +++ /dev/null @@ -1,33 +0,0 @@ -import mutex - -import unittest -import test.test_support - -class MutexTest(unittest.TestCase): - - def setUp(self): - self.mutex = mutex.mutex() - - def called_by_mutex(self, some_data): - self.assert_(self.mutex.test(), "mutex not held") - # Nested locking - self.mutex.lock(self.called_by_mutex2, "eggs") - - def called_by_mutex2(self, some_data): - self.assert_(self.ready_for_2, - "called_by_mutex2 called too soon") - - def test_lock_and_unlock(self): - self.read_for_2 = False - self.mutex.lock(self.called_by_mutex, "spam") - self.ready_for_2 = True - # unlock both locks - self.mutex.unlock() - self.mutex.unlock() - self.failIf(self.mutex.test(), "mutex still held") - -def test_main(): - test.test_support.run_unittest(MutexTest) - -if __name__ == "__main__": - test_main() |