int Py_AtExit(void (*func)(void))
{
- if (_PyRuntime.atexit.ncallbacks >= NEXITFUNCS)
+ struct _atexit_runtime_state *state = &_PyRuntime.atexit;
+ PyThread_acquire_lock(state->mutex, WAIT_LOCK);
+ if (state->ncallbacks >= NEXITFUNCS) {
+ PyThread_release_lock(state->mutex);
return -1;
- _PyRuntime.atexit.callbacks[_PyRuntime.atexit.ncallbacks++] = func;
+ }
+ state->callbacks[state->ncallbacks++] = func;
+ PyThread_release_lock(state->mutex);
return 0;
}
static void
call_ll_exitfuncs(_PyRuntimeState *runtime)
{
+ atexit_callbackfunc exitfunc;
struct _atexit_runtime_state *state = &runtime->atexit;
+
+ PyThread_acquire_lock(state->mutex, WAIT_LOCK);
while (state->ncallbacks > 0) {
/* pop last function from the list */
state->ncallbacks--;
- atexit_callbackfunc exitfunc = state->callbacks[state->ncallbacks];
+ exitfunc = state->callbacks[state->ncallbacks];
state->callbacks[state->ncallbacks] = NULL;
+ PyThread_release_lock(state->mutex);
exitfunc();
+ PyThread_acquire_lock(state->mutex, WAIT_LOCK);
}
+ PyThread_release_lock(state->mutex);
fflush(stdout);
fflush(stderr);
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime);
_Py_COMP_DIAG_POP
-#define NUMLOCKS 5
+#define NUMLOCKS 6
+#define LOCKS_INIT(runtime) \
+ { \
+ &(runtime)->interpreters.mutex, \
+ &(runtime)->xidregistry.mutex, \
+ &(runtime)->getargs.mutex, \
+ &(runtime)->unicode_state.ids.lock, \
+ &(runtime)->imports.extensions.mutex, \
+ &(runtime)->atexit.mutex, \
+ }
static int
alloc_for_runtime(PyThread_type_lock locks[NUMLOCKS])
PyPreConfig_InitPythonConfig(&runtime->preconfig);
- PyThread_type_lock *lockptrs[NUMLOCKS] = {
- &runtime->interpreters.mutex,
- &runtime->xidregistry.mutex,
- &runtime->getargs.mutex,
- &runtime->unicode_state.ids.lock,
- &runtime->imports.extensions.mutex,
- };
+ PyThread_type_lock *lockptrs[NUMLOCKS] = LOCKS_INIT(runtime);
for (int i = 0; i < NUMLOCKS; i++) {
assert(locks[i] != NULL);
*lockptrs[i] = locks[i];
LOCK = NULL; \
}
- PyThread_type_lock *lockptrs[NUMLOCKS] = {
- &runtime->interpreters.mutex,
- &runtime->xidregistry.mutex,
- &runtime->getargs.mutex,
- &runtime->unicode_state.ids.lock,
- &runtime->imports.extensions.mutex,
- };
+ PyThread_type_lock *lockptrs[NUMLOCKS] = LOCKS_INIT(runtime);
for (int i = 0; i < NUMLOCKS; i++) {
FREE_LOCK(*lockptrs[i]);
}
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
- PyThread_type_lock *lockptrs[NUMLOCKS] = {
- &runtime->interpreters.mutex,
- &runtime->xidregistry.mutex,
- &runtime->getargs.mutex,
- &runtime->unicode_state.ids.lock,
- &runtime->imports.extensions.mutex,
- };
+ PyThread_type_lock *lockptrs[NUMLOCKS] = LOCKS_INIT(runtime);
int reinit_err = 0;
for (int i = 0; i < NUMLOCKS; i++) {
reinit_err += _PyThread_at_fork_reinit(lockptrs[i]);