10000 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
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
cc354 8000 0d
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
8000
Prev Previous commit
Next Next commit
current_tss_*() -> gilstate_tss_*().
  • Loading branch information
ericsnowcurrently committed Jan 25, 2023
commit ca98d68b07d8738c587953284cde75a2df39c7c2
64 changes: 32 additions & 32 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,34 @@ tstate_tss_reinit(Py_tss_t *key)
The GIL does no need to be held for these.
*/

#define current_tss_initialized(runtime) \
#define gilstate_tss_initialized(runtime) \
tstate_tss_initialized(&(runtime)->autoTSSkey)
#define current_tss_init(runtime) \
#define gilstate_tss_init(runtime) \
tstate_tss_init(&(runtime)->autoTSSkey)
#define current_tss_fini(runtime) \
#define gilstate_tss_fini(runtime) \
tstate_tss_fini(&(runtime)->autoTSSkey)
#define current_tss_get(runtime) \
#define gilstate_tss_get(runtime) \
tstate_tss_get(&(runtime)->autoTSSkey)
#define _current_tss_set(runtime, tstate) \
#define _gilstate_tss_set(runtime, tstate) \
tstate_tss_set(&(runtime)->autoTSSkey, tstate)
#define _current_tss_clear(runtime) \
#define _gilstate_tss_clear(runtime) \
tstate_tss_clear(&(runtime)->autoTSSkey)
#define current_tss_reinit(runtime) \
#define gilstate_tss_reinit(runtime) \
tstate_tss_reinit(&(runtime)->autoTSSkey)

