@@ -108,23 +108,31 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
108108 */
109109
110110void
111- _PyRuntimeState_ReInitThreads (void )
111+ _PyRuntimeState_ReInitThreads (_PyRuntimeState * runtime )
112112{
113113 // This was initially set in _PyRuntimeState_Init().
114- _PyRuntime .main_thread = PyThread_get_thread_ident ();
114+ runtime -> main_thread = PyThread_get_thread_ident ();
115+
116+ /* Force default allocator, since _PyRuntimeState_Fini() must
117+ use the same allocator than this function. */
118+ PyMemAllocatorEx old_alloc ;
119+ _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
120+
121+ runtime -> interpreters .mutex = PyThread_allocate_lock ();
122+ runtime -> interpreters .main -> id_mutex = PyThread_allocate_lock ();
123+ runtime -> xidregistry .mutex = PyThread_allocate_lock ();
124+
125+ PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
115126
116- _PyRuntime .interpreters .mutex = PyThread_allocate_lock ();
117- if (_PyRuntime .interpreters .mutex == NULL ) {
127+ if (runtime -> interpreters .mutex == NULL ) {
118128 Py_FatalError ("Can't initialize lock for runtime interpreters" );
119129 }
120130
121- _PyRuntime .interpreters .main -> id_mutex = PyThread_allocate_lock ();
122- if (_PyRuntime .interpreters .main -> id_mutex == NULL ) {
131+ if (runtime -> interpreters .main -> id_mutex == NULL ) {
123132 Py_FatalError ("Can't initialize ID lock for main interpreter" );
124133 }
125134
126- _PyRuntime .xidregistry .mutex = PyThread_allocate_lock ();
127- if (_PyRuntime .xidregistry .mutex == NULL ) {
135+ if (runtime -> xidregistry .mutex == NULL ) {
128136 Py_FatalError ("Can't initialize lock for cross-interpreter data registry" );
129137 }
130138}
@@ -290,20 +298,22 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
290298 * is a current interpreter state, it *must* be the main interpreter.
291299 */
292300void
293- _PyInterpreterState_DeleteExceptMain ()
301+ _PyInterpreterState_DeleteExceptMain (_PyRuntimeState * runtime )
294302{
303+ struct pyinterpreters * interpreters = & runtime -> interpreters ;
304+
295305 PyThreadState * tstate = PyThreadState_Swap (NULL );
296- if (tstate != NULL && tstate -> interp != _PyRuntime . interpreters . main ) {
306+ if (tstate != NULL && tstate -> interp != interpreters -> main ) {
297307 Py_FatalError ("PyInterpreterState_DeleteExceptMain: not main interpreter" );
298308 }
299309
300310 HEAD_LOCK ();
301- PyInterpreterState * interp = _PyRuntime . interpreters . head ;
302- _PyRuntime . interpreters . head = NULL ;
311+ PyInterpreterState * interp = interpreters -> head ;
312+ interpreters -> head = NULL ;
303313 while (interp != NULL ) {
304- if (interp == _PyRuntime . interpreters . main ) {
305- _PyRuntime . interpreters . main -> next = NULL ;
306- _PyRuntime . interpreters . head = interp ;
314+ if (interp == interpreters -> main ) {
315+ interpreters -> main -> next = NULL ;
316+ interpreters -> head = interp ;
307317 interp = interp -> next ;
308318 continue ;
309319 }
@@ -319,7 +329,7 @@ _PyInterpreterState_DeleteExceptMain()
319329 }
320330 HEAD_UNLOCK ();
321331
322- if (_PyRuntime . interpreters . head == NULL ) {
332+ if (interpreters -> head == NULL ) {
323333 Py_FatalError ("PyInterpreterState_DeleteExceptMain: missing main" );
324334 }
325335 PyThreadState_Swap (tstate );
@@ -1079,31 +1089,20 @@ _PyGILState_Fini(void)
10791089 * don't reset TSS upon fork(), see issue #10517.
10801090 */
10811091void
1082- _PyGILState_Reinit (void )
1092+ _PyGILState_Reinit (_PyRuntimeState * runtime )
10831093{
1084- /* Force default allocator, since _PyRuntimeState_Fini() must
1085- use the same allocator than this function. */
1086- PyMemAllocatorEx old_alloc ;
1087- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
1088-
1089- _PyRuntime .interpreters .mutex = PyThread_allocate_lock ();
1090-
1091- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
1092-
1093- if (_PyRuntime .interpreters .mutex == NULL ) {
1094- Py_FatalError ("Can't initialize threads for interpreter" );
1095- }
1096-
1094+ struct _gilstate_runtime_state * gilstate = & runtime -> gilstate ;
10971095 PyThreadState * tstate = PyGILState_GetThisThreadState ();
1098- PyThread_tss_delete (& _PyRuntime .gilstate .autoTSSkey );
1099- if (PyThread_tss_create (& _PyRuntime .gilstate .autoTSSkey ) != 0 ) {
1096+
1097+ PyThread_tss_delete (& gilstate -> autoTSSkey );
1098+ if (PyThread_tss_create (& gilstate -> autoTSSkey ) != 0 ) {
11001099 Py_FatalError ("Could not allocate TSS entry" );
11011100 }
11021101
11031102 /* If the thread had an associated auto thread state, reassociate it with
11041103 * the new key. */
11051104 if (tstate &&
1106- PyThread_tss_set (& _PyRuntime . gilstate . autoTSSkey , (void * )tstate ) != 0 )
1105+ PyThread_tss_set (& gilstate -> autoTSSkey , (void * )tstate ) != 0 )
11071106 {
11081107 Py_FatalError ("Couldn't create autoTSSkey mapping" );
11091108 }
0 commit comments