8000 gh-59956: Clarify Runtime State Status Expectations by ericsnowcurrently · Pull Request #101308 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-59956: Clarify Runtime State Status Expectations #101308

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b64ce9f
Factor out tstate_verify_not_active().
ericsnowcurrently Jan 19, 2023
1be0ec4
Drop the check_current param from _PyThreadState_Delete().
ericsnowcurrently Jan 19, 2023
3454bf1
Drop _PyThreadState_Delete().
ericsnowcurrently Jan 19, 2023
a929e32
Unset the "current" thread state before zapping the threads instead o…
ericsnowcurrently Jan 19, 2023
e6ddd92
Only clear the current thread if the interpreter matches.
ericsnowcurrently Jan 19, 2023
36c512f
Always "check_current" in zapthreads().
ericsnowcurrently Jan 19, 2023
d402a82
Do not pass the runtime to _PyThreadState_DeleteExcept().
ericsnowcurrently Jan 19, 2023
2ddfe71
Add some notes, TODO comments, and asserts.
ericsnowcurrently Jan 19, 2023
99de509
Mark the main interpreter as finalizing during runtime fini.
ericsnowcurrently Jan 19, 2023
b77ae5e
Add more notes and TODO comments.
ericsnowcurrently Jan 19, 2023
9ca673c
Make PyThreadState._status more granular.
ericsnowcurrently Jan 19, 2023
8a71957
Add more status fields.
ericsnowcurrently Jan 20, 2023
62d1a93
Add a TODO.
ericsnowcurrently Jan 20, 2023
90cea92
Track active status.
ericsnowcurrently Jan 19, 2023
9965813
Clarify a TODO comment.
ericsnowcurrently Jan 20, 2023
fd5048b
Associate "bound" and "active".
ericsnowcurrently Jan 20, 2023
2105cd6
_PyThreadState_Prealloc() -> _PyThreadState_New()
ericsnowcurrently Jan 23, 2023
8b110cf
Factor out tstate_tss_*().
ericsnowcurrently Jan 24, 2023
ca98d68
current_tss_*() -> gilstate_tss_*().
ericsnowcurrently Jan 24, 2023
4f39976
Add bind_gilstate_tstate() and unbind_gilstate_tstate().
ericsnowcurrently Jan 24, 2023
6e73669
Update some TODO comments.
ericsnowcurrently Jan 24, 2023
cc3540d
Clean up _PyThreadState_Swap() a little.
ericsnowcurrently Jan 24, 2023
1ce3841
Fix the stable ABI.
ericsnowcurrently Jan 25, 2023
121d328
Fixes for multiprocessing.
ericsnowcurrently Jan 25, 2023
8666362
Move a comment.
ericsnowcurrently Jan 25, 2023
52ac2d9
Preserve errno more carefully.
ericsnowcurrently Jan 25, 2023
f7ac19a
Do not call bind_gilstate_tstate() in bind_tstate().
ericsnowcurrently Jan 25, 2023
a338248
Add an assert.
ericsnowcurrently Jan 25, 2023
5be78e9
Clean up bind_gilstate_tstate().
ericsnowcurrently Jan 25, 2023
f019bd6
Clear bound_gilstate for the old thread state.
ericsnowcurrently Jan 25, 2023
6869bfe
bound_gilstate and gilstate_tss_get() must match.
ericsnowcurrently Jan 25, 2023
f91d458
Add a blank line for clarity.
ericsnowcurrently Jan 25, 2023
9b45398
Do not call unbind_gilstate_tstate() in unbind_tstate().
ericsnowcurrently Jan 25, 2023
98a2dae
Fix comments.
ericsnowcurrently Jan 25, 2023
afde196
Fix padding.
ericsnowcurrently Jan 30, 2023
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
Add bind_gilstate_tstate() and unbind_gilstate_tstate().
  • Loading branch information
ericsnowcurrently committed Jan 25, 2023
commit 4f39976bf2ae723712b50b036815e8f7e6f91cbf
4 changes: 3 additions & 1 deletion Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ struct _ts {
unsigned int bound:1;
/* Has been unbound from its OS thread. */
unsigned int unbound:1;
/* Has been bound aa current for the GILState API. */
unsigned int bound_gilstate:1;
/* Currently in use (maybe holds the GIL). */
unsigned int active:1;

Expand All @@ -139,7 +141,7 @@ struct _ts {
unsigned int finalized:1;

/* padding to align to 4 bytes */
unsigned int :26;
unsigned int :25;
} _status;

int py_recursion_remaining;
Expand Down
105 changes: 78 additions & 27 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,73 @@ tstate_is_bound(PyThreadState *tstate)
return tstate->_status.bound && !tstate->_status.unbound;
}

static void bind_gilstate_tstate(PyThreadState *);
static void unbind_gilstate_tstate(PyThreadState *);

