@@ -27,6 +27,10 @@ extern "C" {
2727// "suspended" state. Only the thread performing a stop-the-world pause may
2828// transition a thread from the "suspended" state back to the "detached" state.
2929//
30+ // The "shutting down" state is used when the interpreter is being finalized.
31+ // Threads in this state can't do anything other than block the OS thread.
32+ // (See _PyThreadState_HangThread).
33+ //
3034// State transition diagram:
3135//
3236// (bound thread) (stop-the-world thread)
@@ -37,9 +41,10 @@ extern "C" {
3741//
3842// The (bound thread) and (stop-the-world thread) labels indicate which thread
3943// is allowed to perform the transition.
40- #define _Py_THREAD_DETACHED 0
41- #define _Py_THREAD_ATTACHED 1
42- #define _Py_THREAD_SUSPENDED 2
44+ #define _Py_THREAD_DETACHED 0
45+ #define _Py_THREAD_ATTACHED 1
46+ #define _Py_THREAD_SUSPENDED 2
47+ #define _Py_THREAD_SHUTTING_DOWN 3
4348
4449
4550/* Check if the current thread is the main thread.
@@ -118,7 +123,8 @@ extern _Py_thread_local PyThreadState *_Py_tss_tstate;
118123extern int _PyThreadState_CheckConsistency (PyThreadState * tstate );
119124#endif
120125
121- int _PyThreadState_MustExit (PyThreadState * tstate );
126+ extern int _PyThreadState_MustExit (PyThreadState * tstate );
127+ extern void _PyThreadState_HangThread (PyThreadState * tstate );
122128
123129// Export for most shared extensions, used via _PyThreadState_GET() static
124130// inline function.
@@ -169,6 +175,11 @@ extern void _PyThreadState_Detach(PyThreadState *tstate);
169175// to the "detached" state.
170176extern void _PyThreadState_Suspend (PyThreadState * tstate );
171177
178+ // Mark the thread state as "shutting down". This is used during interpreter
179+ // and runtime finalization. The thread may no longer attach to the
180+ // interpreter and will instead block via _PyThreadState_HangThread().
181+ extern void _PyThreadState_SetShuttingDown (PyThreadState * tstate );
182+
172183// Perform a stop-the-world pause for all threads in the all interpreters.
173184//
174185// Threads in the "attached" state are paused and transitioned to the "GC"
@@ -238,7 +249,7 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_NewBound(
238249 PyInterpreterState * interp ,
239250 int whence );
240251extern PyThreadState * _PyThreadState_RemoveExcept (PyThreadState * tstate );
241- extern void _PyThreadState_DeleteList (PyThreadState * list );
252+ extern void _PyThreadState_DeleteList (PyThreadState * list , int is_after_fork );
242253extern void _PyThreadState_ClearMimallocHeaps (PyThreadState * tstate );
243254
244255// Export for '_testinternalcapi' shared extension
0 commit comments