summaryrefslogtreecommitdiffstats
path: root/Objects/obmalloc.c
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2023-12-07 19:33:40 (GMT)
committerGitHub <noreply@github.com>2023-12-07 19:33:40 (GMT)
commitcf6110ba1337cb67e5867d86e7c0e8d923a5bc8d (patch)
treeab8393161d5ce01479bfb03dce874ab52246d28f /Objects/obmalloc.c
parentdb460735af7503984d1b7d878069722db44b11e8 (diff)
downloadcpython-cf6110ba1337cb67e5867d86e7c0e8d923a5bc8d.zip
cpython-cf6110ba1337cb67e5867d86e7c0e8d923a5bc8d.tar.gz
cpython-cf6110ba1337cb67e5867d86e7c0e8d923a5bc8d.tar.bz2
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.
Diffstat (limited to 'Objects/obmalloc.c')
-rw-r--r--Objects/obmalloc.c61
1 files changed, 16 insertions, 45 deletions
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index 2761c77..b737c03 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -329,13 +329,9 @@ int
_PyMem_SetDefaultAllocator(PyMemAllocatorDomain domain,
PyMemAllocatorEx *old_alloc)
{
- if (ALLOCATORS_MUTEX == NULL) {
- /* The runtime must be initializing. */
- return set_default_allocator_unlocked(domain, pydebug, old_alloc);
- }
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
int res = set_default_allocator_unlocked(domain, pydebug, old_alloc);
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
return res;
}
@@ -467,9 +463,9 @@ set_up_allocators_unlocked(PyMemAllocatorName allocator)
int
_PyMem_SetupAllocators(PyMemAllocatorName allocator)
{
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
int res = set_up_allocators_unlocked(allocator);
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
return res;
}
@@ -554,9 +550,9 @@ get_current_allocator_name_unlocked(void)
const char*
_PyMem_GetCurrentAllocatorName(void)
{
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
const char *name = get_current_allocator_name_unlocked();
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
return name;
}
@@ -653,14 +649,9 @@ set_up_debug_hooks_unlocked(void)
void
PyMem_SetupDebugHooks(void)
{
- if (ALLOCATORS_MUTEX == NULL) {
- /* The runtime must not be completely initialized yet. */
- set_up_debug_hooks_unlocked();
- return;
- }
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
set_up_debug_hooks_unlocked();
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
}
static void
@@ -696,53 +687,33 @@ set_allocator_unlocked(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
void
PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
{
- if (ALLOCATORS_MUTEX == NULL) {
- /* The runtime must not be completely initialized yet. */
- get_allocator_unlocked(domain, allocator);
- return;
- }
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
get_allocator_unlocked(domain, allocator);
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
}
void
PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
{
- if (ALLOCATORS_MUTEX == NULL) {
- /* The runtime must not be completely initialized yet. */
- set_allocator_unlocked(domain, allocator);
- return;
- }
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
set_allocator_unlocked(domain, allocator);
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
}
void
PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)
{
- if (ALLOCATORS_MUTEX == NULL) {
- /* The runtime must not be completely initialized yet. */
- *allocator = _PyObject_Arena;
- return;
- }
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
*allocator = _PyObject_Arena;
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
}
void
PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)
{
- if (ALLOCATORS_MUTEX == NULL) {
- /* The runtime must not be completely initialized yet. */
- _PyObject_Arena = *allocator;
- return;
- }
- PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
+ PyMutex_Lock(&ALLOCATORS_MUTEX);
_PyObject_Arena = *allocator;
- PyThread_release_lock(ALLOCATORS_MUTEX);
+ PyMutex_Unlock(&ALLOCATORS_MUTEX);
}