bpo-46008: Stop calling _PyThreadState_Init() in new_threadstate(). (gh-29973)

This simplifies new_threadstate().  We also rename _PyThreadState_Init() to _PyThreadState_SetCurrent() to reflect what it actually does.

https://bugs.python.org/issue46008
This commit is contained in:
Eric Snow 2021-12-07 17:26:29 -07:00 committed by GitHub
parent 9b577cd01f
commit 1f384e3184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View file

@ -127,6 +127,8 @@ static inline PyInterpreterState* _PyInterpreterState_GET(void) {
// PyThreadState functions // PyThreadState functions
PyAPI_FUNC(void) _PyThreadState_SetCurrent(PyThreadState *tstate);
// We keep this around exclusively for stable ABI compatibility.
PyAPI_FUNC(void) _PyThreadState_Init( PyAPI_FUNC(void) _PyThreadState_Init(
PyThreadState *tstate); PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_DeleteExcept( PyAPI_FUNC(void) _PyThreadState_DeleteExcept(

View file

@ -6,7 +6,7 @@
#include "pycore_interp.h" // _PyInterpreterState.threads.count #include "pycore_interp.h" // _PyInterpreterState.threads.count
#include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pylifecycle.h" #include "pycore_pylifecycle.h"
#include "pycore_pystate.h" // _PyThreadState_Init() #include "pycore_pystate.h" // _PyThreadState_SetCurrent()
#include <stddef.h> // offsetof() #include <stddef.h> // offsetof()
#include "structmember.h" // PyMemberDef #include "structmember.h" // PyMemberDef
@ -1087,7 +1087,7 @@ thread_run(void *boot_raw)
#else #else
tstate->native_thread_id = 0; tstate->native_thread_id = 0;
#endif #endif
_PyThreadState_Init(tstate); _PyThreadState_SetCurrent(tstate);
PyEval_AcquireThread(tstate); PyEval_AcquireThread(tstate);
tstate->interp->threads.count++; tstate->interp->threads.count++;

View file

@ -631,7 +631,7 @@ allocate_chunk(int size_in_bytes, _PyStackChunk* previous)
} }
static PyThreadState * static PyThreadState *
new_threadstate(PyInterpreterState *interp, int init) new_threadstate(PyInterpreterState *interp)
{ {
_PyRuntimeState *runtime = interp->runtime; _PyRuntimeState *runtime = interp->runtime;
PyThreadState *tstate = (PyThreadState *)PyMem_RawCalloc(1, sizeof(PyThreadState)); PyThreadState *tstate = (PyThreadState *)PyMem_RawCalloc(1, sizeof(PyThreadState));
@ -662,10 +662,6 @@ new_threadstate(PyInterpreterState *interp, int init)
tstate->datastack_top = &tstate->datastack_chunk->data[1]; tstate->datastack_top = &tstate->datastack_chunk->data[1];
tstate->datastack_limit = (PyObject **)(((char *)tstate->datastack_chunk) + DATA_STACK_CHUNK_SIZE); tstate->datastack_limit = (PyObject **)(((char *)tstate->datastack_chunk) + DATA_STACK_CHUNK_SIZE);
if (init) {
_PyThreadState_Init(tstate);
}
HEAD_LOCK(runtime); HEAD_LOCK(runtime);
tstate->id = ++interp->threads.next_unique_id; tstate->id = ++interp->threads.next_unique_id;
tstate->next = interp->threads.head; tstate->next = interp->threads.head;
@ -680,17 +676,27 @@ new_threadstate(PyInterpreterState *interp, int init)
PyThreadState * PyThreadState *
PyThreadState_New(PyInterpreterState *interp) PyThreadState_New(PyInterpreterState *interp)
{ {
return new_threadstate(interp, 1); PyThreadState *tstate = new_threadstate(interp);
_PyThreadState_SetCurrent(tstate);
return tstate;
} }
PyThreadState * PyThreadState *
_PyThreadState_Prealloc(PyInterpreterState *interp) _PyThreadState_Prealloc(PyInterpreterState *interp)
{ {
return new_threadstate(interp, 0); return new_threadstate(interp);
}
// We keep this around for (accidental) stable ABI compatibility.
// Realisically, no extensions are using it.
void
_PyThreadState_Init(PyThreadState *tstate)
{
Py_FatalError("_PyThreadState_Init() is for internal use only");
} }
void void
_PyThreadState_Init(PyThreadState *tstate) _PyThreadState_SetCurrent(PyThreadState *tstate)
{ {
_PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate); _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
} }