[3.14] gh-134009: Expose `PyMutex_IsLocked` in the public C API (gh-1… · python/cpython@8e43b13 · GitHub
Skip to content

Commit 8e43b13

Browse files
hugovkcolesbury
andauthored
[3.14] gh-134009: Expose PyMutex_IsLocked in the public C API (gh-134365) (#136971)
Co-authored-by: Sam Gross <colesbury@gmail.com>
1 parent 11f5101 commit 8e43b13

6 files changed

Lines changed: 33 additions & 7 deletions

File tree

Doc/c-api/init.rst

Lines changed: 12 additions & 0 deletions

Doc/whatsnew/3.14.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,6 +3027,7 @@ Porting to Python 3.14
30273027
* ``_PyLong_IsPositive()``: :c:func:`PyLong_IsPositive`
30283028
* ``_PyLong_IsZero()``: :c:func:`PyLong_IsZero`
30293029
* ``_PyLong_Sign()``: :c:func:`PyLong_GetSign`
3030+
* ``_PyMutex_IsLocked()`` : :c:func:`PyMutex_IsLocked`
30303031
* ``_PyUnicodeWriter_Dealloc()``: :c:func:`PyUnicodeWriter_Discard`
30313032
* ``_PyUnicodeWriter_Finish()``: :c:func:`PyUnicodeWriter_Finish`
30323033
* ``_PyUnicodeWriter_Init()``: use :c:func:`PyUnicodeWriter_Create`

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

0 commit comments

Comments
 (0)