Skip to content

Commit 406d3d2

Browse files
ncoghlanericsnowcurrently
authored andcommitted
Add PyInterpreterState_Main().
1 parent 1c41852 commit 406d3d2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Include/pystate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
293293
/* Routines for advanced debuggers, requested by David Beazley.
294294
Don't use unless you know what you are doing! */
295295
#ifndef Py_LIMITED_API
296+
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
296297
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
297298
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
298299
PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);

Python/pystate.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
4646
/* The single PyInterpreterState used by this process'
4747
GILState implementation
4848
*/
49+
/* TODO: Given interp_main, it may be possible to kill this ref */
4950
static PyInterpreterState *autoInterpreterState = NULL;
5051
static int autoTLSkey = -1;
5152
#else
@@ -55,6 +56,7 @@ static int autoTLSkey = -1;
5556
#endif
5657

5758
static PyInterpreterState *interp_head = NULL;
59+
static PyInterpreterState *interp_main = NULL;
5860

5961
/* Assuming the current thread holds the GIL, this is the
6062
PyThreadState for the current thread. */
@@ -102,6 +104,9 @@ PyInterpreterState_New(void)
102104

103105
HEAD_LOCK();
104106
interp->next = interp_head;
107+
if (interp_main == NULL) {
108+
interp_main = interp;
109+
}
105110
interp_head = interp;
106111
HEAD_UNLOCK();
107112
}
@@ -159,6 +164,11 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
159164
if (interp->tstate_head != NULL)
160165
Py_FatalError("PyInterpreterState_Delete: remaining threads");
161166
*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+
}
162172
HEAD_UNLOCK();
163173
PyMem_RawFree(interp);
164174
#ifdef WITH_THREAD
@@ -624,6 +634,12 @@ PyInterpreterState_Head(void)
624634
return interp_head;
625635
}
626636

637+
PyInterpreterState *
638+
PyInterpreterState_Main(void)
639+
{
640+
return interp_main;
641+
}
642+
627643
PyInterpreterState *
628644
PyInterpreterState_Next(PyInterpreterState *interp) {
629645
return interp->next;

0 commit comments

Comments
 (0)