Skip to content

Commit a5679f6

Browse files
author
dagangtj
committed
docs: Clarify that PyMutex is not re-entrant
Add explicit documentation that PyMutex is not re-entrant (recursive): - Add note to PyMutex type explaining it will deadlock on re-entry - Add warning to PyMutex_Lock about deadlock if already held - Clarify PyMutex_Unlock must be called by the locking thread Fixes #146536
1 parent 5992238 commit a5679f6

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Doc/c-api/synchronization.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ The C-API provides a basic mutual exclusion lock.
2424
considered unstable. The size may change in future Python releases
2525
without a deprecation period.
2626

27+
.. note::
28+
29+
A :c:type:`!PyMutex` is **not** re-entrant (recursive). If a thread
30+
that already holds the lock attempts to acquire it again, the thread
31+
will deadlock. Use :c:func:`Py_BEGIN_CRITICAL_SECTION` instead if
32+
re-entrancy is required.
33+
2734
.. versionadded:: 3.13
2835

2936
.. c:function:: void PyMutex_Lock(PyMutex *m)
@@ -32,12 +39,20 @@ The C-API provides a basic mutual exclusion lock.
3239
thread will block until the mutex is unlocked. While blocked, the thread
3340
will temporarily detach the :term:`thread state <attached thread state>` if one exists.
3441
42+
.. warning::
43+
44+
This function will deadlock if the calling thread already holds the lock.
45+
:c:type:`!PyMutex` is not re-entrant. For re-entrant locking, use
46+
:c:func:`Py_BEGIN_CRITICAL_SECTION` instead.
47+
3548
.. versionadded:: 3.13
3649
3750
.. c:function:: void PyMutex_Unlock(PyMutex *m)
3851
39-
Unlock mutex *m*. The mutex must be locked --- otherwise, the function will
40-
issue a fatal error.
52+
Unlock mutex *m*. The mutex must be locked by the calling thread ---
53+
otherwise, the function will issue a fatal error. Unlocking a mutex from
54+
a different thread than the one that locked it results in undefined
55+
behavior.
4156
4257
.. versionadded:: 3.13
4358

0 commit comments

Comments
 (0)