Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-142585: Add PyMutex_STATIC_INIT macro
  • Loading branch information
vstinner committed Dec 11, 2025
commit 36f9811cd34d8ee2a3d491e6774759bed12d9ec6
6 changes: 5 additions & 1 deletion Include/cpython/pylock.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C" {
// represent the unlocked state.
//
// Typical initialization:
// PyMutex m = (PyMutex){0};
// PyMutex m = PyMutex_STATIC_INIT;
//
// Or initialize as global variables:
// static PyMutex m;
Expand All @@ -34,6 +34,10 @@ typedef struct PyMutex {
uint8_t _bits; // (private)
} PyMutex;

// Static initialization for a PyMutex. Typical usage:
// PyMutex mutex = PyMutex_STATIC_INIT;
#define PyMutex_STATIC_INIT (PyMutex){_Py_UNLOCKED}

// exported function for locking the mutex
PyAPI_FUNC(void) PyMutex_Lock(PyMutex *m);

Expand Down
4 changes: 2 additions & 2 deletions Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
return NULL;
}

self->mutex = (PyMutex){0};
self->mutex = PyMutex_STATIC_INIT;
self->bzs.opaque = NULL;
self->bzs.bzalloc = BZ2_Malloc;
self->bzs.bzfree = BZ2_Free;
Expand Down Expand Up @@ -633,7 +633,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
return NULL;
}

self->mutex = (PyMutex){0};
self->mutex = PyMutex_STATIC_INIT;
self->needs_input = 1;
self->bzs_avail_in_real = 0;
self->input_buffer = NULL;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PyCStgInfo_clone(StgInfo *dst_info, StgInfo *src_info)

memcpy(dst_info, src_info, sizeof(StgInfo));
#ifdef Py_GIL_DISABLED
dst_info->mutex = (PyMutex){0};
dst_info->mutex = PyMutex_STATIC_INIT;
#endif
dst_info->dict_final = 0;

Expand Down
4 changes: 2 additions & 2 deletions Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
self->alloc.free = PyLzma_Free;
self->lzs.allocator = &self->alloc;

self->mutex = (PyMutex){0};
self->mutex = PyMutex_STATIC_INIT;

self->flushed = 0;
switch (format) {
Expand Down Expand Up @@ -1230,7 +1230,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
self->lzs.allocator = &self->alloc;
self->lzs.next_in = NULL;

self->mutex = (PyMutex){0};
self->mutex = PyMutex_STATIC_INIT;

self->check = LZMA_CHECK_UNKNOWN;
self->needs_input = 1;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3503,7 +3503,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
self->psk_client_callback = NULL;
self->psk_server_callback = NULL;
#endif
self->tstate_mutex = (PyMutex){0};
self->tstate_mutex = PyMutex_STATIC_INIT;

/* Don't check host name by default */
if (proto_version == PY_SSL_VERSION_TLS_CLIENT) {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi/test_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pysleep(int ms)
static PyObject *
test_lock_basic(PyObject *self, PyObject *obj)
{
PyMutex m = (PyMutex){0};
PyMutex m = PyMutex_STATIC_INIT;

// uncontended lock and unlock
PyMutex_Lock(&m);
Expand Down
6 changes: 3 additions & 3 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ ThreadHandle_new(void)
self->os_handle = 0;
self->has_os_handle = 0;
self->thread_is_exiting = (PyEvent){0};
self->mutex = (PyMutex){_Py_UNLOCKED};
self->mutex = PyMutex_STATIC_INIT;
self->once = (_PyOnceFlag){0};
self->state = THREAD_HANDLE_NOT_STARTED;
self->refcount = 1;
Expand Down Expand Up @@ -320,7 +320,7 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state)
// it's safe to set this non-atomically.
handle->state = THREAD_HANDLE_DONE;
handle->once = (_PyOnceFlag){_Py_ONCE_INITIALIZED};
handle->mutex = (PyMutex){_Py_UNLOCKED};
handle->mutex = PyMutex_STATIC_INIT;
_PyEvent_Notify(&handle->thread_is_exiting);
llist_remove(node);
remove_from_shutdown_handles(handle);
Expand Down Expand Up @@ -995,7 +995,7 @@ lock_new_impl(PyTypeObject *type)
if (self == NULL) {
return NULL;
}
self->lock = (PyMutex){0};
self->lock = PyMutex_STATIC_INIT;
return (PyObject *)self;
}

Expand Down
2 changes: 1 addition & 1 deletion Modules/_zstd/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,

self->use_multithread = 0;
self->dict = NULL;
self->lock = (PyMutex){0};
self->lock = PyMutex_STATIC_INIT;

/* Compression context */
self->cctx = ZSTD_createCCtx();
Expand Down
2 changes: 1 addition & 1 deletion Modules/_zstd/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
self->unused_data = NULL;
self->eof = 0;
self->dict = NULL;
self->lock = (PyMutex){0};
self->lock = PyMutex_STATIC_INIT;

/* needs_input flag */
self->needs_input = 1;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_zstd/zstddict.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,
self->d_dict = NULL;
self->dict_buffer = NULL;
self->dict_id = 0;
self->lock = (PyMutex){0};
self->lock = PyMutex_STATIC_INIT;

/* ZSTD_CDict dict */
self->c_dicts = PyDict_New();
Expand Down
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ new_keys_object(uint8_t log2_size, bool unicode)
dk->dk_log2_index_bytes = log2_bytes;
dk->dk_kind = unicode ? DICT_KEYS_UNICODE : DICT_KEYS_GENERAL;
#ifdef Py_GIL_DISABLED
dk->dk_mutex = (PyMutex){0};
dk->dk_mutex = PyMutex_STATIC_INIT;
#endif
dk->dk_nentries = 0;
dk->dk_usable = usable;
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2632,7 +2632,7 @@ new_reference(PyObject *op)
#endif
#else
op->ob_flags = 0;
op->ob_mutex = (PyMutex){ 0 };
op->ob_mutex = PyMutex_STATIC_INIT;
#ifdef _Py_THREAD_SANITIZER
_Py_atomic_store_uintptr_relaxed(&op->ob_tid, _Py_ThreadId());
_Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, 0);
Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ init_interpreter(PyInterpreterState *interp,

llist_init(&interp->mem_free_queue.head);
llist_init(&interp->asyncio_tasks_head);
interp->asyncio_tasks_lock = (PyMutex){0};
interp->asyncio_tasks_lock = PyMutex_STATIC_INIT;
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ PyThread_allocate_lock(void)

PyMutex *lock = (PyMutex *)PyMem_RawMalloc(sizeof(PyMutex));
if (lock) {
*lock = (PyMutex){0};
*lock = PyMutex_STATIC_INIT;
}

return (PyThread_type_lock)lock;
Expand Down
Loading