Skip to content

Commit 36f9811

Browse files
committed
gh-142585: Add PyMutex_STATIC_INIT macro
1 parent ffc7172 commit 36f9811

14 files changed

Lines changed: 22 additions & 18 deletions

File tree

Include/cpython/pylock.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" {
1313
// represent the unlocked state.
1414
//
1515
// Typical initialization:
16-
// PyMutex m = (PyMutex){0};
16+
// PyMutex m = PyMutex_STATIC_INIT;
1717
//
1818
// Or initialize as global variables:
1919
// static PyMutex m;
@@ -34,6 +34,10 @@ typedef struct PyMutex {
3434
uint8_t _bits; // (private)
3535
} PyMutex;
3636

37+
// Static initialization for a PyMutex. Typical usage:
38+
// PyMutex mutex = PyMutex_STATIC_INIT;
39+
#define PyMutex_STATIC_INIT (PyMutex){_Py_UNLOCKED}
40+
3741
// exported function for locking the mutex
3842
PyAPI_FUNC(void) PyMutex_Lock(PyMutex *m);
3943

Modules/_bz2module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
348348
return NULL;
349349
}
350350

351-
self->mutex = (PyMutex){0};
351+
self->mutex = PyMutex_STATIC_INIT;
352352
self->bzs.opaque = NULL;
353353
self->bzs.bzalloc = BZ2_Malloc;
354354
self->bzs.bzfree = BZ2_Free;
@@ -633,7 +633,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
633633
return NULL;
634634
}
635635

636-
self->mutex = (PyMutex){0};
636+
self->mutex = PyMutex_STATIC_INIT;
637637
self->needs_input = 1;
638638
self->bzs_avail_in_real = 0;
639639
self->input_buffer = NULL;

Modules/_ctypes/stgdict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PyCStgInfo_clone(StgInfo *dst_info, StgInfo *src_info)
2929

3030
memcpy(dst_info, src_info, sizeof(StgInfo));
3131
#ifdef Py_GIL_DISABLED
32-
dst_info->mutex = (PyMutex){0};
32+
dst_info->mutex = PyMutex_STATIC_INIT;
3333
#endif
3434
dst_info->dict_final = 0;
3535

Modules/_lzmamodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
813813
self->alloc.free = PyLzma_Free;
814814
self->lzs.allocator = &self->alloc;
815815

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

818818
self->flushed = 0;
819819
switch (format) {
@@ -1230,7 +1230,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
12301230
self->lzs.allocator = &self->alloc;
12311231
self->lzs.next_in = NULL;
12321232

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

12351235
self->check = LZMA_CHECK_UNKNOWN;
12361236
self->needs_input = 1;

Modules/_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3503,7 +3503,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
35033503
self->psk_client_callback = NULL;
35043504
self->psk_server_callback = NULL;
35053505
#endif
3506-
self->tstate_mutex = (PyMutex){0};
3506+
self->tstate_mutex = PyMutex_STATIC_INIT;
35073507

35083508
/* Don't check host name by default */
35093509
if (proto_version == PY_SSL_VERSION_TLS_CLIENT) {

Modules/_testinternalcapi/test_lock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pysleep(int ms)
3232
static PyObject *
3333
test_lock_basic(PyObject *self, PyObject *obj)
3434
{
35-
PyMutex m = (PyMutex){0};
35+
PyMutex m = PyMutex_STATIC_INIT;
3636

3737
// uncontended lock and unlock
3838
PyMutex_Lock(&m);

Modules/_threadmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ ThreadHandle_new(void)
231231
self->os_handle = 0;
232232
self->has_os_handle = 0;
233233
self->thread_is_exiting = (PyEvent){0};
234-
self->mutex = (PyMutex){_Py_UNLOCKED};
234+
self->mutex = PyMutex_STATIC_INIT;
235235
self->once = (_PyOnceFlag){0};
236236
self->state = THREAD_HANDLE_NOT_STARTED;
237237
self->refcount = 1;
@@ -320,7 +320,7 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state)
320320
// it's safe to set this non-atomically.
321321
handle->state = THREAD_HANDLE_DONE;
322322
handle->once = (_PyOnceFlag){_Py_ONCE_INITIALIZED};
323-
handle->mutex = (PyMutex){_Py_UNLOCKED};
323+
handle->mutex = PyMutex_STATIC_INIT;
324324
_PyEvent_Notify(&handle->thread_is_exiting);
325325
llist_remove(node);
326326
remove_from_shutdown_handles(handle);
@@ -995,7 +995,7 @@ lock_new_impl(PyTypeObject *type)
995995
if (self == NULL) {
996996
return NULL;
997997
}
998-
self->lock = (PyMutex){0};
998+
self->lock = PyMutex_STATIC_INIT;
999999
return (PyObject *)self;
10001000
}
10011001

Modules/_zstd/compressor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
348348

349349
self->use_multithread = 0;
350350
self->dict = NULL;
351-
self->lock = (PyMutex){0};
351+
self->lock = PyMutex_STATIC_INIT;
352352

353353
/* Compression context */
354354
self->cctx = ZSTD_createCCtx();

Modules/_zstd/decompressor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
499499
self->unused_data = NULL;
500500
self->eof = 0;
501501
self->dict = NULL;
502-
self->lock = (PyMutex){0};
502+
self->lock = PyMutex_STATIC_INIT;
503503

504504
/* needs_input flag */
505505
self->needs_input = 1;

Modules/_zstd/zstddict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,
6262
self->d_dict = NULL;
6363
self->dict_buffer = NULL;
6464
self->dict_id = 0;
65-
self->lock = (PyMutex){0};
65+
self->lock = PyMutex_STATIC_INIT;
6666

6767
/* ZSTD_CDict dict */
6868
self->c_dicts = PyDict_New();

0 commit comments

Comments
 (0)