annotate Demo/threads/condition.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 7100acb0d363
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1959
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Defines classes that provide synchronization objects. Note that use of
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # this module requires that your Python support threads.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # condition() # a POSIX-like condition-variable object
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # barrier(n) # an n-thread barrier
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # event() # an event object
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # semaphore(n=1)# a semaphore object, with initial count n
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # CONDITIONS
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # A condition object is created via
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # import this_module
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # your_condition_object = this_module.condition()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # Methods:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # .acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # acquire the lock associated with the condition
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # .release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # release the lock associated with the condition
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # .wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # block the thread until such time as some other thread does a
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # .signal or .broadcast on the same condition, and release the
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # lock associated with the condition. The lock associated with
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # the condition MUST be in the acquired state at the time
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # .wait is invoked.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # .signal()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # wake up exactly one thread (if any) that previously did a .wait
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # on the condition; that thread will awaken with the lock associated
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # with the condition in the acquired state. If no threads are
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # .wait'ing, this is a nop. If more than one thread is .wait'ing on
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # the condition, any of them may be awakened.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # .broadcast()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # wake up all threads (if any) that are .wait'ing on the condition;
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # the threads are woken up serially, each with the lock in the
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # acquired state, so should .release() as soon as possible. If no
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # threads are .wait'ing, this is a nop.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # Note that if a thread does a .wait *while* a signal/broadcast is
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # in progress, it's guaranteeed to block until a subsequent
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # signal/broadcast.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # Secret feature: `broadcast' actually takes an integer argument,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # and will wake up exactly that many waiting threads (or the total
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # number waiting, if that's less). Use of this is dubious, though,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # and probably won't be supported if this form of condition is
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # reimplemented in C.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # DIFFERENCES FROM POSIX
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # + A separate mutex is not needed to guard condition data. Instead, a
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # condition object can (must) be .acquire'ed and .release'ed directly.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # This eliminates a common error in using POSIX conditions.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # + Because of implementation difficulties, a POSIX `signal' wakes up
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # _at least_ one .wait'ing thread. Race conditions make it difficult
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # to stop that. This implementation guarantees to wake up only one,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # but you probably shouldn't rely on that.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # PROTOCOL
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # Condition objects are used to block threads until "some condition" is
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # true. E.g., a thread may wish to wait until a producer pumps out data
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # for it to consume, or a server may wish to wait until someone requests
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # its services, or perhaps a whole bunch of threads want to wait until a
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # preceding pass over the data is complete. Early models for conditions
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # relied on some other thread figuring out when a blocked thread's
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # condition was true, and made the other thread responsible both for
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # waking up the blocked thread and guaranteeing that it woke up with all
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # data in a correct state. This proved to be very delicate in practice,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 # and gave conditions a bad name in some circles.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # The POSIX model addresses these problems by making a thread responsible
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # for ensuring that its own state is correct when it wakes, and relies
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # on a rigid protocol to make this easy; so long as you stick to the
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 # protocol, POSIX conditions are easy to "get right":
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 # A) The thread that's waiting for some arbitrarily-complex condition
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 # (ACC) to become true does:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # condition.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # while not (code to evaluate the ACC):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 # condition.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 # # That blocks the thread, *and* releases the lock. When a
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 # # condition.signal() happens, it will wake up some thread that
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 # # did a .wait, *and* acquire the lock again before .wait
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 # # returns.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 # #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 # # Because the lock is acquired at this point, the state used
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 # # in evaluating the ACC is frozen, so it's safe to go back &
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 # # reevaluate the ACC.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # # At this point, ACC is true, and the thread has the condition
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # # locked.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 # # So code here can safely muck with the shared state that
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 # # went into evaluating the ACC -- if it wants to.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 # # When done mucking with the shared state, do
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 # condition.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 # B) Threads that are mucking with shared state that may affect the
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 # ACC do:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 # condition.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 # # muck with shared state
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 # condition.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 # if it's possible that ACC is true now:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 # condition.signal() # or .broadcast()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 # Note: You may prefer to put the "if" clause before the release().
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 # That's fine, but do note that anyone waiting on the signal will
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 # stay blocked until the release() is done (since acquiring the
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 # condition is part of what .wait() does before it returns).
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 # TRICK OF THE TRADE
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 # With simpler forms of conditions, it can be impossible to know when
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 # a thread that's supposed to do a .wait has actually done it. But
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 # because this form of condition releases a lock as _part_ of doing a
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 # wait, the state of that lock can be used to guarantee it.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 # E.g., suppose thread A spawns thread B and later wants to wait for B to
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 # complete:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 # In A: In B:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # B_done = condition() ... do work ...
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 # B_done.acquire() B_done.acquire(); B_done.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 # spawn B B_done.signal()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 # ... some time later ... ... and B exits ...
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 # B_done.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 # Because B_done was in the acquire'd state at the time B was spawned,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 # B's attempt to acquire B_done can't succeed until A has done its
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # B_done.wait() (which releases B_done). So B's B_done.signal() is
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 # guaranteed to be seen by the .wait(). Without the lock trick, B
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # may signal before A .waits, and then A would wait forever.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 # BARRIERS
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 # A barrier object is created via
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 # import this_module
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 # your_barrier = this_module.barrier(num_threads)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 # Methods:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 # .enter()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 # the thread blocks until num_threads threads in all have done
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 # .enter(). Then the num_threads threads that .enter'ed resume,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 # and the barrier resets to capture the next num_threads threads
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 # that .enter it.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # EVENTS
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 # An event object is created via
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 # import this_module
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 # your_event = this_module.event()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 # An event has two states, `posted' and `cleared'. An event is
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 # created in the cleared state.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 # Methods:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # .post()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 # Put the event in the posted state, and resume all threads
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 # .wait'ing on the event (if any).
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 # .clear()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 # Put the event in the cleared state.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 # .is_posted()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 # Returns 0 if the event is in the cleared state, or 1 if the event
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 # is in the posted state.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 # .wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 # If the event is in the posted state, returns immediately.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 # If the event is in the cleared state, blocks the calling thread
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 # until the event is .post'ed by another thread.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 # Note that an event, once posted, remains posted until explicitly
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # cleared. Relative to conditions, this is both the strength & weakness
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 # of events. It's a strength because the .post'ing thread doesn't have to
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 # worry about whether the threads it's trying to communicate with have
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 # already done a .wait (a condition .signal is seen only by threads that
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 # do a .wait _prior_ to the .signal; a .signal does not persist). But
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 # it's a weakness because .clear'ing an event is error-prone: it's easy
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 # to mistakenly .clear an event before all the threads you intended to
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 # see the event get around to .wait'ing on it. But so long as you don't
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 # need to .clear an event, events are easy to use safely.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 # SEMAPHORES
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 # A semaphore object is created via
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 # import this_module
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 # your_semaphore = this_module.semaphore(count=1)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 # A semaphore has an integer count associated with it. The initial value
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 # of the count is specified by the optional argument (which defaults to
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 # 1) passed to the semaphore constructor.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 # Methods:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 # .p()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 # If the semaphore's count is greater than 0, decrements the count
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 # by 1 and returns.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 # Else if the semaphore's count is 0, blocks the calling thread
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 # until a subsequent .v() increases the count. When that happens,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 # the count will be decremented by 1 and the calling thread resumed.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 # .v()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 # Increments the semaphore's count by 1, and wakes up a thread (if
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 # any) blocked by a .p(). It's an (detected) error for a .v() to
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 # increase the semaphore's count to a value larger than the initial
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 # count.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 import thread
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 class condition:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 def __init__(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 # the lock actually used by .acquire() and .release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 self.mutex = thread.allocate_lock()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 # lock used to block threads until a signal
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 self.checkout = thread.allocate_lock()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 self.checkout.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 # internal critical-section lock, & the data it protects
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 self.idlock = thread.allocate_lock()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 self.id = 0
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 self.waiting = 0 # num waiters subject to current release
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 self.pending = 0 # num waiters awaiting next signal
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 self.torelease = 0 # num waiters to release
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 self.releasing = 0 # 1 iff release is in progress
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 def acquire(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 self.mutex.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 def release(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 self.mutex.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 def wait(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 mutex, checkout, idlock = self.mutex, self.checkout, self.idlock
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 if not mutex.locked():
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 raise ValueError, \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 "condition must be .acquire'd when .wait() invoked"
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 idlock.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 myid = self.id
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 self.pending = self.pending + 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 idlock.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 mutex.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 while 1:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 checkout.acquire(); idlock.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 if myid < self.id:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 break
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 checkout.release(); idlock.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 self.waiting = self.waiting - 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 self.torelease = self.torelease - 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 if self.torelease:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 checkout.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 else:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 self.releasing = 0
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 if self.waiting == self.pending == 0:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 self.id = 0
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 idlock.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 mutex.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 def signal(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 self.broadcast(1)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 def broadcast(self, num = -1):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 if num < -1:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 raise ValueError, '.broadcast called with num ' + `num`
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if num == 0:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 return
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 self.idlock.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 if self.pending:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 self.waiting = self.waiting + self.pending
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 self.pending = 0
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 self.id = self.id + 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 if num == -1:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 self.torelease = self.waiting
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 else:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 self.torelease = min( self.waiting,
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 self.torelease + num )
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 if self.torelease and not self.releasing:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 self.releasing = 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 self.checkout.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 self.idlock.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 class barrier:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 def __init__(self, n):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.n = n
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 self.togo = n
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 self.full = condition()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 def enter(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 full = self.full
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 full.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 self.togo = self.togo - 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 if self.togo:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 full.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 else:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 self.togo = self.n
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 full.broadcast()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 full.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 class event:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 def __init__(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 self.state = 0
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 self.posted = condition()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 def post(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 self.posted.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 self.state = 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 self.posted.broadcast()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 self.posted.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 def clear(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 self.posted.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 self.state = 0
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 self.posted.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
323
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 def is_posted(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 self.posted.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 answer = self.state
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 self.posted.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 return answer
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 def wait(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 self.posted.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 if not self.state:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 self.posted.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 self.posted.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 class semaphore:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 def __init__(self, count=1):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 if count <= 0:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 raise ValueError, 'semaphore count %d; must be >= 1' % count
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 self.count = count
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 self.maxcount = count
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 self.nonzero = condition()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
343
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 def p(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 self.nonzero.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 while self.count == 0:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 self.nonzero.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 self.count = self.count - 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 self.nonzero.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 def v(self):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 self.nonzero.acquire()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 if self.count == self.maxcount:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 raise ValueError, '.v() tried to raise semaphore count above ' \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 'initial value ' + `maxcount`
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 self.count = self.count + 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 self.nonzero.signal()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 self.nonzero.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
359
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 # The rest of the file is a test case, that runs a number of parallelized
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 # quicksorts in parallel. If it works, you'll get about 600 lines of
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 # tracing output, with a line like
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 # test passed! 209 threads created in all
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 # as the last line. The content and order of preceding lines will
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 # vary across runs.
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 def _new_thread(func, *args):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 global TID
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 tid.acquire(); id = TID = TID+1; tid.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 io.acquire(); alive.append(id); \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 print 'starting thread', id, '--', len(alive), 'alive'; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 thread.start_new_thread( func, (id,) + args )
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 def _qsort(tid, a, l, r, finished):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 # sort a[l:r]; post finished when done
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 io.acquire(); print 'thread', tid, 'qsort', l, r; io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 if r-l > 1:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 pivot = a[l]
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 j = l+1 # make a[l:j] <= pivot, and a[j:r] > pivot
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 for i in range(j, r):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 if a[i] <= pivot:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 a[j], a[i] = a[i], a[j]
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 j = j + 1
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 a[l], a[j-1] = a[j-1], pivot
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
386
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 l_subarray_sorted = event()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 r_subarray_sorted = event()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 _new_thread(_qsort, a, l, j-1, l_subarray_sorted)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 _new_thread(_qsort, a, j, r, r_subarray_sorted)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 l_subarray_sorted.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 r_subarray_sorted.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
393
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 io.acquire(); print 'thread', tid, 'qsort done'; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 alive.remove(tid); io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 finished.post()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 def _randarray(tid, a, finished):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 io.acquire(); print 'thread', tid, 'randomizing array'; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 for i in range(1, len(a)):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 wh.acquire(); j = randint(0,i); wh.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 a[i], a[j] = a[j], a[i]
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 io.acquire(); print 'thread', tid, 'randomizing done'; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 alive.remove(tid); io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 finished.post()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 def _check_sort(a):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 if a != range(len(a)):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 raise ValueError, ('a not sorted', a)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
411
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 def _run_one_sort(tid, a, bar, done):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 # randomize a, and quicksort it
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 # for variety, all the threads running this enter a barrier
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 # at the end, and post `done' after the barrier exits
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 io.acquire(); print 'thread', tid, 'randomizing', a; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 finished = event()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 _new_thread(_randarray, a, finished)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 finished.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
421
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 io.acquire(); print 'thread', tid, 'sorting', a; io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 finished.clear()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 _new_thread(_qsort, a, 0, len(a), finished)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 finished.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 _check_sort(a)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
427
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 io.acquire(); print 'thread', tid, 'entering barrier'; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 bar.enter()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 io.acquire(); print 'thread', tid, 'leaving barrier'; \
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 io.acquire(); alive.remove(tid); io.release()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 bar.enter() # make sure they've all removed themselves from alive
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 ## before 'done' is posted
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 bar.enter() # just to be cruel
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 done.post()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
438
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 def test():
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 global TID, tid, io, wh, randint, alive
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 import whrandom
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 randint = whrandom.randint
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 TID = 0 # thread ID (1, 2, ...)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 tid = thread.allocate_lock() # for changing TID
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 io = thread.allocate_lock() # for printing, and 'alive'
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 wh = thread.allocate_lock() # for calls to whrandom
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 alive = [] # IDs of active threads
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 NSORTS = 5
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 arrays = []
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 for i in range(NSORTS):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 arrays.append( range( (i+1)*10 ) )
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 bar = barrier(NSORTS)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 finished = event()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 for i in range(NSORTS):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 _new_thread(_run_one_sort, arrays[i], bar, finished)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 finished.wait()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 print 'all threads done, and checking results ...'
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 if alive:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 raise ValueError, ('threads still alive at end', alive)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 for i in range(NSORTS):
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 a = arrays[i]
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 if len(a) != (i+1)*10:
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 raise ValueError, ('length of array', i, 'screwed up')
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 _check_sort(a)
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
469
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 print 'test passed!', TID, 'threads created in all'
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 if __name__ == '__main__':
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 test()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 # end of module