summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorKumar Aditya <kumaraditya@python.org>2024-10-14 08:36:31 (GMT)
committerGitHub <noreply@github.com>2024-10-14 08:36:31 (GMT)
commit67f6e08147bc005e460d82fcce85bf5d56009cf5 (patch)
tree57a73834eb7ab251b52dc4e4a48561bb5b6c3fb5 /Python
parent5217328f93f599755bd70418952392c54f705a71 (diff)
downloadcpython-67f6e08147bc005e460d82fcce85bf5d56009cf5.zip
cpython-67f6e08147bc005e460d82fcce85bf5d56009cf5.tar.gz
cpython-67f6e08147bc005e460d82fcce85bf5d56009cf5.tar.bz2
gh-125139: use `_PyRecursiveMutex` in `_thread.RLock` (#125144)
Diffstat (limited to 'Python')
-rw-r--r--Python/lock.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/Python/lock.c b/Python/lock.c
index 57675fe..554c51d 100644
--- a/Python/lock.c
+++ b/Python/lock.c
@@ -377,21 +377,46 @@ _PyRecursiveMutex_Lock(_PyRecursiveMutex *m)
assert(m->level == 0);
}
+PyLockStatus
+_PyRecursiveMutex_LockTimed(_PyRecursiveMutex *m, PyTime_t timeout, _PyLockFlags flags)
+{
+ PyThread_ident_t thread = PyThread_get_thread_ident_ex();
+ if (recursive_mutex_is_owned_by(m, thread)) {
+ m->level++;
+ return PY_LOCK_ACQUIRED;
+ }
+ PyLockStatus s = _PyMutex_LockTimed(&m->mutex, timeout, flags);
+ if (s == PY_LOCK_ACQUIRED) {
+ _Py_atomic_store_ullong_relaxed(&m->thread, thread);
+ assert(m->level == 0);
+ }
+ return s;
+}
+
void
_PyRecursiveMutex_Unlock(_PyRecursiveMutex *m)
{
+ if (_PyRecursiveMutex_TryUnlock(m) < 0) {
+ Py_FatalError("unlocking a recursive mutex that is not "
+ "owned by the current thread");
+ }
+}
+
+int
+_PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
+{
PyThread_ident_t thread = PyThread_get_thread_ident_ex();
if (!recursive_mutex_is_owned_by(m, thread)) {
- Py_FatalError("unlocking a recursive mutex that is not owned by the"
- " current thread");
+ return -1;
}
if (m->level > 0) {
m->level--;
- return;
+ return 0;
}
assert(m->level == 0);
_Py_atomic_store_ullong_relaxed(&m->thread, 0);
PyMutex_Unlock(&m->mutex);
+ return 0;
}
#define _Py_WRITE_LOCKED 1