annotate Lib/mutex.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b674706f3b10
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
1 """Mutual exclusion -- for use with module sched
216
1754b8024b55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
3 A mutex has two pieces of state -- a 'locked' bit and a queue.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
4 When the mutex is not locked, the queue is empty.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
5 Otherwise, the queue contains 0 or more (function, argument) pairs
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
6 representing functions (or methods) waiting to acquire the lock.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
7 When the mutex is unlocked while the queue is not empty,
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
8 the first queue entry is removed and its function(argument) pair called,
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
9 implying it now has the lock.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
10
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
11 Of course, no multi-threading is implied -- hence the funny interface
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
12 for lock, where a function is called once the lock is aquired.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
13 """
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 31436
diff changeset
14 from warnings import warnpy3k
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 31436
diff changeset
15 warnpy3k("the mutex module has been removed in Python 3.0", stacklevel=2)
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 31436
diff changeset
16 del warnpy3k
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
17
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 24058
diff changeset
18 from collections import deque
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 24058
diff changeset
19
531
8331076cfe15 New class syntax.
Guido van Rossum <guido@python.org>
parents: 216
diff changeset
20 class mutex:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
21 def __init__(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
22 """Create a new mutex -- initially unlocked."""
51127
b674706f3b10 use True and False
Benjamin Peterson <benjamin@python.org>
parents: 46752
diff changeset
23 self.locked = False
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 24058
diff changeset
24 self.queue = deque()
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
25
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
26 def test(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
27 """Test the locked bit of the mutex."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
28 return self.locked
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
29
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
30 def testandset(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
31 """Atomic test-and-set -- grab the lock if it is not set,
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 16657
diff changeset
32 return True if it succeeded."""
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
33 if not self.locked:
51127
b674706f3b10 use True and False
Benjamin Peterson <benjamin@python.org>
parents: 46752
diff changeset
34 self.locked = True
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 16657
diff changeset
35 return True
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
36 else:
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 16657
diff changeset
37 return False
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
38
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
39 def lock(self, function, argument):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
40 """Lock a mutex, call the function with supplied argument
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
41 when it is acquired. If the mutex is already locked, place
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
42 function and argument in the queue."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
43 if self.testandset():
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
44 function(argument)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
45 else:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
46 self.queue.append((function, argument))
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
47
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
48 def unlock(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
49 """Unlock a mutex. If the queue is not empty, call the next
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
50 function with its argument."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
51 if self.queue:
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 24058
diff changeset
52 function, argument = self.queue.popleft()
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
53 function(argument)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
54 else:
51127
b674706f3b10 use True and False
Benjamin Peterson <benjamin@python.org>
parents: 46752
diff changeset
55 self.locked = False