8000 gh-109860: Use a New Thread State When Switching Interpreters, When Necessary by ericsnowcurrently · Pull Request #110245 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-109860: Use a New Thread State When Switching Interpreters, When Necessary #110245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use existing API.
  • Loading branch information
ericsnowcurrently committed Oct 2, 2023
commit 781cb53c186965b6e3af868bf2803db8149d490d
7 changes: 0 additions & 7 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ struct _ts {
# define Py_C_RECURSION_LIMIT 1500
#endif

PyAPI_FUNC(void) PyThreadState_Init(
PyThreadState *tstate,
PyInterpreterState *interp,
int whence);
PyAPI_FUNC(void) PyThreadState_Fini(PyThreadState *tstate);
PyAPI_FUNC(void) PyThreadState_Bind(PyThreadState *tstate);


/* other API */

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static inline PyInterpreterState* _PyInterpreterState_GET(void) {
extern PyThreadState * _PyThreadState_New(
PyInterpreterState *interp,
int whence);
#define _PyThreadState_Bind(tstate) PyThreadState_Bind(tstate)
extern void _PyThreadState_Bind(PyThreadState *tstate);
extern void _PyThreadState_DeleteExcept(PyThreadState *tstate);

// Export for '_testinternalcapi' shared extension
Expand Down
18 changes: 8 additions & 10 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,9 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp,
// Switch to interpreter.
PyThreadState *save_tstate = NULL;
PyThreadState *tstate = NULL;
PyThreadState _tstate;
if (interp != PyInterpreterState_Get()) {
tstate = &_tstate;
PyThreadState_Init(tstate, interp, _PyThreadState_WHENCE_EXEC);
PyThreadState_Bind(tstate);
tstate = PyThreadState_New(interp);
tstate->_whence = _PyThreadState_WHENCE_EXEC;
// XXX Possible GILState issues?
save_tstate = PyThreadState_Swap(tstate);
}
Expand All @@ -445,8 +443,9 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp,

// Switch back.
if (save_tstate != NULL) {
PyThreadState_Clear(tstate);
PyThreadState_Swap(save_tstate);
PyThreadState_Fini(tstate);
PyThreadState_Delete(tstate);
}

// Propagate any exception out to the caller.
Expand Down Expand Up @@ -565,12 +564,11 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
}

// Destroy the interpreter.
PyThreadState tstate;
PyThreadState_Init(&tstate, interp, _PyThreadState_WHENCE_INTERP);
PyThreadState_Bind(&tstate);
PyThreadState *tstate = PyThreadState_New(interp);
tstate->_whence = _PyThreadState_WHENCE_INTERP;
// XXX Possible GILState issues?
PyThreadState *save_tstate = PyThreadState_Swap(&tstate);
Py_EndInterpreter(&tstate);
PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Py_EndInterpreter(tstate);
PyThreadState_Swap(save_tstate);

Py_RETURN_NONE;
Expand Down
41 changes: 6 additions & 35 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,13 +1183,13 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
PyThread_release_lock(interp->id_mutex);

if (refcount == 0 && interp->requires_idref) {
PyThreadState tstate;
PyThreadState_Init(&tstate, interp, _PyThreadState_WHENCE_INTERP);
_PyThreadState_Bind(&tstate);
PyThreadState *tstate = _PyThreadState_New(interp,
_PyThreadState_WHENCE_INTERP);
_PyThreadState_Bind(tstate);

// XXX Possible GILState issues?
PyThreadState *save_tstate = _PyThreadState_Swap(runtime, &tstate);
Py_EndInterpreter(&tstate);
PyThreadState *save_tstate = _PyThreadState_Swap(runtime, tstate);
Py_EndInterpreter(tstate);
_PyThreadState_Swap(runtime, save_tstate);
}
}
Expand Down Expand Up @@ -1441,7 +1441,6 @@ new_threadstate(PyInterpreterState *interp, int whence)
PyThreadState *old_head = interp->threads.head;
if (old_head == NULL) {
// It's the interpreter's initial thread state.
assert(id == 1);
used_newtstate = 0;
tstate = &interp->_initial_thread;
}
Expand Down Expand Up @@ -1729,34 +1728,6 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
}


void
PyThreadState_Init(PyThreadState *tstate,
PyInterpreterState *interp, int whence)
{
HEAD_LOCK(interp->runtime);

interp->threads.next_unique_id += 1;
uint64_t id = interp->threads.next_unique_id;

// Set to _PyThreadState_INIT.
memcpy(tstate,
&initial._main_interpreter._initial_thread,
sizeof(*tstate));

init_threadstate(tstate, interp, id, whence);
add_threadstate(interp, tstate, interp->threads.head);

HEAD_UNLOCK(interp->runtime);
}

void
PyThreadState_Fini(PyThreadState *tstate)
{
PyThreadState_Clear(tstate);
tstate_delete_common(tstate);
}


//----------
// accessors
//----------
Expand Down Expand Up @@ -1989,7 +1960,7 @@ PyThreadState_Swap(PyThreadState *newts)


void
PyThreadState_Bind(PyThreadState *tstate)
_PyThreadState_Bind(PyThreadState *tstate)
{
// gh-104690: If Python is being finalized and PyInterpreterState_Delete()
// was called, tstate becomes a dangling pointer.
Expand Down
0