annotate Lib/mutex.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 37ffce2d9c3c
children 63fa60f2d61c
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 """
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
14
531
8331076cfe15 New class syntax.
Guido van Rossum <guido@python.org>
parents: 216
diff changeset
15 class mutex:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
16 def __init__(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
17 """Create a new mutex -- initially unlocked."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
18 self.locked = 0
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
19 self.queue = []
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
20
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
21 def test(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
22 """Test the locked bit of the mutex."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
23 return self.locked
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
24
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
25 def testandset(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
26 """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
27 return True if it succeeded."""
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
28 if not self.locked:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
29 self.locked = 1
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 16657
diff changeset
30 return True
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
31 else:
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 False
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 4363
diff changeset
33
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
34 def lock(self, function, argument):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
35 """Lock a mutex, call the function with supplied argument
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
36 when it is acquired. If the mutex is already locked, place
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
37 function and argument in the queue."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
38 if self.testandset():
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
39 function(argument)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
40 else:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
41 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
42
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
43 def unlock(self):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
44 """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
45 function with its argument."""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
46 if self.queue:
24058
37ffce2d9c3c Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents: 22830
diff changeset
47 function, argument = self.queue.pop(0)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
48 function(argument)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
49 else:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11706
diff changeset
50 self.locked = 0