static void
bind_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate_is_alive(tstate) && !tstate->_status.bound);
assert(!tstate->_status.unbound); // just in case
assert(!tstate->_status.bound_gilstate);
assert(tstate != gilstate_tss_get(tstate->interp->runtime));
assert(!tstate->_status.active);
assert(tstate->thread_id == 0);
assert(tstate->native_thread_id == 0);

// Currently we don't necessarily store the thread state
// in thread-local storage (e.g. per-interpreter).

tstate->thread_id = PyThread_get_thread_ident();
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif

tstate->_status.bound = 1;

// This make sure there's a gilstate tstate bound
// as soon as possible.
if (gilstate_tss_get(tstate->interp->runtime) == NULL) {
bind_gilstate_tstate(tstate);
}
}

static void
unbind_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
// XXX assert(tstate_is_alive(tstate));
assert(tstate_is_bound(tstate));
// XXX assert(!tstate->_status.active);
assert(tstate->thread_id > 0);
#ifdef PY_HAVE_THREAD_NATIVE_ID
assert(tstate->native_thread_id > 0);
#endif

if (tstate->_status.bound_gilstate) {
unbind_gilstate_tstate(tstate);
}

// 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.

// We leave tstate->_status.bound set to 1
// to indicate it was previously bound.
tstate->_status.unbound = 1;
}


static void
bind_gilstate_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate_is_alive(tstate));
assert(tstate_is_bound(tstate));
// XXX assert(!tstate->_status.active);
// XXX assert(!tstate->_status.bound_gilstate);
_PyRuntimeState *runtime = tstate->interp->runtime;

/* Stick the thread state for this thread in thread specific storage.
Expand All @@ -238,42 +296,24 @@ bind_tstate(PyThreadState *tstate)
if (gilstate_tss_get(runtime) == NULL) {
gilstate_tss_set(runtime, tstate);
}

tstate->thread_id = PyThread_get_thread_ident();
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif

tstate->_status.bound = 1;
tstate->_status.bound_gilstate = 1;
}

static void
unbind_tstate(PyThreadState *tstate)
unbind_gilstate_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
// XXX assert(tstate_is_alive(tstate));
assert(tstate_is_bound(tstate));
// XXX assert(tstate_is_alive(tstate) && tstate_is_bound(tstate));
// XXX assert(!tstate->_status.active);
assert(tstate->thread_id > 0);
#ifdef PY_HAVE_THREAD_NATIVE_ID
assert(tstate->native_thread_id > 0);
#endif
_PyRuntimeState *runtime = tstate->interp->runtime;
assert(tstate->_status.bound_gilstate);
// XXX assert(tstate == gilstate_tss_get(tstate->interp->runtime));

if (gilstate_tss_initialized(runtime) &&
tstate == gilstate_tss_get(runtime))
{
gilstate_tss_clear(runtime);
// XXX This check *should* always succeed.
if (tstate == gilstate_tss_get(tstate->interp->runtime)) {
gilstate_tss_clear(tstate->interp->runtime);
}

// 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.

// We leave tstate->_status.bound set to 1
// to indicate it was previously bound.
tstate->_status.unbound = 1;
tstate->_status.bound_gilstate = 0;
}


Expand Down Expand Up @@ -1560,6 +1600,11 @@ tstate_activate(PyThreadState *tstate)
assert(tstate != NULL);
assert(tstate_is_alive(tstate) && tstate_is_bound(tstate));
assert(!tstate->_status.active);

if (!tstate->_status.bound_gilstate) {
bind_gilstate_tstate(tstate);
}

tstate->_status.active = 1;
}

Expand All @@ -1570,7 +1615,11 @@ tstate_deactivate(PyThreadState *tstate)
assert(tstate_is_bound(tstate));
// XXX assert(tstate_is_alive(tstate) && tstate_is_bound(tstate));
assert(tstate->_status.active);

tstate->_status.active = 0;

// We do not unbind the gilstate tstate here.
// It will still be used in PyGILState_Ensure().
}


Expand Down Expand Up @@ -2091,6 +2140,7 @@ PyGILState_Ensure(void)
Py_FatalError("Couldn't create thread-state for new thread");
}
bind_tstate(tcur);
bind_gilstate_tstate(tcur);

/* This is our thread state! We'll need to delete it in the
matching call to PyGILState_Release(). */
Expand Down Expand Up @@ -2146,6 +2196,7 @@ PyGILState_Release(PyGILState_STATE oldstate)
if (tstate->gilstate_counter == 0) {
/* can't have been locked when we created it */
assert(oldstate == PyGILState_UNLOCKED);
// XXX Unbind tstate here.
PyThreadState_Clear(tstate);
/* Delete the thread-state. Note this releases the GIL too!
* It's vital that the GIL be held here, to avoid shutdown
Expand Down
0