mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
gh-99113: Make Sure the GIL is Acquired at the Right Places (gh-104208)
This is a pre-requisite for a per-interpreter GIL. Without it this change isn't strictly necessary. However, there is no real downside otherwise.
This commit is contained in:
parent
fff193bbfe
commit
92d8bfffbf
4 changed files with 113 additions and 40 deletions
|
@ -1863,17 +1863,11 @@ PyThreadState_Get(void)
|
|||
}
|
||||
|
||||
|
||||
PyThreadState *
|
||||
_PyThreadState_Swap(_PyRuntimeState *runtime, PyThreadState *newts)
|
||||
static void
|
||||
_swap_thread_states(_PyRuntimeState *runtime,
|
||||
PyThreadState *oldts, PyThreadState *newts)
|
||||
{
|
||||
#if defined(Py_DEBUG)
|
||||
/* This can be called from PyEval_RestoreThread(). Similar
|
||||
to it, we need to ensure errno doesn't change.
|
||||
*/
|
||||
int err = errno;
|
||||
#endif
|
||||
PyThreadState *oldts = current_fast_get(runtime);
|
||||
|
||||
// XXX Do this only if oldts != NULL?
|
||||
current_fast_clear(runtime);
|
||||
|
||||
if (oldts != NULL) {
|
||||
|
@ -1887,6 +1881,20 @@ _PyThreadState_Swap(_PyRuntimeState *runtime, PyThreadState *newts)
|
|||
current_fast_set(runtime, newts);
|
||||
tstate_activate(newts);
|
||||
}
|
||||
}
|
||||
|
||||
PyThreadState *
|
||||
_PyThreadState_SwapNoGIL(PyThreadState *newts)
|
||||
{
|
||||
#if defined(Py_DEBUG)
|
||||
/* This can be called from PyEval_RestoreThread(). Similar
|
||||
to it, we need to ensure errno doesn't change.
|
||||
*/
|
||||
int err = errno;
|
||||
#endif
|
||||
|
||||
PyThreadState *oldts = current_fast_get(&_PyRuntime);
|
||||
_swap_thread_states(&_PyRuntime, oldts, newts);
|
||||
|
||||
#if defined(Py_DEBUG)
|
||||
errno = err;
|
||||
|
@ -1894,6 +1902,20 @@ _PyThreadState_Swap(_PyRuntimeState *runtime, PyThreadState *newts)
|
|||
return oldts;
|
||||
}
|
||||
|
||||
PyThreadState *
|
||||
_PyThreadState_Swap(_PyRuntimeState *runtime, PyThreadState *newts)
|
||||
{
|
||||
PyThreadState *oldts = current_fast_get(runtime);
|
||||
if (oldts != NULL) {
|
||||
_PyEval_ReleaseLock(oldts);
|
||||
}
|
||||
_swap_thread_states(runtime, oldts, newts);
|
||||
if (newts != NULL) {
|
||||
_PyEval_AcquireLock(newts);
|
||||
}
|
||||
return oldts;
|
||||
}
|
||||
|
||||
PyThreadState *
|
||||
PyThreadState_Swap(PyThreadState *newts)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue