annotate Demo/threads/sync.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 86e3943d0d5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Defines classes that provide synchronization objects. Note that use of
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # this module requires that your Python support threads.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #
1641
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
4 # condition(lock=None) # a POSIX-like condition-variable object
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
5 # barrier(n) # an n-thread barrier
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
6 # event() # an event object
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
7 # semaphore(n=1) # a semaphore object, with initial count n
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
8 # mrsw() # a multiple-reader single-writer lock
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # CONDITIONS
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # A condition object is created via
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # import this_module
1641
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
14 # your_condition_object = this_module.condition(lock=None)
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
15 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
16 # As explained below, a condition object has a lock associated with it,
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
17 # used in the protocol to protect condition data. You can specify a
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
18 # lock to use in the constructor, else the constructor will allocate
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
19 # an anonymous lock for you. Specifying a lock explicitly can be useful
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
20 # when more than one condition keys off the same set of shared data.
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # Methods:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # .acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # acquire the lock associated with the condition
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # .release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # release the lock associated with the condition
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # .wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # block the thread until such time as some other thread does a
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # .signal or .broadcast on the same condition, and release the
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # lock associated with the condition. The lock associated with
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # the condition MUST be in the acquired state at the time
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # .wait is invoked.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # .signal()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # wake up exactly one thread (if any) that previously did a .wait
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # on the condition; that thread will awaken with the lock associated
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # with the condition in the acquired state. If no threads are
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # .wait'ing, this is a nop. If more than one thread is .wait'ing on
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # the condition, any of them may be awakened.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # .broadcast()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # wake up all threads (if any) that are .wait'ing on the condition;
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # the threads are woken up serially, each with the lock in the
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # acquired state, so should .release() as soon as possible. If no
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # threads are .wait'ing, this is a nop.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # Note that if a thread does a .wait *while* a signal/broadcast is
1638
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
46 # in progress, it's guaranteeed to block until a subsequent
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # signal/broadcast.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # Secret feature: `broadcast' actually takes an integer argument,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # and will wake up exactly that many waiting threads (or the total
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # number waiting, if that's less). Use of this is dubious, though,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # and probably won't be supported if this form of condition is
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # reimplemented in C.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # DIFFERENCES FROM POSIX
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # + A separate mutex is not needed to guard condition data. Instead, a
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # condition object can (must) be .acquire'ed and .release'ed directly.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # This eliminates a common error in using POSIX conditions.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # + Because of implementation difficulties, a POSIX `signal' wakes up
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # _at least_ one .wait'ing thread. Race conditions make it difficult
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # to stop that. This implementation guarantees to wake up only one,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # but you probably shouldn't rely on that.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # PROTOCOL
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # Condition objects are used to block threads until "some condition" is
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # true. E.g., a thread may wish to wait until a producer pumps out data
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 # for it to consume, or a server may wish to wait until someone requests
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # its services, or perhaps a whole bunch of threads want to wait until a
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # preceding pass over the data is complete. Early models for conditions
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # relied on some other thread figuring out when a blocked thread's
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # condition was true, and made the other thread responsible both for
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 # waking up the blocked thread and guaranteeing that it woke up with all
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 # data in a correct state. This proved to be very delicate in practice,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 # and gave conditions a bad name in some circles.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 # The POSIX model addresses these problems by making a thread responsible
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # for ensuring that its own state is correct when it wakes, and relies
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # on a rigid protocol to make this easy; so long as you stick to the
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 # protocol, POSIX conditions are easy to "get right":
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 # A) The thread that's waiting for some arbitrarily-complex condition
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 # (ACC) to become true does:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 # condition.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 # while not (code to evaluate the ACC):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 # condition.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 # # That blocks the thread, *and* releases the lock. When a
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 # # condition.signal() happens, it will wake up some thread that
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # # did a .wait, *and* acquire the lock again before .wait
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # # returns.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 # #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 # # Because the lock is acquired at this point, the state used
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 # # in evaluating the ACC is frozen, so it's safe to go back &
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 # # reevaluate the ACC.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 # # At this point, ACC is true, and the thread has the condition
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 # # locked.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 # # So code here can safely muck with the shared state that
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 # # went into evaluating the ACC -- if it wants to.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 # # When done mucking with the shared state, do
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 # condition.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 # B) Threads that are mucking with shared state that may affect the
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 # ACC do:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 # condition.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 # # muck with shared state
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 # condition.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # if it's possible that ACC is true now:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 # condition.signal() # or .broadcast()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 # Note: You may prefer to put the "if" clause before the release().
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 # That's fine, but do note that anyone waiting on the signal will
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 # stay blocked until the release() is done (since acquiring the
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 # condition is part of what .wait() does before it returns).
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 # TRICK OF THE TRADE
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 # With simpler forms of conditions, it can be impossible to know when
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 # a thread that's supposed to do a .wait has actually done it. But
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 # because this form of condition releases a lock as _part_ of doing a
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # wait, the state of that lock can be used to guarantee it.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 # E.g., suppose thread A spawns thread B and later wants to wait for B to
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 # complete:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 # In A: In B:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 # B_done = condition() ... do work ...
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # B_done.acquire() B_done.acquire(); B_done.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 # spawn B B_done.signal()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # ... some time later ... ... and B exits ...
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 # B_done.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 # Because B_done was in the acquire'd state at the time B was spawned,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 # B's attempt to acquire B_done can't succeed until A has done its
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 # B_done.wait() (which releases B_done). So B's B_done.signal() is
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 # guaranteed to be seen by the .wait(). Without the lock trick, B
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 # may signal before A .waits, and then A would wait forever.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 # BARRIERS
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 # A barrier object is created via
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 # import this_module
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 # your_barrier = this_module.barrier(num_threads)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # Methods:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 # .enter()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 # the thread blocks until num_threads threads in all have done
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 # .enter(). Then the num_threads threads that .enter'ed resume,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 # and the barrier resets to capture the next num_threads threads
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 # that .enter it.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 # EVENTS
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 # An event object is created via
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # import this_module
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # your_event = this_module.event()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 # An event has two states, `posted' and `cleared'. An event is
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 # created in the cleared state.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 # Methods:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 # .post()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 # Put the event in the posted state, and resume all threads
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 # .wait'ing on the event (if any).
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 # .clear()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 # Put the event in the cleared state.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 # .is_posted()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 # Returns 0 if the event is in the cleared state, or 1 if the event
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 # is in the posted state.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 # .wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 # If the event is in the posted state, returns immediately.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 # If the event is in the cleared state, blocks the calling thread
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 # until the event is .post'ed by another thread.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 #
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 # Note that an event, once posted, remains posted until explicitly
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 # cleared. Relative to conditions, this is both the strength & weakness
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 # of events. It's a strength because the .post'ing thread doesn't have to
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 # worry about whether the threads it's trying to communicate with have
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 # already done a .wait (a condition .signal is seen only by threads that
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 # do a .wait _prior_ to the .signal; a .signal does not persist). But
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 # it's a weakness because .clear'ing an event is error-prone: it's easy
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 # to mistakenly .clear an event before all the threads you intended to
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 # see the event get around to .wait'ing on it. But so long as you don't
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 # need to .clear an event, events are easy to use safely.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 #
1638
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
195 # SEMAPHORES
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
196 #
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
197 # A semaphore object is created via
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
198 # import this_module
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
199 # your_semaphore = this_module.semaphore(count=1)
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
200 #
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
201 # A semaphore has an integer count associated with it. The initial value
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
202 # of the count is specified by the optional argument (which defaults to
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
203 # 1) passed to the semaphore constructor.
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
204 #
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
205 # Methods:
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
206 #
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
207 # .p()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
208 # If the semaphore's count is greater than 0, decrements the count
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
209 # by 1 and returns.
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
210 # Else if the semaphore's count is 0, blocks the calling thread
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
211 # until a subsequent .v() increases the count. When that happens,
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
212 # the count will be decremented by 1 and the calling thread resumed.
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
213 #
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
214 # .v()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
215 # Increments the semaphore's count by 1, and wakes up a thread (if
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
216 # any) blocked by a .p(). It's an (detected) error for a .v() to
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
217 # increase the semaphore's count to a value larger than the initial
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
218 # count.
1641
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
219 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
220 # MULTIPLE-READER SINGLE-WRITER LOCKS
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
221 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
222 # A mrsw lock is created via
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
223 # import this_module
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
224 # your_mrsw_lock = this_module.mrsw()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
225 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
226 # This kind of lock is often useful with complex shared data structures.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
227 # The object lets any number of "readers" proceed, so long as no thread
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
228 # wishes to "write". When a (one or more) thread declares its intention
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
229 # to "write" (e.g., to update a shared structure), all current readers
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
230 # are allowed to finish, and then a writer gets exclusive access; all
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
231 # other readers & writers are blocked until the current writer completes.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
232 # Finally, if some thread is waiting to write and another is waiting to
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
233 # read, the writer takes precedence.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
234 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
235 # Methods:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
236 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
237 # .read_in()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
238 # If no thread is writing or waiting to write, returns immediately.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
239 # Else blocks until no thread is writing or waiting to write. So
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
240 # long as some thread has completed a .read_in but not a .read_out,
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
241 # writers are blocked.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
242 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
243 # .read_out()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
244 # Use sometime after a .read_in to declare that the thread is done
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
245 # reading. When all threads complete reading, a writer can proceed.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
246 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
247 # .write_in()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
248 # If no thread is writing (has completed a .write_in, but hasn't yet
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
249 # done a .write_out) or reading (similarly), returns immediately.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
250 # Else blocks the calling thread, and threads waiting to read, until
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
251 # the current writer completes writing or all the current readers
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
252 # complete reading; if then more than one thread is waiting to
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
253 # write, one of them is allowed to proceed, but which one is not
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
254 # specified.
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
255 #
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
256 # .write_out()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
257 # Use sometime after a .write_in to declare that the thread is done
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
258 # writing. Then if some other thread is waiting to write, it's
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
259 # allowed to proceed. Else all threads (if any) waiting to read are
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
260 # allowed to proceed.
1959
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
261 #
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
262 # .write_to_read()
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
263 # Use instead of a .write_in to declare that the thread is done
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
264 # writing but wants to continue reading without other writers
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
265 # intervening. If there are other threads waiting to write, they
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
266 # are allowed to proceed only if the current thread calls
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
267 # .read_out; threads waiting to read are only allowed to proceed
72984
86e3943d0d5b Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 32073
diff changeset
268 # if there are no threads waiting to write. (This is a
1959
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
269 # weakness of the interface!)
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 import thread
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 class condition:
1641
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
274 def __init__(self, lock=None):
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 # the lock actually used by .acquire() and .release()
1641
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
276 if lock is None:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
277 self.mutex = thread.allocate_lock()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
278 else:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
279 if hasattr(lock, 'acquire') and \
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
280 hasattr(lock, 'release'):
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
281 self.mutex = lock
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
282 else:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
283 raise TypeError, 'condition constructor requires ' \
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
284 'a lock argument'
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 # lock used to block threads until a signal
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 self.checkout = thread.allocate_lock()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 self.checkout.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 # internal critical-section lock, & the data it protects
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 self.idlock = thread.allocate_lock()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 self.id = 0
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.waiting = 0 # num waiters subject to current release
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 self.pending = 0 # num waiters awaiting next signal
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 self.torelease = 0 # num waiters to release
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 self.releasing = 0 # 1 iff release is in progress
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 def acquire(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 self.mutex.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 def release(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 self.mutex.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 def wait(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 mutex, checkout, idlock = self.mutex, self.checkout, self.idlock
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 if not mutex.locked():
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 raise ValueError, \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 "condition must be .acquire'd when .wait() invoked"
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 idlock.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 myid = self.id
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 self.pending = self.pending + 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 idlock.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 mutex.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 while 1:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 checkout.acquire(); idlock.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 if myid < self.id:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 break
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 checkout.release(); idlock.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 self.waiting = self.waiting - 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 self.torelease = self.torelease - 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 if self.torelease:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 checkout.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 else:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 self.releasing = 0
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 if self.waiting == self.pending == 0:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 self.id = 0
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 idlock.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 mutex.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 def signal(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 self.broadcast(1)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 def broadcast(self, num = -1):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 if num < -1:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 9108
diff changeset
339 raise ValueError, '.broadcast called with num %r' % (num,)
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 if num == 0:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 return
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 self.idlock.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 if self.pending:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 self.waiting = self.waiting + self.pending
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 self.pending = 0
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 self.id = self.id + 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 if num == -1:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 self.torelease = self.waiting
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 else:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 self.torelease = min( self.waiting,
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 self.torelease + num )
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 if self.torelease and not self.releasing:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 self.releasing = 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 self.checkout.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 self.idlock.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 class barrier:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 def __init__(self, n):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 self.n = n
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 self.togo = n
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 self.full = condition()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 def enter(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 full = self.full
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 full.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 self.togo = self.togo - 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 if self.togo:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 full.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 else:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 self.togo = self.n
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 full.broadcast()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 full.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 class event:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 def __init__(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 self.state = 0
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 self.posted = condition()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 def post(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 self.posted.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 self.state = 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 self.posted.broadcast()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 self.posted.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 def clear(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 self.posted.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 self.state = 0
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 self.posted.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 def is_posted(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 self.posted.acquire()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 answer = self.state
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 self.posted.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 return answer
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 def wait(self):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 self.posted.acquire()
1638
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
398 if not self.state:
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 self.posted.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 self.posted.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
1638
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
402 class semaphore:
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
403 def __init__(self, count=1):
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
404 if count <= 0:
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
405 raise ValueError, 'semaphore count %d; must be >= 1' % count
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
406 self.count = count
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
407 self.maxcount = count
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
408 self.nonzero = condition()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
409
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
410 def p(self):
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
411 self.nonzero.acquire()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
412 while self.count == 0:
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
413 self.nonzero.wait()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
414 self.count = self.count - 1
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
415 self.nonzero.release()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
416
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
417 def v(self):
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
418 self.nonzero.acquire()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
419 if self.count == self.maxcount:
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
420 raise ValueError, '.v() tried to raise semaphore count above ' \
32073
ab0c8ddb2c92 fix error message; closes SF patch #932796
Fred Drake <fdrake@acm.org>
parents: 31515
diff changeset
421 'initial value %r' % self.maxcount
1638
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
422 self.count = self.count + 1
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
423 self.nonzero.signal()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
424 self.nonzero.release()
7a29bb3bddb4 Added semaphores; fix event.wait().
Guido van Rossum <guido@python.org>
parents: 1637
diff changeset
425
1641
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
426 class mrsw:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
427 def __init__(self):
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
428 # critical-section lock & the data it protects
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
429 self.rwOK = thread.allocate_lock()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
430 self.nr = 0 # number readers actively reading (not just waiting)
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
431 self.nw = 0 # number writers either waiting to write or writing
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
432 self.writing = 0 # 1 iff some thread is writing
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
433
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
434 # conditions
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
435 self.readOK = condition(self.rwOK) # OK to unblock readers
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
436 self.writeOK = condition(self.rwOK) # OK to unblock writers
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
437
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
438 def read_in(self):
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
439 self.rwOK.acquire()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
440 while self.nw:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
441 self.readOK.wait()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
442 self.nr = self.nr + 1
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
443 self.rwOK.release()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
444
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
445 def read_out(self):
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
446 self.rwOK.acquire()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
447 if self.nr <= 0:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
448 raise ValueError, \
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
449 '.read_out() invoked without an active reader'
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
450 self.nr = self.nr - 1
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
451 if self.nr == 0:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
452 self.writeOK.signal()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
453 self.rwOK.release()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
454
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
455 def write_in(self):
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
456 self.rwOK.acquire()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
457 self.nw = self.nw + 1
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
458 while self.writing or self.nr:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
459 self.writeOK.wait()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
460 self.writing = 1
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
461 self.rwOK.release()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
462
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
463 def write_out(self):
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
464 self.rwOK.acquire()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
465 if not self.writing:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
466 raise ValueError, \
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
467 '.write_out() invoked without an active writer'
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
468 self.writing = 0
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
469 self.nw = self.nw - 1
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
470 if self.nw:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
471 self.writeOK.signal()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
472 else:
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
473 self.readOK.broadcast()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
474 self.rwOK.release()
708654a5ddde Added optional lock parameter to condition class.
Guido van Rossum <guido@python.org>
parents: 1638
diff changeset
475
1959
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
476 def write_to_read(self):
9108
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
477 self.rwOK.acquire()
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
478 if not self.writing:
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
479 raise ValueError, \
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
480 '.write_to_read() invoked without an active writer'
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
481 self.writing = 0
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
482 self.nw = self.nw - 1
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
483 self.nr = self.nr + 1
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
484 if not self.nw:
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
485 self.readOK.broadcast()
b52864c5e9c9 nannified
Guido van Rossum <guido@python.org>
parents: 8395
diff changeset
486 self.rwOK.release()
1959
7100acb0d363 new version by Tim
Guido van Rossum <guido@python.org>
parents: 1641
diff changeset
487
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 # The rest of the file is a test case, that runs a number of parallelized
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 # quicksorts in parallel. If it works, you'll get about 600 lines of
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 # tracing output, with a line like
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 # test passed! 209 threads created in all
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 # as the last line. The content and order of preceding lines will
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 # vary across runs.
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 def _new_thread(func, *args):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 global TID
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 tid.acquire(); id = TID = TID+1; tid.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 io.acquire(); alive.append(id); \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 print 'starting thread', id, '--', len(alive), 'alive'; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 thread.start_new_thread( func, (id,) + args )
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 def _qsort(tid, a, l, r, finished):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 # sort a[l:r]; post finished when done
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 io.acquire(); print 'thread', tid, 'qsort', l, r; io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 if r-l > 1:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 pivot = a[l]
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 j = l+1 # make a[l:j] <= pivot, and a[j:r] > pivot
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 for i in range(j, r):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 if a[i] <= pivot:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 a[j], a[i] = a[i], a[j]
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 j = j + 1
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 a[l], a[j-1] = a[j-1], pivot
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 l_subarray_sorted = event()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 r_subarray_sorted = event()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 _new_thread(_qsort, a, l, j-1, l_subarray_sorted)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 _new_thread(_qsort, a, j, r, r_subarray_sorted)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 l_subarray_sorted.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 r_subarray_sorted.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 io.acquire(); print 'thread', tid, 'qsort done'; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 alive.remove(tid); io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 finished.post()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 def _randarray(tid, a, finished):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 io.acquire(); print 'thread', tid, 'randomizing array'; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 for i in range(1, len(a)):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 wh.acquire(); j = randint(0,i); wh.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 a[i], a[j] = a[j], a[i]
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 io.acquire(); print 'thread', tid, 'randomizing done'; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 alive.remove(tid); io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 finished.post()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 def _check_sort(a):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 if a != range(len(a)):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 raise ValueError, ('a not sorted', a)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 def _run_one_sort(tid, a, bar, done):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 # randomize a, and quicksort it
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 # for variety, all the threads running this enter a barrier
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 # at the end, and post `done' after the barrier exits
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 io.acquire(); print 'thread', tid, 'randomizing', a; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 finished = event()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 _new_thread(_randarray, a, finished)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 finished.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 io.acquire(); print 'thread', tid, 'sorting', a; io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 finished.clear()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 _new_thread(_qsort, a, 0, len(a), finished)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 finished.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 _check_sort(a)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 io.acquire(); print 'thread', tid, 'entering barrier'; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 bar.enter()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 io.acquire(); print 'thread', tid, 'leaving barrier'; \
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 io.acquire(); alive.remove(tid); io.release()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 bar.enter() # make sure they've all removed themselves from alive
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 ## before 'done' is posted
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 bar.enter() # just to be cruel
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 done.post()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 def test():
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 global TID, tid, io, wh, randint, alive
8395
d7c568e7707b whrandom -> random
Guido van Rossum <guido@python.org>
parents: 1959
diff changeset
569 import random
d7c568e7707b whrandom -> random
Guido van Rossum <guido@python.org>
parents: 1959
diff changeset
570 randint = random.randint
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 TID = 0 # thread ID (1, 2, ...)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 tid = thread.allocate_lock() # for changing TID
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 io = thread.allocate_lock() # for printing, and 'alive'
8395
d7c568e7707b whrandom -> random
Guido van Rossum <guido@python.org>
parents: 1959
diff changeset
575 wh = thread.allocate_lock() # for calls to random
1637
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 alive = [] # IDs of active threads
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 NSORTS = 5
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 arrays = []
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 for i in range(NSORTS):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 arrays.append( range( (i+1)*10 ) )
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
582
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 bar = barrier(NSORTS)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 finished = event()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 for i in range(NSORTS):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 _new_thread(_run_one_sort, arrays[i], bar, finished)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 finished.wait()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 print 'all threads done, and checking results ...'
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 if alive:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 raise ValueError, ('threads still alive at end', alive)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 for i in range(NSORTS):
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 a = arrays[i]
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 if len(a) != (i+1)*10:
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 raise ValueError, ('length of array', i, 'screwed up')
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 _check_sort(a)
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 print 'test passed!', TID, 'threads created in all'
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 if __name__ == '__main__':
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 test()
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
602
9af8b3991ced Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 # end of module