This commit is contained in:
ton
2024-10-07 10:13:40 +07:00
parent aa1631742f
commit 3a7d696db6
9729 changed files with 1832837 additions and 161742 deletions

View File

@@ -37,6 +37,20 @@ _daemon_threads_allowed = _thread.daemon_threads_allowed
_allocate_lock = _thread.allocate_lock
_set_sentinel = _thread._set_sentinel
get_ident = _thread.get_ident
try:
_is_main_interpreter = _thread._is_main_interpreter
except AttributeError:
# See https://github.com/python/cpython/issues/112826.
# We can pretend a subinterpreter is the main interpreter for the
# sake of _shutdown(), since that only means we do not wait for the
# subinterpreter's threads to finish. Instead, they will be stopped
# later by the mechanism we use for daemon threads. The likelihood
# of this case is small because rarely will the _thread module be
# replaced by a module without _is_main_interpreter().
# Furthermore, this is all irrelevant in applications
# that do not use subinterpreters.
def _is_main_interpreter():
return True
try:
get_native_id = _thread.get_native_id
_HAVE_THREAD_NATIVE_ID = True
@@ -238,6 +252,13 @@ class _RLock:
def _is_owned(self):
return self._owner == get_ident()
# Internal method used for reentrancy checks
def _recursion_count(self):
if self._owner != get_ident():
return 0
return self._count
_PyRLock = _RLock
@@ -311,7 +332,7 @@ class Condition:
awakened or timed out, it re-acquires the lock and returns.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
floating-point number specifying a timeout for the operation in seconds
(or fractions thereof).
When the underlying lock is an RLock, it is not released using its
@@ -621,7 +642,7 @@ class Event:
the optional timeout occurs.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
floating-point number specifying a timeout for the operation in seconds
(or fractions thereof).
This method returns the internal flag on exit, so it will always return
@@ -664,6 +685,8 @@ class Barrier:
default for all subsequent 'wait()' calls.
"""
if parties < 1:
raise ValueError("parties must be > 0")
self._cond = Condition(Lock())
self._action = action
self._timeout = timeout
@@ -1099,7 +1122,7 @@ class Thread:
or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
floating-point number specifying a timeout for the operation in seconds
(or fractions thereof). As join() always returns None, you must call
is_alive() after join() to decide whether a timeout happened -- if the
thread is still alive, the join() call timed out.
@@ -1439,7 +1462,6 @@ class _DummyThread(Thread):
def __init__(self):
Thread.__init__(self, name=_newname("Dummy-%d"),
daemon=_daemon_threads_allowed())
self._started.set()
self._set_ident()
if _HAVE_THREAD_NATIVE_ID:
@@ -1559,7 +1581,7 @@ def _shutdown():
# the main thread's tstate_lock - that won't happen until the interpreter
# is nearly dead. So we release it here. Note that just calling _stop()
# isn't enough: other threads may already be waiting on _tstate_lock.
if _main_thread._is_stopped:
if _main_thread._is_stopped and _is_main_interpreter():
# _shutdown() was already called
return
@@ -1612,6 +1634,7 @@ def main_thread():
In normal conditions, the main thread is the thread from which the
Python interpreter was started.
"""
# XXX Figure this out for subinterpreters. (See gh-75698.)
return _main_thread
# get thread-local implementation, either from the thread
@@ -1663,6 +1686,11 @@ def _after_fork():
# its new value since it can have changed.
thread._reset_internal_locks(True)
ident = get_ident()
if isinstance(thread, _DummyThread):
thread.__class__ = _MainThread
thread._name = 'MainThread'
thread._daemonic = False
thread._set_tstate_lock()
thread._ident = ident
new_active[ident] = thread
else: