mirror of
https://github.com/python/cpython.git
synced 2025-11-03 15:11:34 +00:00
gh-111924: Use PyMutex for Runtime-global Locks. (gh-112207)
This replaces some usages of PyThread_type_lock with PyMutex, which does not require memory allocation to initialize. This simplifies some of the runtime initialization and is also one step towards avoiding changing the default raw memory allocator during initialize/finalization, which can be non-thread-safe in some circumstances.
This commit is contained in:
parent
db460735af
commit
cf6110ba13
18 changed files with 97 additions and 241 deletions
|
|
@ -3056,13 +3056,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;
|
||||
}
|
||||
|
||||
|
|
@ -3072,18 +3072,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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue