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:
Sam Gross 2023-12-07 14:33:40 -05:00 committed by GitHub
parent db460735af
commit cf6110ba13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 97 additions and 241 deletions

View file

@ -451,15 +451,9 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
e->hookCFunction = (Py_AuditHookFunction)hook;
e->userData = userData;
if (runtime->audit_hooks.mutex == NULL) {
/* The runtime must not be initialized yet. */
add_audit_hook_entry_unlocked(runtime, e);
}
else {
PyThread_acquire_lock(runtime->audit_hooks.mutex, WAIT_LOCK);
add_audit_hook_entry_unlocked(runtime, e);
PyThread_release_lock(runtime->audit_hooks.mutex);
}
PyMutex_Lock(&runtime->audit_hooks.mutex);
add_audit_hook_entry_unlocked(runtime, e);
PyMutex_Unlock(&runtime->audit_hooks.mutex);
return 0;
}