static inline void
current_tss_set(_PyRuntimeState *runtime, PyThreadState *tstate)
gilstate_tss_set(_PyRuntimeState *runtime, PyThreadState *tstate)
{
assert(tstate != NULL && tstate->interp->runtime == runtime);
if (_current_tss_set(runtime, tstate) != 0) {
if (_gilstate_tss_set(runtime, tstate) != 0) {
Py_FatalError("failed to set current tstate (TSS)");
}
}

static inline void
current_tss_clear(_PyRuntimeState *runtime)
gilstate_tss_clear(_PyRuntimeState *runtime)
{
if (_current_tss_clear(runtime) != 0) {
if (_gilstate_tss_clear(runtime) != 0) {
Py_FatalError("failed to clear current tstate (TSS)");
}
}
Expand Down Expand Up @@ -235,8 +235,8 @@ bind_tstate(PyThreadState *tstate)
(This is a better fix for SF bug #1010677 than the first one attempted.)
*/
// XXX Skipping like this does not play nice with multiple interpreters.
if (current_tss_get(runtime) == NULL) {
current_tss_set(runtime, tstate);
if (gilstate_tss_get(runtime) == NULL) {
gilstate_tss_set(runtime, tstate);
}

tstate->thread_id = PyThread_get_thread_ident();
Expand All @@ -260,10 +260,10 @@ unbind_tstate(PyThreadState *tstate)
#endif
_PyRuntimeState *runtime = tstate->interp->runtime;

if (current_tss_initialized(runtime) &&
tstate == current_tss_get(runtime))
if (gilstate_tss_initialized(runtime) &&
tstate == gilstate_tss_get(runtime))
{
current_tss_clear(runtime);
gilstate_tss_clear(runtime);
}

// We leave thread_id and native_thraed_id alone
Expand Down Expand Up @@ -297,7 +297,7 @@ holds_gil(PyThreadState *tstate)
assert(tstate != NULL);
_PyRuntimeState *runtime = tstate->interp->runtime;
/* Must be the tstate for this thread */
assert(tstate == current_tss_get(runtime));
assert(tstate == gilstate_tss_get(runtime));
return tstate == current_fast_get(runtime);
}

Expand Down Expand Up @@ -430,7 +430,7 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
memcpy(runtime, &initial, sizeof(*runtime));
}

if (current_tss_init(runtime) != 0) {
if (gilstate_tss_init(runtime) != 0) {
_PyRuntimeState_Fini(runtime);
return _PyStatus_NO_MEMORY();
}
Expand All @@ -449,8 +449,8 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
void
_PyRuntimeState_Fini(_PyRuntimeState *runtime)
{
if (current_tss_initialized(runtime)) {
current_tss_fini(runtime);
8000 if (gilstate_tss_initialized(runtime)) {
gilstate_tss_fini(runtime);
}

if (PyThread_tss_is_created(&runtime->trashTSSkey)) {
Expand Down Expand Up @@ -510,7 +510,7 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)

}

PyStatus status = current_tss_reinit(runtime);
PyStatus status = gilstate_tss_reinit(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand Down Expand Up @@ -1671,12 +1671,12 @@ _PyThreadState_Swap(_PyRuntimeState *runtime, PyThreadState *newts)
*/
// XXX The above isn't true when multiple interpreters are involved.
#if defined(Py_DEBUG)
if (newts && current_tss_initialized(runtime)) {
if (newts && gilstate_tss_initialized(runtime)) {
/* This can be called from PyEval_RestoreThread(). Similar
to it, we need to ensure errno doesn't change.
*/
int err = errno;
PyThreadState *check = current_tss_get(runtime);
PyThreadState *check = gilstate_tss_get(runtime);
if (check && check->interp == newts->interp && check != newts) {
Py_FatalError("Invalid thread state for this thread");
}
Expand Down Expand Up @@ -1985,7 +1985,7 @@ _PyGILState_Init(PyInterpreterState *interp)
return _PyStatus_OK();
}
_PyRuntimeState *runtime = interp->runtime;
assert(current_tss_get(runtime) == NULL);
assert(gilstate_tss_get(runtime) == NULL);
assert(runtime->gilstate.autoInterpreterState == NULL);
runtim 8000 e->gilstate.autoInterpreterState = interp;
return _PyStatus_OK();
Expand Down Expand Up @@ -2021,7 +2021,7 @@ _PyGILState_SetTstate(PyThreadState *tstate)
_PyRuntimeState *runtime = tstate->interp->runtime;

assert(runtime->gilstate.autoInterpreterState == tstate->interp);
assert(current_tss_get(runtime) == tstate);
assert(gilstate_tss_get(runtime) == tstate);
assert(tstate->gilstate_counter == 1);
#endif

Expand All @@ -2040,10 +2040,10 @@ PyThreadState *
PyGILState_GetThisThreadState(void)
{
_PyRuntimeState *runtime = &_PyRuntime;
if (!current_tss_initialized(runtime)) {
if (!gilstate_tss_initialized(runtime)) {
return NULL;
}
return current_tss_get(runtime);
return gilstate_tss_get(runtime);
}

int
Expand All @@ -2054,7 +2054,7 @@ PyGILState_Check(void)
return 1;
}

if (!current_tss_initialized(runtime)) {
if (!gilstate_tss_initialized(runtime)) {
return 1;
}

Expand All @@ -2063,7 +2063,7 @@ PyGILState_Check(void)
return 0;
}

return (tstate == current_tss_get(runtime));
return (tstate == gilstate_tss_get(runtime));
}

PyGILState_STATE
Expand All @@ -2079,10 +2079,10 @@ PyGILState_Ensure(void)
/* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
called by Py_Initialize() */
assert(_PyEval_ThreadsInitialized(runtime));
assert(current_tss_initialized(runtime));
assert(gilstate_tss_initialized(runtime));
assert(runtime->gilstate.autoInterpreterState != NULL);

PyThreadState *tcur = current_tss_get(runtime);
PyThreadState *tcur = gilstate_tss_get(runtime);
int has_gil;
if (tcur == NULL) {
/* Create a new Python thread state for this thread */
Expand Down Expand Up @@ -2120,7 +2120,7 @@ void
PyGILState_Release(PyGILState_STATE oldstate)
{
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = current_tss_get(runtime);
PyThreadState *tstate = gilstate_tss_get(runtime);
if (tstate == NULL) {
Py_FatalError("auto-releasing thread-state, "
"but no thread-state for this thread");
Expand Down
0