summaryrefslogtreecommitdiffstats
path: root/Objects
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
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')
-rw-r--r--Objects/obmalloc.c61
-rw-r--r--Objects/unicodeobject.c4
2 files changed, 18 insertions, 47 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);
}
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 10022e2..836e14f 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1904,7 +1904,7 @@ _PyUnicode_FromId(_Py_Identifier *id)
if (index < 0) {
struct _Py_unicode_runtime_ids *rt_ids = &interp->runtime->unicode_state.ids;
- PyThread_acquire_lock(rt_ids->lock, WAIT_LOCK);
+ PyMutex_Lock(&rt_ids->mutex);
// Check again to detect concurrent access. Another thread can have
// initialized the index while this thread waited for the lock.
index = _Py_atomic_load_ssize(&id->index);
@@ -1914,7 +1914,7 @@ _PyUnicode_FromId(_Py_Identifier *id)
rt_ids->next_index++;
_Py_atomic_store_ssize(&id->index, index);
}
- PyThread_release_lock(rt_ids->lock);
+ PyMutex_Unlock(&rt_ids->mutex);
}
assert(index >= 0);