gh-111924: Use PyMutex for runtime global locks.

This replaces some usages of PyThread_type_lock with PyMutex, which
does not require memory allocation to initialize.
This commit is contained in:
Sam Gross 2023-11-16 16:41:21 -05:00
parent 974847be44
commit 628f6eb003
18 changed files with 91 additions and 251 deletions

View file

@ -3055,13 +3055,13 @@ wait_for_thread_shutdown(PyThreadState *tstate)
int Py_AtExit(void (*func)(void))
{
struct _atexit_runtime_state *state = &_PyRuntime.atexit;
PyThread_acquire_lock(state->mutex, WAIT_LOCK);
PyMutex_Lock(&state->mutex);
if (state->ncallbacks >= NEXITFUNCS) {
PyThread_release_lock(state->mutex);
PyMutex_Unlock(&state->mutex);
return -1;
}
state->callbacks[state->ncallbacks++] = func;
PyThread_release_lock(state->mutex);
PyMutex_Unlock(&state->mutex);
return 0;
}
@ -3071,18 +3071,18 @@ call_ll_exitfuncs(_PyRuntimeState *runtime)
atexit_callbackfunc exitfunc;
struct _atexit_runtime_state *state = &runtime->atexit;
PyThread_acquire_lock(state->mutex, WAIT_LOCK);
PyMutex_Lock(&state->mutex);
while (state->ncallbacks > 0) {
/* pop last function from the list */
state->ncallbacks--;
exitfunc = state->callbacks[state->ncallbacks];
state->callbacks[state->ncallbacks] = NULL;
PyThread_release_lock(state->mutex);
PyMutex_Unlock(&state->mutex);
exitfunc();
PyThread_acquire_lock(state->mutex, WAIT_LOCK);
PyMutex_Lock(&state->mutex);
}
PyThread_release_lock(state->mutex);
PyMutex_Unlock(&state->mutex);
fflush(stdout);
fflush(stderr);