| 1 |
# |
| 2 |
# Module implementing synchronization primitives |
| 3 |
# |
| 4 |
# multiprocessing/synchronize.py |
| 5 |
# |
| 6 |
# Copyright (c) 2006-2008, R Oudkerk |
| 7 |
# Licensed to PSF under a Contributor Agreement. |
| 8 |
# |
| 9 |
|
| 10 |
__all__ = [ |
| 11 |
'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event' |
| 12 |
] |
| 13 |
|
| 14 |
import threading |
| 15 |
import sys |
| 16 |
import tempfile |
| 17 |
import _multiprocessing |
| 18 |
import time |
| 19 |
|
| 20 |
from . import context |
| 21 |
from . import process |
| 22 |
from . import util |
| 23 |
|
| 24 |
# TODO: Do any platforms still lack a functioning sem_open? |
| 25 |
try: |
| 26 |
from _multiprocessing import SemLock, sem_unlink |
| 27 |
except ImportError: |
| 28 |
raise ImportError("This platform lacks a functioning sem_open" + |
| 29 |
" implementation. https://github.com/python/cpython/issues/48020.") |
| 30 |
|
| 31 |
# |
| 32 |
# Constants |
| 33 |
# |
| 34 |
|
| 35 |
# These match the enum in Modules/_multiprocessing/semaphore.c |
| 36 |
RECURSIVE_MUTEX = 0 |
| 37 |
SEMAPHORE = 1 |
| 38 |
|
| 39 |
SEM_VALUE_MAX = _multiprocessing.SemLock.SEM_VALUE_MAX |
| 40 |
|
| 41 |
# |
| 42 |
# Base class for semaphores and mutexes; wraps `_multiprocessing.SemLock` |
| 43 |
# |
| 44 |
|
| 45 |
class SemLock(object): |
| 46 |
|
| 47 |
_rand = tempfile._RandomNameSequence() |
| 48 |
|
| 49 |
def __init__(self, kind, value, maxvalue, *, ctx): |
| 50 |
if ctx is None: |
| 51 |
ctx = context._default_context.get_context() |
| 52 |
self._is_fork_ctx = ctx.get_start_method() == 'fork' |
| 53 |
unlink_now = sys.platform == 'win32' or self._is_fork_ctx |
| 54 |
for i in range(100): |
| 55 |
try: |
| 56 |
sl = self._semlock = _multiprocessing.SemLock( |
| 57 |
kind, value, maxvalue, self._make_name(), |
| 58 |
unlink_now) |
| 59 |
except FileExistsError: |
| 60 |
pass |
| 61 |
else: |
| 62 |
break |
| 63 |
else: |
| 64 |
raise FileExistsError('cannot find name for semaphore') |
| 65 |
|
| 66 |
util.debug('created semlock with handle %s' % sl.handle) |
| 67 |
self._make_methods() |
| 68 |
|
| 69 |
if sys.platform != 'win32': |
| 70 |
def _after_fork(obj): |
| 71 |
obj._semlock._after_fork() |
| 72 |
util.register_after_fork(self, _after_fork) |
| 73 |
|
| 74 |
if self._semlock.name is not None: |
| 75 |
# We only get here if we are on Unix with forking |
| 76 |
# disabled. When the object is garbage collected or the |
| 77 |
# process shuts down we unlink the semaphore name |
| 78 |
from .resource_tracker import register |
| 79 |
register(self._semlock.name, "semaphore") |
| 80 |
util.Finalize(self, SemLock._cleanup, (self._semlock.name,), |
| 81 |
exitpriority=0) |
| 82 |
|
| 83 |
@staticmethod |
| 84 |
def _cleanup(name): |
| 85 |
from .resource_tracker import unregister |
| 86 |
sem_unlink(name) |
| 87 |
unregister(name, "semaphore") |
| 88 |
|
| 89 |
def _make_methods(self): |
| 90 |
self.acquire = self._semlock.acquire |
| 91 |
self.release = self._semlock.release |
| 92 |
|
| 93 |
def locked(self): |
| 94 |
return self._semlock._is_zero() |
| 95 |
|
| 96 |
def __enter__(self): |
| 97 |
return self._semlock.__enter__() |
| 98 |
|
| 99 |
def __exit__(self, *args): |
| 100 |
return self._semlock.__exit__(*args) |
| 101 |
|
| 102 |
def __getstate__(self): |
| 103 |
context.assert_spawning(self) |
| 104 |
sl = self._semlock |
| 105 |
if sys.platform == 'win32': |
| 106 |
h = context.get_spawning_popen().duplicate_for_child(sl.handle) |
| 107 |
else: |
| 108 |
if self._is_fork_ctx: |
| 109 |
raise RuntimeError('A SemLock created in a fork context is being ' |
| 110 |
'shared with a process in a spawn context. This is ' |
| 111 |
'not supported. Please use the same context to create ' |
| 112 |
'multiprocessing objects and Process.') |
| 113 |
h = sl.handle |
| 114 |
return (h, sl.kind, sl.maxvalue, sl.name) |
| 115 |
|
| 116 |
def __setstate__(self, state): |
| 117 |
self._semlock = _multiprocessing.SemLock._rebuild(*state) |
| 118 |
util.debug('recreated blocker with handle %r' % state[0]) |
| 119 |
self._make_methods() |
| 120 |
# Ensure that deserialized SemLock can be serialized again (gh-108520). |
| 121 |
self._is_fork_ctx = False |
| 122 |
|
| 123 |
@staticmethod |
| 124 |
def _make_name(): |
| 125 |
return '%s-%s' % (process.current_process()._config['semprefix'], |
| 126 |
next(SemLock._rand)) |
| 127 |
|
| 128 |
# |
| 129 |
# Semaphore |
| 130 |
# |
| 131 |
|
| 132 |
class Semaphore(SemLock): |
| 133 |
|
| 134 |
def __init__(self, value=1, *, ctx): |
| 135 |
SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX, ctx=ctx) |
| 136 |
|
| 137 |
def get_value(self): |
| 138 |
'''Returns current value of Semaphore. |
| 139 |
|
| 140 |
Raises NotImplementedError on Mac OSX |
| 141 |
because of broken sem_getvalue(). |
| 142 |
''' |
| 143 |
return self._semlock._get_value() |
| 144 |
|
| 145 |
def __repr__(self): |
| 146 |
try: |
| 147 |
value = self.get_value() |
| 148 |
except Exception: |
| 149 |
value = 'unknown' |
| 150 |
return '<%s(value=%s)>' % (self.__class__.__name__, value) |
| 151 |
|
| 152 |
# |
| 153 |
# Bounded semaphore |
| 154 |
# |
| 155 |
|
| 156 |
class BoundedSemaphore(Semaphore): |
| 157 |
|
| 158 |
def __init__(self, value=1, *, ctx): |
| 159 |
SemLock.__init__(self, SEMAPHORE, value, value, ctx=ctx) |
| 160 |
|
| 161 |
def __repr__(self): |
| 162 |
try: |
| 163 |
value = self.get_value() |
| 164 |
except Exception: |
| 165 |
value = 'unknown' |
| 166 |
return '<%s(value=%s, maxvalue=%s)>' % \ |
| 167 |
(self.__class__.__name__, value, self._semlock.maxvalue) |
| 168 |
|
| 169 |
# |
| 170 |
# Non-recursive lock |
| 171 |
# |
| 172 |
|
| 173 |
class Lock(SemLock): |
| 174 |
|
| 175 |
def __init__(self, *, ctx): |
| 176 |
SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx) |
| 177 |
|
| 178 |
def __repr__(self): |
| 179 |
try: |
| 180 |
if self._semlock._is_mine(): |
| 181 |
name = process.current_process().name |
| 182 |
if threading.current_thread().name != 'MainThread': |
| 183 |
name += '|' + threading.current_thread().name |
| 184 |
elif not self._semlock._is_zero(): |
| 185 |
name = 'None' |
| 186 |
elif self._semlock._count() > 0: |
| 187 |
name = 'SomeOtherThread' |
| 188 |
else: |
| 189 |
name = 'SomeOtherProcess' |
| 190 |
except Exception: |
| 191 |
name = 'unknown' |
| 192 |
return '<%s(owner=%s)>' % (self.__class__.__name__, name) |
| 193 |
|
| 194 |
# |
| 195 |
# Recursive lock |
| 196 |
# |
| 197 |
|
| 198 |
class RLock(SemLock): |
| 199 |
|
| 200 |
def __init__(self, *, ctx): |
| 201 |
SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1, ctx=ctx) |
| 202 |
|
| 203 |
def __repr__(self): |
| 204 |
try: |
| 205 |
if self._semlock._is_mine(): |
| 206 |
name = process.current_process().name |
| 207 |
if threading.current_thread().name != 'MainThread': |
| 208 |
name += '|' + threading.current_thread().name |
| 209 |
count = self._semlock._count() |
| 210 |
elif not self._semlock._is_zero(): |
| 211 |
name, count = 'None', 0 |
| 212 |
elif self._semlock._count() > 0: |
| 213 |
name, count = 'SomeOtherThread', 'nonzero' |
| 214 |
else: |
| 215 |
name, count = 'SomeOtherProcess', 'nonzero' |
| 216 |
except Exception: |
| 217 |
name, count = 'unknown', 'unknown' |
| 218 |
return '<%s(%s, %s)>' % (self.__class__.__name__, name, count) |
| 219 |
|
| 220 |
# |
| 221 |
# Condition variable |
| 222 |
# |
| 223 |
|
| 224 |
class Condition(object): |
| 225 |
|
| 226 |
def __init__(self, lock=None, *, ctx): |
| 227 |
self._lock = lock or ctx.RLock() |
| 228 |
self._sleeping_count = ctx.Semaphore(0) |
| 229 |
self._woken_count = ctx.Semaphore(0) |
| 230 |
self._wait_semaphore = ctx.Semaphore(0) |
| 231 |
self._make_methods() |
| 232 |
|
| 233 |
def __getstate__(self): |
| 234 |
context.assert_spawning(self) |
| 235 |
return (self._lock, self._sleeping_count, |
| 236 |
self._woken_count, self._wait_semaphore) |
| 237 |
|
| 238 |
def __setstate__(self, state): |
| 239 |
(self._lock, self._sleeping_count, |
| 240 |
self._woken_count, self._wait_semaphore) = state |
| 241 |
self._make_methods() |
| 242 |
|
| 243 |
def __enter__(self): |
| 244 |
return self._lock.__enter__() |
| 245 |
|
| 246 |
def __exit__(self, *args): |
| 247 |
return self._lock.__exit__(*args) |
| 248 |
|
| 249 |
def _make_methods(self): |
| 250 |
self.acquire = self._lock.acquire |
| 251 |
self.release = self._lock.release |
| 252 |
|
| 253 |
def __repr__(self): |
| 254 |
try: |
| 255 |
num_waiters = (self._sleeping_count.get_value() - |
| 256 |
self._woken_count.get_value()) |
| 257 |
except Exception: |
| 258 |
num_waiters = 'unknown' |
| 259 |
return '<%s(%s, %s)>' % (self.__class__.__name__, self._lock, num_waiters) |
| 260 |
|
| 261 |
def wait(self, timeout=None): |
| 262 |
assert self._lock._semlock._is_mine(), \ |
| 263 |
'must acquire() condition before using wait()' |
| 264 |
|
| 265 |
# indicate that this thread is going to sleep |
| 266 |
self._sleeping_count.release() |
| 267 |
|
| 268 |
# release lock |
| 269 |
count = self._lock._semlock._count() |
| 270 |
for i in range(count): |
| 271 |
self._lock.release() |
| 272 |
|
| 273 |
try: |
| 274 |
# wait for notification or timeout |
| 275 |
return self._wait_semaphore.acquire(True, timeout) |
| 276 |
finally: |
| 277 |
# indicate that this thread has woken |
| 278 |
self._woken_count.release() |
| 279 |
|
| 280 |
# reacquire lock |
| 281 |
for i in range(count): |
| 282 |
self._lock.acquire() |
| 283 |
|
| 284 |
def notify(self, n=1): |
| 285 |
assert self._lock._semlock._is_mine(), 'lock is not owned' |
| 286 |
assert not self._wait_semaphore.acquire( |
| 287 |
False), ('notify: Should not have been able to acquire ' |
| 288 |
+ '_wait_semaphore') |
| 289 |
|
| 290 |
# to take account of timeouts since last notify*() we subtract |
| 291 |
# woken_count from sleeping_count and rezero woken_count |
| 292 |
while self._woken_count.acquire(False): |
| 293 |
res = self._sleeping_count.acquire(False) |
| 294 |
assert res, ('notify: Bug in sleeping_count.acquire' |
| 295 |
+ '- res should not be False') |
| 296 |
|
| 297 |
sleepers = 0 |
| 298 |
while sleepers < n and self._sleeping_count.acquire(False): |
| 299 |
self._wait_semaphore.release() # wake up one sleeper |
| 300 |
sleepers += 1 |
| 301 |
|
| 302 |
if sleepers: |
| 303 |
for i in range(sleepers): |
| 304 |
self._woken_count.acquire() # wait for a sleeper to wake |
| 305 |
|
| 306 |
# rezero wait_semaphore in case some timeouts just happened |
| 307 |
while self._wait_semaphore.acquire(False): |
| 308 |
pass |
| 309 |
|
| 310 |
def notify_all(self): |
| 311 |
self.notify(n=sys.maxsize) |
| 312 |
|
| 313 |
def wait_for(self, predicate, timeout=None): |
| 314 |
result = predicate() |
| 315 |
if result: |
| 316 |
return result |
| 317 |
if timeout is not None: |
| 318 |
endtime = time.monotonic() + timeout |
| 319 |
else: |
| 320 |
endtime = None |
| 321 |
waittime = None |
| 322 |
while not result: |
| 323 |
if endtime is not None: |
| 324 |
waittime = endtime - time.monotonic() |
| 325 |
if waittime <= 0: |
| 326 |
break |
| 327 |
self.wait(waittime) |
| 328 |
result = predicate() |
| 329 |
return result |
| 330 |
|
| 331 |
# |
| 332 |
# Event |
| 333 |
# |
| 334 |
|
| 335 |
class Event(object): |
| 336 |
|
| 337 |
def __init__(self, *, ctx): |
| 338 |
self._cond = ctx.Condition(ctx.Lock()) |
| 339 |
self._flag = ctx.Semaphore(0) |
| 340 |
|
| 341 |
def is_set(self): |
| 342 |
with self._cond: |
| 343 |
if self._flag.acquire(False): |
| 344 |
self._flag.release() |
| 345 |
return True |
| 346 |
return False |
| 347 |
|
| 348 |
def set(self): |
| 349 |
with self._cond: |
| 350 |
self._flag.acquire(False) |
| 351 |
self._flag.release() |
| 352 |
self._cond.notify_all() |
| 353 |
|
| 354 |
def clear(self): |
| 355 |
with self._cond: |
| 356 |
self._flag.acquire(False) |
| 357 |
|
| 358 |
def wait(self, timeout=None): |
| 359 |
with self._cond: |
| 360 |
if self._flag.acquire(False): |
| 361 |
self._flag.release() |
| 362 |
else: |
| 363 |
self._cond.wait(timeout) |
| 364 |
|
| 365 |
if self._flag.acquire(False): |
| 366 |
self._flag.release() |
| 367 |
return True |
| 368 |
return False |
| 369 |
|
| 370 |
def __repr__(self): |
| 371 |
set_status = 'set' if self.is_set() else 'unset' |
| 372 |
return f"<{type(self).__qualname__} at {id(self):#x} {set_status}>" |
| 373 |
# |
| 374 |
# Barrier |
| 375 |
# |
| 376 |
|
| 377 |
class Barrier(threading.Barrier): |
| 378 |
|
| 379 |
def __init__(self, parties, action=None, timeout=None, *, ctx): |
| 380 |
import struct |
| 381 |
from .heap import BufferWrapper |
| 382 |
wrapper = BufferWrapper(struct.calcsize('i') * 2) |
| 383 |
cond = ctx.Condition() |
| 384 |
self.__setstate__((parties, action, timeout, cond, wrapper)) |
| 385 |
self._state = 0 |
| 386 |
self._count = 0 |
| 387 |
|
| 388 |
def __setstate__(self, state): |
| 389 |
(self._parties, self._action, self._timeout, |
| 390 |
self._cond, self._wrapper) = state |
| 391 |
self._array = self._wrapper.create_memoryview().cast('i') |
| 392 |
|
| 393 |
def __getstate__(self): |
| 394 |
return (self._parties, self._action, self._timeout, |
| 395 |
self._cond, self._wrapper) |
| 396 |
|
| 397 |
@property |
| 398 |
def _state(self): |
| 399 |
return self._array[0] |
| 400 |
|
| 401 |
@_state.setter |
| 402 |
def _state(self, value): |
| 403 |
self._array[0] = value |
| 404 |
|
| 405 |
@property |
| 406 |
def _count(self): |
| 407 |
return self._array[1] |
| 408 |
|
| 409 |
@_count.setter |
| 410 |
def _count(self, value): |
| 411 |
self._array[1] = value |