Skip to content

Commit 1f384e3

Browse files
bpo-46008: Stop calling _PyThreadState_Init() in new_threadstate(). (pythongh-29973)
This simplifies new_threadstate(). We also rename _PyThreadState_Init() to _PyThreadState_SetCurrent() to reflect what it actually does. https://bugs.python.org/issue46008
1 parent 9b577cd commit 1f384e3

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ static inline PyInterpreterState* _PyInterpreterState_GET(void) {
127127

128128
// PyThreadState functions
129129

130+
PyAPI_FUNC(void) _PyThreadState_SetCurrent(PyThreadState *tstate);
131+
// We keep this around exclusively for stable ABI compatibility.
130132
PyAPI_FUNC(void) _PyThreadState_Init(
131133
PyThreadState *tstate);
132134
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(

Modules/_threadmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "pycore_interp.h" // _PyInterpreterState.threads.count
77
#include "pycore_moduleobject.h" // _PyModule_GetState()
88
#include "pycore_pylifecycle.h"
9-
#include "pycore_pystate.h" // _PyThreadState_Init()
9+
#include "pycore_pystate.h" // _PyThreadState_SetCurrent()
1010
#include <stddef.h> // offsetof()
1111
#include "structmember.h" // PyMemberDef
1212

@@ -1087,7 +1087,7 @@ thread_run(void *boot_raw)
10871087
#else
10881088
tstate->native_thread_id = 0;
10891089
#endif
1090-
_PyThreadState_Init(tstate);
1090+
_PyThreadState_SetCurrent(tstate);
10911091
PyEval_AcquireThread(tstate);
10921092
tstate->interp->threads.count++;
10931093

Python/pystate.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ allocate_chunk(int size_in_bytes, _PyStackChunk* previous)
631631
}
632632

633633
static PyThreadState *
634-
new_threadstate(PyInterpreterState *interp, int init)
634+
new_threadstate(PyInterpreterState *interp)
635635
{
636636
_PyRuntimeState *runtime = interp->runtime;
637637
PyThreadState *tstate = (PyThreadState *)PyMem_RawCalloc(1, sizeof(PyThreadState));
@@ -662,10 +662,6 @@ new_threadstate(PyInterpreterState *interp, int init)
662662
tstate->datastack_top = &tstate->datastack_chunk->data[1];
663663
tstate->datastack_limit = (PyObject **)(((char *)tstate->datastack_chunk) + DATA_STACK_CHUNK_SIZE);
664664

665-
if (init) {
666-
_PyThreadState_Init(tstate);
667-
}
668-
669665
HEAD_LOCK(runtime);
670666
tstate->id = ++interp->threads.next_unique_id;
671667
tstate->next = interp->threads.head;
@@ -680,17 +676,27 @@ new_threadstate(PyInterpreterState *interp, int init)
680676
PyThreadState *
681677
PyThreadState_New(PyInterpreterState *interp)
682678
{
683-
return new_threadstate(interp, 1);
679+
PyThreadState *tstate = new_threadstate(interp);
680+
_PyThreadState_SetCurrent(tstate);
681+
return tstate;
684682
}
685683

686684
PyThreadState *
687685
_PyThreadState_Prealloc(PyInterpreterState *interp)
688686
{
689-
return new_threadstate(interp, 0);
687+
return new_threadstate(interp);
690688
}
691689

690+
// We keep this around for (accidental) stable ABI compatibility.
691+
// Realisically, no extensions are using it.
692692
void
693693
_PyThreadState_Init(PyThreadState *tstate)
694+
{
695+
Py_FatalError("_PyThreadState_Init() is for internal use only");
696+
}
697+
698+
void
699+
_PyThreadState_SetCurrent(PyThreadState *tstate)
694700
{
695701
_PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
696702
}

0 commit comments

Comments
 (0)