|
9 | 9 |
|
10 | 10 | __all__ = [ |
11 | 11 | 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event' |
12 | | - ] |
| 12 | +] |
13 | 13 |
|
14 | 14 | import threading |
15 | 15 | import sys |
@@ -157,7 +157,7 @@ def __repr__(self): |
157 | 157 | except Exception: |
158 | 158 | value = 'unknown' |
159 | 159 | return '<%s(value=%s, maxvalue=%s)>' % \ |
160 | | - (self.__class__.__name__, value, self._semlock.maxvalue) |
| 160 | + (self.__class__.__name__, value, self._semlock.maxvalue) |
161 | 161 |
|
162 | 162 | # |
163 | 163 | # Non-recursive lock |
@@ -253,7 +253,7 @@ def __repr__(self): |
253 | 253 |
|
254 | 254 | def wait(self, timeout=None): |
255 | 255 | assert self._lock._semlock._is_mine(), \ |
256 | | - 'must acquire() condition before using wait()' |
| 256 | + 'must acquire() condition before using wait()' |
257 | 257 |
|
258 | 258 | # indicate that this thread is going to sleep |
259 | 259 | self._sleeping_count.release() |
@@ -328,72 +328,41 @@ def wait_for(self, predicate, timeout=None): |
328 | 328 | class Event(object): |
329 | 329 |
|
330 | 330 | def __init__(self, *, ctx): |
331 | | - self._flag = ctx.Value('i', 0) |
332 | | - # Allocate a ctypes.c_ulonglong to hold the set_id: |
333 | | - # Represents the C unsigned long long datatype. |
334 | | - # The constructor accepts an optional integer initializer; no overflow checking is done. |
335 | | - # From https://docs.python.org/3/library/ctypes.html#ctypes.c_ulonglong |
336 | | - # See multiprocessing/sharedctypes.py for typecode to ctypes definitions |
337 | | - self._set_id = ctx.Value('Q', 0) |
| 331 | + self._cond = ctx.Condition(ctx.Lock()) |
| 332 | + self._flag = ctx.Semaphore(0) |
338 | 333 |
|
339 | 334 | def is_set(self): |
340 | | - # From https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Value : |
341 | | - # If lock is True (the default) then a new recursive lock object is created to synchronize access to the value. |
342 | | - with self._flag: |
343 | | - return self._flag.value == 1 |
| 335 | + with self._cond: |
| 336 | + if self._flag.acquire(False): |
| 337 | + self._flag.release() |
| 338 | + return True |
| 339 | + return False |
344 | 340 |
|
345 | 341 | def set(self): |
346 | | - # If set() is called from a signal handler, this is fine as the lock is recursive (i.e. it won't deadlock). |
347 | | - # If the thread interrupted by the signal handler is wait()-ing and the signal handler calls set(), |
348 | | - # this is fine as wait() spins on the value. Thus, after the signal handler is done, the thread will |
349 | | - # return from wait() |
350 | | - # Fixes https://github.com/python/cpython/issues/85772 |
351 | | - with self._flag: |
352 | | - with self._set_id: |
353 | | - if self._flag.value == 0: |
354 | | - # There is a theoretical chance of a race here. It requires the following conditions: |
355 | | - # The interrupted thread must be wait()ing. |
356 | | - # Then set() must be called reentrant for the maximum value of c_ulonglong + 1 times, i.e. |
357 | | - # ./python.exe -c 'from ctypes import *; print(256**sizeof(c_ulonglong()))', which |
358 | | - # evaluates to 18446744073709551616 on a 64 bit machine. |
359 | | - # All interruptions must happen exactly after `if self._flag.value == 0:`. |
360 | | - # The _set_id value will then wrap around. Then clear() must be called |
361 | | - # before the original wait() code continues. The wait() code will then continue |
362 | | - # to (incorrectly) wait. This case should be safe to ignore. The stack |
363 | | - # will probably grow too large before there is any chance of this actually happening. |
364 | | - |
365 | | - self._flag.value = 1 |
366 | | - self._set_id.value += 1 |
367 | | - # There is no race here by reentrant set() when reaching the maximum value for `self._set_id.value`. |
368 | | - # ctypes.c_ulonglong will overflow without any exception: |
369 | | - # https://docs.python.org/3/library/ctypes.html#ctypes.c_ulonglong |
370 | | - # > no overflow checking is done. |
371 | | - # This means that we do not need to check if some maximum value is reached: |
372 | | - # C will wrap around the value for us. |
| 342 | + with self._cond: |
| 343 | + self._flag.acquire(False) |
| 344 | + self._flag.release() |
| 345 | + self._cond.notify_all() |
373 | 346 |
|
374 | 347 | def clear(self): |
375 | | - with self._flag: |
376 | | - self._flag.value = 0 |
| 348 | + with self._cond: |
| 349 | + self._flag.acquire(False) |
377 | 350 |
|
378 | 351 | def wait(self, timeout=None): |
379 | | - start_time = time.monotonic() |
380 | | - set_id = self._set_id.value |
381 | | - while True: |
382 | | - if self._flag.value == 1: |
383 | | - return True |
384 | | - elif set_id != self._set_id.value: |
385 | | - return True # flag is unset, but set_id changed, so there must have been a `set` followed by a `clear` |
386 | | - # during `time.sleep()`. Fixes https://github.com/python/cpython/issues/95826 |
387 | | - elif timeout is not None and (time.monotonic() - start_time) > timeout: |
388 | | - return False |
| 352 | + with self._cond: |
| 353 | + if self._flag.acquire(False): |
| 354 | + self._flag.release() |
389 | 355 | else: |
390 | | - # Fixes https://github.com/python/cpython/issues/85772 by spinning and sleeping. |
391 | | - time.sleep(0.010) # sleep 10 milliseconds |
| 356 | + self._cond.wait(timeout) |
| 357 | + |
| 358 | + if self._flag.acquire(False): |
| 359 | + self._flag.release() |
| 360 | + return True |
| 361 | + return False |
392 | 362 |
|
393 | 363 | def __repr__(self) -> str: |
394 | 364 | set_status = 'set' if self.is_set() else 'unset' |
395 | 365 | return f"<{type(self).__qualname__} at {id(self):#x} {set_status}>" |
396 | | - |
397 | 366 | # |
398 | 367 | # Barrier |
399 | 368 | # |
|
0 commit comments