10000 gh-59956: Clarify GILState-related Code by ericsnowcurrently · Pull Request #101161 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-59956: Clarify GILState-related Code #101161

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 11 commits into from
Jan 19, 2023
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
Do not set PyThreadState.thread_id to a negative value.
  • Loading branch information
ericsnowcurrently committed Jan 19, 2023
commit 57bd92e359a00594fb8ba93fff414d30f26488a2
6 changes: 1 addition & 5 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ struct _ts {
PyThreadState *next;
PyInterpreterState *interp;

/* Has been initialized to a safe state.

In order to be effective, this must be set to 0 during or right
after allocation. */
int _initialized;
int _status;

int py_recursion_remaining;
int py_recursion_limit;
Expand Down
12 changes: 12 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ _PyThreadState_UpdateTracingState(PyThreadState *tstate)
}


/* PyThreadState status */

#define PyThreadState_UNINITIALIZED 0
/* Has been initialized to a safe state.

In order to be effective, this must be set to 0 during or right
after allocation. */
#define PyThreadState_INITIALIZED 1
#define PyThreadState_BOUND 2
#define PyThreadState_UNBOUND 3


/* Other */

PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
Expand Down
17 changes: 12 additions & 5 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static void
bind_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate->_status == PyThreadState_INITIALIZED);
assert(tstate->thread_id == 0);
assert(tstate->native_thread_id == 0);
_PyRuntimeState *runtime = tstate->interp->runtime;
Expand Down Expand Up @@ -183,12 +184,15 @@ bind_tstate(PyThreadState *tstate)
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif

tstate->_status = PyThreadState_BOUND;
}

static void
unbind_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate->_status == PyThreadState_BOUND);
assert(tstate->thread_id > 0);
#ifdef PY_HAVE_THREAD_NATIVE_ID
assert(tstate->native_thread_id > 0);
Expand All @@ -201,9 +205,12 @@ unbind_tstate(PyThreadState *tstate)
current_tss_clear(runtime);
}

// -1 makes sure the thread state won't be re-bound.
tstate->thread_id = -1;
tstate->native_thread_id = -1;
// We leave thread_id and native_thraed_id alone
// since they can be useful for debugging.
// Check the `_status` field to know if these values
// are still valid.

tstate->_status = PyThreadState_UNBOUND;
}

/* This is not exported, as it is not reliable! It can only
Expand Down Expand Up @@ -984,7 +991,7 @@ init_threadstate(PyThreadState *tstate,
PyInterpreterState *interp, uint64_t id,
PyThreadState *next)
{
if (tstate->_initialized) {
if (tstate->_status != PyThreadState_UNINITIALIZED) {
Py_FatalError("thread state already initialized");
}

Expand Down Expand Up @@ -1020,7 +1027,7 @@ init_threadstate(PyThreadState *tstate,
tstate->datastack_top = NULL;
tstate->datastack_limit = NULL;

tstate->_initialized = 1;
tstate->_status = PyThreadState_INITIALIZED;
}

static PyThreadState *
Expand Down
0