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
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 a local PyThreadState in _PyInterpreterState_IDDecref().
  • Loading branch information
ericsnowcurrently committed Oct 2, 2023
commit 3e10a5c08d84277ccbbf9b960bbac7962b8c129e
41 changes: 37 additions & 4 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,10 @@ _PyInterpreterState_IDIncref(PyInterpreterState *interp)
}


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

void
_PyInterpreterState_IDDecref(PyInterpreterState *interp)
{
Expand All @@ -1183,11 +1187,13 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
PyThread_release_lock(interp->id_mutex);

if (refcount == 0 && interp->requires_idref) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
PyThreadState tstate;
PyThreadState_Init(&tstate, 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 @@ -1727,6 +1733,33 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
}
}

static 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);
}

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


//----------
// accessors
Expand Down
0