Skip to content

Commit 76bde03

Browse files
committed
Isolate all logic in _threadmodule
1 parent 0e86cf9 commit 76bde03

File tree

8 files changed

+302
-287
lines changed

8 files changed

+302
-287
lines changed

Include/cpython/pystate.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,6 @@ struct _ts {
161161
*/
162162
uintptr_t critical_section;
163163

164-
/* Boolean storing whether or not this is a daemon thread. */
165-
int is_daemon;
166-
167-
/* Set when the tstate has been cleared and unlinked from the list of
168-
* active tstates.
169-
*
170-
* This is used by _PyInterpreterState_WaitForThreads to wait for all
171-
* non-daemon threads to finish. It cannot be a PyObject because its
172-
* lifetime exceeds the tstate to which it is bound.
173-
*/
174-
struct _PyEventRc *done_event;
175-
176164
int coroutine_origin_tracking_depth;
177165

178166
PyObject *async_gen_firstiter;

Include/internal/pycore_interp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(int64_t);
302302
PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
303303
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
304304
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
305-
PyAPI_FUNC(void) _PyInterpreterState_WaitForThreads(PyInterpreterState *);
306305

307306
extern const PyConfig* _PyInterpreterState_GetConfig(PyInterpreterState *interp);
308307

Include/internal/pycore_pythread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct _pythread_runtime_state {
7878
} stubs;
7979
#endif
8080

81-
// Linked list of ThreadHandleObjects
81+
// Linked list of ThreadHandles
8282
struct llist_node handles;
8383
};
8484

Lib/threading.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
_allocate_lock = _thread.allocate_lock
3838
_LockType = _thread.LockType
3939
_thread_shutdown = _thread._shutdown
40-
_get_thread_handle = _thread._get_thread_handle
40+
_make_thread_handle = _thread._make_thread_handle
4141
get_ident = _thread.get_ident
42+
_get_main_thread_ident = _thread._get_main_thread_ident
4243
_is_main_interpreter = _thread._is_main_interpreter
4344
try:
4445
get_native_id = _thread.get_native_id
@@ -1346,8 +1347,8 @@ class _MainThread(Thread):
13461347
def __init__(self):
13471348
Thread.__init__(self, name="MainThread", daemon=False)
13481349
self._started.set()
1349-
self._set_ident()
1350-
self._handle = _get_thread_handle()
1350+
self._ident = _get_main_thread_ident()
1351+
self._handle = _make_thread_handle(self._ident)
13511352
if _HAVE_THREAD_NATIVE_ID:
13521353
self._set_native_id()
13531354
with _active_limbo_lock:
@@ -1395,7 +1396,7 @@ def __init__(self):
13951396
daemon=_daemon_threads_allowed())
13961397
self._started.set()
13971398
self._set_ident()
1398-
self._handle = _get_thread_handle()
1399+
self._handle = _make_thread_handle(self._ident)
13991400
if _HAVE_THREAD_NATIVE_ID:
14001401
self._set_native_id()
14011402
with _active_limbo_lock:
@@ -1529,16 +1530,8 @@ def _shutdown():
15291530
for atexit_call in reversed(_threading_atexits):
15301531
atexit_call()
15311532

1532-
# Main thread
1533-
if _main_thread.ident == get_ident():
1533+
if _is_main_interpreter():
15341534
_main_thread._handle._set_done()
1535-
else:
1536-
# bpo-1596321: _shutdown() must be called in the main thread.
1537-
# If the threading module was not imported by the main thread,
1538-
# _main_thread is the thread which imported the threading module.
1539-
# In this case, ignore _main_thread, similar behavior than for threads
1540-
# spawned by C libraries or using _thread.start_new_thread().
1541-
pass
15421535

15431536
# Wait for all non-daemon threads to exit.
15441537
_thread_shutdown()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
Fix a race in ``threading.Thread.join()``.
2+
3+
``threading._MainThread`` now always represents the main thread of the main
4+
interpreter.
5+
6+
``PyThreadState.on_delete`` and ``PyThreadState.on_delete_data`` have been
7+
removed.

0 commit comments

Comments
 (0)