8000 Do not set PyThreadState.thread_id to a negative value. · python/cpython@57bd92e · GitHub
[go: up one dir, main page]

Skip to content

Commit 57bd92e

Browse files
Do not set PyThreadState.thread_id to a negative value.
1 parent 5e7283c commit 57bd92e

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

Include/cpython/pystate.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,7 @@ struct _ts {
114114
PyThreadState *next;
115115
PyInterpreterState *interp;
116116

117-
/* Has been initialized to a safe state.
118-
119-
In order to be effective, this must be set to 0 during or right
120-
after allocation. */
121-
int _initialized;
117+
int _status;
122118

123119
int py_recursion_remaining;
124120
int py_recursion_limit;

Include/internal/pycore_pystate.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ _PyThreadState_UpdateTracingState(PyThreadState *tstate)
139139
}
140140

141141

142+
/* PyThreadState status */
143+
144+
#define PyThreadState_UNINITIALIZED 0
145+
/* Has been initialized to a safe state.
146+
147+
In order to be effective, this must be set to 0 during or right
148+
after allocation. */
149+
#define PyThreadState_INITIALIZED 1
150+
#define PyThreadState_BOUND 2
151+
#define PyThreadState_UNBOUND 3
152+
153+
142154
/* Other */
143155

144156
PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(

Python/pystate.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static void
152152
bind_tstate(PyThreadState *tstate)
153153
{
154154
assert(tstate != NULL);
155+
assert(tstate->_status == PyThreadState_INITIALIZED);
155156
assert(tstate->thread_id == 0);
156157
assert(tstate->native_thread_id == 0);
157158
_PyRuntimeState *runtime = tstate->interp->runtime;
@@ -183,12 +184,15 @@ bind_tstate(PyThreadState *tstate)
183184
#ifdef PY_HAVE_THREAD_NATIVE_ID
184185
tstate->native_thread_id = PyThread_get_thread_native_id();
185186
#endif
187+
188+
tstate->_status = PyThreadState_BOUND;
186189
}
187190

188191
static void
189192
unbind_tstate(PyThreadState *tstate)
190193
{
191194
assert(tstate != NULL);
195+
assert(tstate->_status == PyThreadState_BOUND);
192196
assert(tstate->thread_id > 0);
193197
#ifdef PY_HAVE_THREAD_NATIVE_ID
194198
assert(tstate->native_thread_id > 0);
@@ -201,9 +205,12 @@ unbind_tstate(PyThreadState *tstate)
201205
current_tss_clear(runtime);
202206
}
203207

204-
// -1 makes sure the thread state won't be re-bound.
205-
tstate->thread_id = -1;
206-
tstate->native_thread_id = -1;
208+
// We leave thread_id and native_thraed_id alone
209+
// since they can be useful for debugging.
210+
// Check the `_status` field to know if these values
211+
// are still valid.
212+
213+
tstate->_status = PyThreadState_UNBOUND;
207214
}
208215

209216
/* This is not exported, as it is not reliable! It can only
@@ -984,7 +991,7 @@ init_threadstate(PyThreadState *tstate,
984991
PyInterpreterState *interp, uint64_t id,
985992
PyThreadState *next)
986993
{
987-
if (tstate->_initialized) {
994+
if (tstate->_status != PyThreadState_UNINITIALIZED) {
988995
Py_FatalError("thread state already initialized");
989996
}
990997

@@ -1020,7 +1027,7 @@ init_threadstate(PyThreadState *tstate,
10201027
tstate->datastack_top = NULL;
10211028
tstate->datastack_limit = NULL;
10221029

1023-
tstate->_initialized = 1;
1030+
tstate->_status = PyThreadState_INITIALIZED;
10241031
}
10251032

10261033
static PyThreadState *

0 commit comments

Comments
 (0)
0