Skip to content

Commit ab32409

Browse files
committed
Initial commit
1 parent 28625d4 commit ab32409

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Lib/test/lock_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,19 @@ def test_locked(self):
370370
lock.release()
371371
self.assertFalse(lock.locked())
372372

373+
def test_locked_2threads(self):
374+
l = []
375+
rlock = self.locktype()
376+
def acquire():
377+
rlock.acquire()
378+
l.append(rlock.locked())
379+
380+
with Bunch(acquire, 1):
381+
pass
382+
self.assertTrue(rlock.locked())
383+
self.assertTrue(l[0])
384+
del rlock
385+
373386
def test_release_save_unacquired(self):
374387
# Cannot _release_save an unacquired lock
375388
lock = self.locktype()

Lib/threading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def __exit__(self, t, v, tb):
237237

238238
def locked(self):
239239
"""Return whether this object is locked."""
240-
return self._count > 0
240+
return self._block.locked()
241241

242242
# Internal methods used by condition variables
243243

Modules/_threadmodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,8 +1110,12 @@ Release the lock.");
11101110
static PyObject *
11111111
rlock_locked(PyObject *op, PyObject *Py_UNUSED(ignored))
11121112
{
1113-
rlockobject *self = rlockobject_CAST(op);
1114-
int is_locked = _PyRecursiveMutex_IsLockedByCurrentThread(&self->lock);
1113+
/*
1114+
see gh-134323: the `_PyRecursiveMutex_IsLocked` function does not exist, so we cast the `op`
1115+
to `lockobject` in order to call `PyMutex_IsLocked`.
1116+
*/
1117+
lockobject *self = lockobject_CAST(op);
1118+
int is_locked = PyMutex_IsLocked(&self->lock);
11151119
return PyBool_FromLong(is_locked);
11161120
}
11171121

0 commit comments

Comments
 (0)