blob: edf6c3e02d2870205a7a3131cbd9c8a24854bde0 [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
Georg Brandl2fa4cc52010-10-28 13:01:06 +0000220 ret = self._wait_semaphore.acquire(True, timeout)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000221 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()
Georg Brandl2fa4cc52010-10-28 13:01:06 +0000228 return ret
Benjamin Petersone711caf2008-06-11 16:44:04 +0000229
230 def notify(self):
231 assert self._lock._semlock._is_mine(), 'lock is not owned'
232 assert not self._wait_semaphore.acquire(False)
233
234 # to take account of timeouts since last notify() we subtract
235 # woken_count from sleeping_count and rezero woken_count
236 while self._woken_count.acquire(False):
237 res = self._sleeping_count.acquire(False)
238 assert res
239
240 if self._sleeping_count.acquire(False): # try grabbing a sleeper
241 self._wait_semaphore.release() # wake up one sleeper
242 self._woken_count.acquire() # wait for the sleeper to wake
243
244 # rezero _wait_semaphore in case a timeout just happened
245 self._wait_semaphore.acquire(False)
246
247 def notify_all(self):
248 assert self._lock._semlock._is_mine(), 'lock is not owned'
249 assert not self._wait_semaphore.acquire(False)
250
251 # to take account of timeouts since last notify*() we subtract
252 # woken_count from sleeping_count and rezero woken_count
253 while self._woken_count.acquire(False):
254 res = self._sleeping_count.acquire(False)
255 assert res
256
257 sleepers = 0
258 while self._sleeping_count.acquire(False):
259 self._wait_semaphore.release() # wake up one sleeper
260 sleepers += 1
261
262 if sleepers:
263 for i in range(sleepers):
264 self._woken_count.acquire() # wait for a sleeper to wake
265
266 # rezero wait_semaphore in case some timeouts just happened
267 while self._wait_semaphore.acquire(False):
268 pass
269
270#
271# Event
272#
273
274class Event(object):
275
276 def __init__(self):
277 self._cond = Condition(Lock())
278 self._flag = Semaphore(0)
279
280 def is_set(self):
281 self._cond.acquire()
282 try:
283 if self._flag.acquire(False):
284 self._flag.release()
285 return True
286 return False
287 finally:
288 self._cond.release()
289
290 def set(self):
291 self._cond.acquire()
292 try:
293 self._flag.acquire(False)
294 self._flag.release()
295 self._cond.notify_all()
296 finally:
297 self._cond.release()
298
299 def clear(self):
300 self._cond.acquire()
301 try:
302 self._flag.acquire(False)
303 finally:
304 self._cond.release()
305
306 def wait(self, timeout=None):
307 self._cond.acquire()
308 try:
309 if self._flag.acquire(False):
310 self._flag.release()
311 else:
312 self._cond.wait(timeout)
Benjamin Peterson965ce872009-04-05 21:24:58 +0000313
314 if self._flag.acquire(False):
315 self._flag.release()
316 return True
317 return False
Benjamin Petersone711caf2008-06-11 16:44:04 +0000318 finally:
319 self._cond.release()