gh-59956: Allow the "Trashcan" Mechanism to Work Without a Thread State (gh-101209)

We've factored out a struct from the two PyThreadState fields. This accomplishes two things:

* make it clear that the trashcan-related code doesn't need any other parts of PyThreadState
* allows us to use the trashcan mechanism even when there isn't a "current" thread state

We still expect the caller to hold the GIL.

https://github.com/python/cpython/issues/59956
This commit is contained in:
Eric Snow 2023-01-23 08:30:20 -07:00 committed by GitHub
parent 984387f39a
commit 7b20a0f55a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 24 deletions

View file

@ -400,6 +400,11 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
return status;
}
if (PyThread_tss_create(&runtime->trashTSSkey) != 0) {
_PyRuntimeState_Fini(runtime);
return _PyStatus_NO_MEMORY();
}
init_runtime(runtime, open_code_hook, open_code_userdata, audit_hook_head,
unicode_next_index, lock1, lock2, lock3, lock4);
@ -413,6 +418,10 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
current_tss_fini(runtime);
}
if (PyThread_tss_is_created(&runtime->trashTSSkey)) {
PyThread_tss_delete(&runtime->trashTSSkey);
}
/* Force the allocator used by _PyRuntimeState_Init(). */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
@ -471,6 +480,13 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
return status;
}
if (PyThread_tss_is_created(&runtime->trashTSSkey)) {
PyThread_tss_delete(&runtime->trashTSSkey);
}
if (PyThread_tss_create(&runtime->trashTSSkey) != 0) {
return _PyStatus_NO_MEMORY();
}
return _PyStatus_OK();
}
#endif