Change a variable type to avoid signed overflow; replace repeated '19999' constant...
[python.git] / Lib / multiprocessing / synchronize.py
blob6f90cb575e1c2741c0ad4064780d479f3c4a8057
2 # Module implementing synchronization primitives
4 # multiprocessing/synchronize.py
6 # Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
9 __all__ = [
10 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event'
13 import threading
14 import os
15 import sys
17 from time import time as _time, sleep as _sleep
19 import _multiprocessing
20 from multiprocessing.process import current_process
21 from multiprocessing.util import Finalize, register_after_fork, debug
22 from multiprocessing.forking import assert_spawning, Popen
24 # Try to import the mp.synchronize module cleanly, if it fails
25 # raise ImportError for platforms lacking a working sem_open implementation.
26 # See issue 3770
27 try:
28 from _multiprocessing import SemLock
29 except (ImportError):
30 raise ImportError("This platform lacks a functioning sem_open" +
31 " implementation, therefore, the required" +
32 " synchronization primitives needed will not" +
33 " function, see issue 3770.")
36 # Constants
39 RECURSIVE_MUTEX, SEMAPHORE = range(2)
40 SEM_VALUE_MAX = _multiprocessing.SemLock.SEM_VALUE_MAX
43 # Base class for semaphores and mutexes; wraps `_multiprocessing.SemLock`
46 class SemLock(object):
48 def __init__(self, kind, value, maxvalue):
49 sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
50 debug('created semlock with handle %s' % sl.handle)
51 self._make_methods()
53 if sys.platform != 'win32':
54 def _after_fork(obj):
55 obj._semlock._after_fork()
56 register_after_fork(self, _after_fork)
58 def _make_methods(self):
59 self.acquire = self._semlock.acquire
60 self.release = self._semlock.release
62 def __enter__(self):
63 return self._semlock.__enter__()
65 def __exit__(self, *args):
66 return self._semlock.__exit__(*args)
68 def __getstate__(self):
69 assert_spawning(self)
70 sl = self._semlock
71 return (Popen.duplicate_for_child(sl.handle), sl.kind, sl.maxvalue)
73 def __setstate__(self, state):
74 self._semlock = _multiprocessing.SemLock._rebuild(*state)
75 debug('recreated blocker with handle %r' % state[0])
76 self._make_methods()
79 # Semaphore
82 class Semaphore(SemLock):
84 def __init__(self, value=1):
85 SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX)
87 def get_value(self):
88 return self._semlock._get_value()
90 def __repr__(self):
91 try:
92 value = self._semlock._get_value()
93 except Exception:
94 value = 'unknown'
95 return '<Semaphore(value=%s)>' % value
98 # Bounded semaphore
101 class BoundedSemaphore(Semaphore):
103 def __init__(self, value=1):
104 SemLock.__init__(self, SEMAPHORE, value, value)
106 def __repr__(self):
107 try:
108 value = self._semlock._get_value()
109 except Exception:
110 value = 'unknown'
111 return '<BoundedSemaphore(value=%s, maxvalue=%s)>' % \
112 (value, self._semlock.maxvalue)
115 # Non-recursive lock
118 class Lock(SemLock):
120 def __init__(self):
121 SemLock.__init__(self, SEMAPHORE, 1, 1)
123 def __repr__(self):
124 try:
125 if self._semlock._is_mine():
126 name = current_process().name
127 if threading.current_thread().name != 'MainThread':
128 name += '|' + threading.current_thread().name
129 elif self._semlock._get_value() == 1:
130 name = 'None'
131 elif self._semlock._count() > 0:
132 name = 'SomeOtherThread'
133 else:
134 name = 'SomeOtherProcess'
135 except Exception:
136 name = 'unknown'
137 return '<Lock(owner=%s)>' % name
140 # Recursive lock
143 class RLock(SemLock):
145 def __init__(self):
146 SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1)
148 def __repr__(self):
149 try:
150 if self._semlock._is_mine():
151 name = current_process().name
152 if threading.current_thread().name != 'MainThread':
153 name += '|' + threading.current_thread().name
154 count = self._semlock._count()
155 elif self._semlock._get_value() == 1:
156 name, count = 'None', 0
157 elif self._semlock._count() > 0:
158 name, count = 'SomeOtherThread', 'nonzero'
159 else:
160 name, count = 'SomeOtherProcess', 'nonzero'
161 except Exception:
162 name, count = 'unknown', 'unknown'
163 return '<RLock(%s, %s)>' % (name, count)
166 # Condition variable
169 class Condition(object):
171 def __init__(self, lock=None):
172 self._lock = lock or RLock()
173 self._sleeping_count = Semaphore(0)
174 self._woken_count = Semaphore(0)
175 self._wait_semaphore = Semaphore(0)
176 self._make_methods()
178 def __getstate__(self):
179 assert_spawning(self)
180 return (self._lock, self._sleeping_count,
181 self._woken_count, self._wait_semaphore)
183 def __setstate__(self, state):
184 (self._lock, self._sleeping_count,
185 self._woken_count, self._wait_semaphore) = state
186 self._make_methods()
188 def __enter__(self):
189 return self._lock.__enter__()
191 def __exit__(self, *args):
192 return self._lock.__exit__(*args)
194 def _make_methods(self):
195 self.acquire = self._lock.acquire
196 self.release = self._lock.release
198 def __repr__(self):
199 try:
200 num_waiters = (self._sleeping_count._semlock._get_value() -
201 self._woken_count._semlock._get_value())
202 except Exception:
203 num_waiters = 'unkown'
204 return '<Condition(%s, %s)>' % (self._lock, num_waiters)
206 def wait(self, timeout=None):
207 assert self._lock._semlock._is_mine(), \
208 'must acquire() condition before using wait()'
210 # indicate that this thread is going to sleep
211 self._sleeping_count.release()
213 # release lock
214 count = self._lock._semlock._count()
215 for i in xrange(count):
216 self._lock.release()
218 try:
219 # wait for notification or timeout
220 self._wait_semaphore.acquire(True, timeout)
221 finally:
222 # indicate that this thread has woken
223 self._woken_count.release()
225 # reacquire lock
226 for i in xrange(count):
227 self._lock.acquire()
229 def notify(self):
230 assert self._lock._semlock._is_mine(), 'lock is not owned'
231 assert not self._wait_semaphore.acquire(False)
233 # to take account of timeouts since last notify() we subtract
234 # woken_count from sleeping_count and rezero woken_count
235 while self._woken_count.acquire(False):
236 res = self._sleeping_count.acquire(False)
237 assert res
239 if self._sleeping_count.acquire(False): # try grabbing a sleeper
240 self._wait_semaphore.release() # wake up one sleeper
241 self._woken_count.acquire() # wait for the sleeper to wake
243 # rezero _wait_semaphore in case a timeout just happened
244 self._wait_semaphore.acquire(False)
246 def notify_all(self):
247 assert self._lock._semlock._is_mine(), 'lock is not owned'
248 assert not self._wait_semaphore.acquire(False)
250 # to take account of timeouts since last notify*() we subtract
251 # woken_count from sleeping_count and rezero woken_count
252 while self._woken_count.acquire(False):
253 res = self._sleeping_count.acquire(False)
254 assert res
256 sleepers = 0
257 while self._sleeping_count.acquire(False):
258 self._wait_semaphore.release() # wake up one sleeper
259 sleepers += 1
261 if sleepers:
262 for i in xrange(sleepers):
263 self._woken_count.acquire() # wait for a sleeper to wake
265 # rezero wait_semaphore in case some timeouts just happened
266 while self._wait_semaphore.acquire(False):
267 pass
270 # Event
273 class Event(object):
275 def __init__(self):
276 self._cond = Condition(Lock())
277 self._flag = Semaphore(0)
279 def is_set(self):
280 self._cond.acquire()
281 try:
282 if self._flag.acquire(False):
283 self._flag.release()
284 return True
285 return False
286 finally:
287 self._cond.release()
289 def set(self):
290 self._cond.acquire()
291 try:
292 self._flag.acquire(False)
293 self._flag.release()
294 self._cond.notify_all()
295 finally:
296 self._cond.release()
298 def clear(self):
299 self._cond.acquire()
300 try:
301 self._flag.acquire(False)
302 finally:
303 self._cond.release()
305 def wait(self, timeout=None):
306 self._cond.acquire()
307 try:
308 if self._flag.acquire(False):
309 self._flag.release()
310 else:
311 self._cond.wait(timeout)
313 if self._flag.acquire(False):
314 self._flag.release()
315 return True
316 return False
317 finally:
318 self._cond.release()