@@ -46,6 +46,7 @@ static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
46
46
/* The single PyInterpreterState used by this process'
47
47
GILState implementation
48
48
*/
49
+ /* TODO: Given interp_main, it may be possible to kill this ref */
49
50
static PyInterpreterState * autoInterpreterState = NULL ;
50
51
static int autoTLSkey = -1 ;
51
52
#else
@@ -55,6 +56,7 @@ static int autoTLSkey = -1;
55
56
#endif
56
57
57
58
static PyInterpreterState * interp_head = NULL ;
59
+ static PyInterpreterState * interp_main = NULL ;
58
60
59
61
/* Assuming the current thread holds the GIL, this is the
60
62
PyThreadState for the current thread. */
@@ -102,6 +104,9 @@ PyInterpreterState_New(void)
102
104
103
105
HEAD_LOCK ();
104
106
interp -> next = interp_head ;
107
+ if (interp_main == NULL ) {
108
+ interp_main = interp ;
109
+ }
105
110
interp_head = interp ;
106
111
HEAD_UNLOCK ();
107
112
}
@@ -159,6 +164,11 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
159
164
if (interp -> tstate_head != NULL )
160
165
Py_FatalError ("PyInterpreterState_Delete: remaining threads" );
161
166
* p = interp -> next ;
167
+ if (interp_main == interp ) {
168
+ interp_main = NULL ;
169
+ if (interp_head != NULL )
170
+ Py_FatalError ("PyInterpreterState_Delete: remaining subinterpreters" );
171
+ }
162
172
HEAD_UNLOCK ();
163
173
PyMem_RawFree (interp );
164
174
#ifdef WITH_THREAD
@@ -624,6 +634,12 @@ PyInterpreterState_Head(void)
624
634
return interp_head ;
625
635
}
626
636
637
+ PyInterpreterState *
638
+ PyInterpreterState_Main (void )
639
+ {
640
+ return interp_main ;
641
+ }
642
+
627
643
PyInterpreterState *
628
644
PyInterpreterState_Next (PyInterpreterState * interp ) {
629
645
return interp -> next ;
0 commit comments