gh-134009: Expose `PyMutex_IsLocked` in the public C API (gh-134365) · python/cpython@f41e9c7 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit f41e9c7

Browse files
authored
gh-134009: Expose PyMutex_IsLocked in the public C API (gh-134365)
The `PyMutex_IsLocked()` function is useful in assertions for verifying that code maintains certain locking invariants.
1 parent 86c3316 commit f41e9c7

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

Doc/c-api/init.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,6 +2277,18 @@ The C-API provides a basic mutual exclusion lock.
22772277
22782278
.. versionadded:: 3.13
22792279
2280+
.. c:function:: int PyMutex_IsLocked(PyMutex *m)
2281+
2282+
Returns non-zero if the mutex *m* is currently locked, zero otherwise.
2283+
2284+
.. note::
2285+
2286+
This function is intended for use in assertions and debugging only and
2287+
should not be used to make concurrency control decisions, as the lock
2288+
state may change immediately after the check.
2289+
2290+
.. versionadded:: next
2291+
22802292
.. _python-critical-section-api:
22812293
22822294
Python Critical Section API

Doc/whatsnew/3.15.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ Porting to Python 3.15
346346

347347
(Contributed by Serhiy Storchaka in :gh:`133595`.)
348348

349+
* Private functions promoted to public C APIs:
350+
351+
* ``PyMutex_IsLocked()`` : :c:func:`PyMutex_IsLocked`
352+
353+
The |pythoncapi_compat_project| can be used to get most of these new
354+
functions on Python 3.14 and older.
355+
349356
Deprecated C APIs
350357
-----------------
351358

Include/cpython/lock.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ PyAPI_FUNC(void) PyMutex_Lock(PyMutex *m);
3636
// exported function for unlocking the mutex
3737
PyAPI_FUNC(void) PyMutex_Unlock(PyMutex *m);
3838

39+
// exported function for checking if the mutex is locked
40+
PyAPI_FUNC(int) PyMutex_IsLocked(PyMutex *m);
41+
3942
// Locks the mutex.
4043
//
4144
// If the mutex is currently locked, the calling thread will be parked until
@@ -61,3 +64,11 @@ _PyMutex_Unlock(PyMutex *m)
6164
}
6265
}
6366
#define PyMutex_Unlock _PyMutex_Unlock
67+
68+
// Checks if the mutex is currently locked.
69+
static inline int
70+
_PyMutex_IsLocked(PyMutex *m)
71+
{
72+
return (_Py_atomic_load_uint8(&m->_bits) & _Py_LOCKED) != 0;
73+
}
74+
#define PyMutex_IsLocked _PyMutex_IsLocked

Include/internal/pycore_lock.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ PyMutex_LockFast(PyMutex *m)
2525
return _Py_atomic_compare_exchange_uint8(lock_bits, &expected, _Py_LOCKED);
2626
}
2727

28-
// Checks if the mutex is currently locked.
29-
static inline int
30-
PyMutex_IsLocked(PyMutex *m)
31-
{
32-
return (_Py_atomic_load_uint8(&m->_bits) & _Py_LOCKED) != 0;
33-
}
34-
3528
// Re-initializes the mutex after a fork to the unlocked state.
3629
static inline void
3730
_PyMutex_at_fork_reinit(PyMutex *m)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expose :c:func:`PyMutex_IsLocked` as part of the public C API.

Python/lock.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,11 @@ PyMutex_Unlock(PyMutex *m)
634634
Py_FatalError("unlocking mutex that is not locked");
635635
}
636636
}
637+
638+
639+
#undef PyMutex_IsLocked
640+
int
641+
PyMutex_IsLocked(PyMutex *m)
642+
{
643+
return _PyMutex_IsLocked(m);
644+
}

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.