blob: 7f4718c8472b90756ed4478850700aecc529bcc7 [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Module implementing synchronization primitives
3#
4# multiprocessing/synchronize.py
5#
6# Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
7#
8
9__all__ = [
10 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event'
11 ]
12
13import threading
14import os
15import sys
16
17from time import time as _time, sleep as _sleep
18
19import _multiprocessing
20from multiprocessing.process import current_process
21from multiprocessing.util import Finalize, register_after_fork, debug
22from multiprocessing.forking import assert_spawning, Popen
23
Benjamin Petersone5384b02008-10-04 22:00:42 +000024# 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
27try:
28 from _multiprocessing import SemLock
29except (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.")
34
Benjamin Petersone711caf2008-06-11 16:44:04 +000035#
36# Constants
37#
38
39RECURSIVE_MUTEX, SEMAPHORE = list(range(2))
40SEM_VALUE_MAX = _multiprocessing.SemLock.SEM_VALUE_MAX
41
42#
43# Base class for semaphores and mutexes; wraps `_multiprocessing.SemLock`
44#
45
46class SemLock(object):
47
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()
52
53 if sys.platform != 'win32':
54 def _after_fork(obj):
55 obj._semlock._after_fork()
56 register_after_fork(self, _after_fork)
57
58 def _make_methods(self):
59 self.acquire = self._semlock.acquire
60 self.release = self._semlock.release
Benjamin Peterson8cc7d882009-06-01 23:14:51 +000061
62 def __enter__(self):
63 return self._semlock.__enter__()
64
65 def __exit__(self, *args):
66 return self._semlock.__exit__(*args)
Benjamin Petersone711caf2008-06-11 16:44:04 +000067
68 def __getstate__(self):
69 assert_spawning(self)
70 sl = self._semlock
71 return (Popen.duplicate_for_child(sl.handle), sl.kind, sl.maxvalue)
72
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()
77
78#
79# Semaphore
80#
81
82class Semaphore(SemLock):
83
84 def __init__(self, value=1):
85 SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX)
86
87 def get_value(self):
88 return self._semlock._get_value()
89
90 def __repr__(self):
91 try:
92 value = self._semlock._get_value()
93 except Exception:
94 value = 'unknown'
95 return '<Semaphore(value=%s)>' % value
96
97#
98# Bounded semaphore
99#
100
101class BoundedSemaphore(Semaphore):
102
103 def __init__(self, value=1):
104 SemLock.__init__(self, SEMAPHORE, value, value)
105
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)
113
114#
115# Non-recursive lock
116#
117
118class Lock(SemLock):
119
120 def __init__(self):
121 SemLock.__init__(self, SEMAPHORE, 1, 1)
122
123 def __repr__(self):
124 try:
125 if self._semlock._is_mine():
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000126 name = current_process().name
127 if threading.current_thread().name != 'MainThread':
128 name += '|' + threading.current_thread().name
Benjamin Petersone711caf2008-06-11 16:44:04 +0000129 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
138
139#
140# Recursive lock
141#
142
143class RLock(SemLock):
144
145 def __init__(self):
146 SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1)
147
148 def __repr__(self):
149 try:
150 if self._semlock._is_mine():
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000151 name = current_process().name
152 if threading.current_thread().name != 'MainThread':
153 name += '|' + threading.current_thread().name
Benjamin Petersone711caf2008-06-11 16:44:04 +0000154 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)
164
165#
166# Condition variable
167#
168
169class Condition(object):
170
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()
177
178 def __getstate__(self):
179 assert_spawning(self)
180 return (self._lock, self._sleeping_count,
181 self._woken_count, self._wait_semaphore)
182
183 def __setstate__(self, state):
184 (self._lock, self._sleeping_count,
185 self._woken_count, self._wait_semaphore) = state
186 self._make_methods()
187
Benjamin Peterson8cc7d882009-06-01 23:14:51 +0000188 def __enter__(self):
189 return self._lock.__enter__()
190
191 def __exit__(self, *args):
192 return self._lock.__exit__(*args)
193
Benjamin Petersone711caf2008-06-11 16:44:04 +0000194 def _make_methods(self):
195 self.acquire = self._lock.acquire
196 self.release = self._lock.release
Benjamin Petersone711caf2008-06-11 16:44:04 +0000197
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)
205
206 def wait(self, timeout=None):
207 assert self._lock._semlock._is_mine(), \
208 'must acquire() condition before using wait()'
209
210 # indicate that this thread is going to sleep
211 self._sleeping_count.release()
212
213 # release lock
214 count = self._lock._semlock._count()
215 for i in range(count):
216 self._lock.release()
217
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()
224
225 # reacquire lock
226 for i in range(count):
227 self._lock.acquire()
228
229 def notify(self):
230 assert self._lock._semlock._is_mine(), 'lock is not owned'
231 assert not self._wait_semaphore.acquire(False)
232
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
238
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
242
243 # rezero _wait_semaphore in case a timeout just happened
244 self._wait_semaphore.acquire(False)
245
246 def notify_all(self):
247 assert self._lock._semlock._is_mine(), 'lock is not owned'
248 assert not self._wait_semaphore.acquire(False)
249
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
255
256 sleepers = 0
257 while self._sleeping_count.acquire(False):
258 self._wait_semaphore.release() # wake up one sleeper
259 sleepers += 1
260
261 if sleepers:
262 for i in range(sleepers):
263 self._woken_count.acquire() # wait for a sleeper to wake
264
265 # rezero wait_semaphore in case some timeouts just happened
266 while self._wait_semaphore.acquire(False):
267 pass
268
269#
270# Event
271#
272
273class Event(object):
274
275 def __init__(self):
276 self._cond = Condition(Lock())
277 self._flag = Semaphore(0)
278
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()
288
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()
297
298 def clear(self):
299 self._cond.acquire()
300 try:
301 self._flag.acquire(False)
302 finally:
303 self._cond.release()
304
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)
Benjamin Peterson965ce872009-04-05 21:24:58 +0000312
313 if self._flag.acquire(False):
314 self._flag.release()
315 return True
316 return False
Benjamin Petersone711caf2008-06-11 16:44:04 +0000317 finally:
318 self._cond.release()