Skip to content

gh-115942: Add locked to several multiprocessing locks #115944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,13 @@ object -- see :ref:`multiprocessing-managers`.
when invoked on an unlocked lock, a :exc:`ValueError` is raised.


.. method:: locked()

Return a boolean indicating whether this object is locked right now.

.. versionadded:: next


.. class:: RLock()

A recursive lock object: a close analog of :class:`threading.RLock`. A
Expand Down Expand Up @@ -1481,6 +1488,13 @@ object -- see :ref:`multiprocessing-managers`.
differs from the implemented behavior in :meth:`threading.RLock.release`.


.. method:: locked()

Return a boolean indicating whether this object is locked right now.

.. versionadded:: next


.. class:: Semaphore([value])

A semaphore object: a close analog of :class:`threading.Semaphore`.
Expand Down
13 changes: 13 additions & 0 deletions Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,13 @@ call release as many times the lock has been acquired can lead to deadlock.
There is no return value.


.. method:: locked()

Return a boolean indicating whether this object is locked right now.

.. versionadded:: next


.. _condition-objects:

Condition Objects
Expand Down Expand Up @@ -801,6 +808,12 @@ item to the buffer only needs to wake up one consumer thread.
Release the underlying lock. This method calls the corresponding method on
the underlying lock; there is no return value.

.. method:: locked()

Return a boolean indicating whether this object is locked right now.

.. versionadded:: next

.. method:: wait(timeout=None)

Wait until notified or until a timeout occurs. If the calling thread has
Expand Down
3 changes: 3 additions & 0 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ def release(self):
self.waiters.pop()
self.wakeup.release()

def locked(self):
return bool(self.count)

def __repr__(self):
return f'_ModuleLock({self.name!r}) at {id(self)}'

Expand Down
6 changes: 4 additions & 2 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,20 +1059,22 @@ def close(self, *args):


class AcquirerProxy(BaseProxy):
_exposed_ = ('acquire', 'release')
_exposed_ = ('acquire', 'release', 'locked')
def acquire(self, blocking=True, timeout=None):
args = (blocking,) if timeout is None else (blocking, timeout)
return self._callmethod('acquire', args)
def release(self):
return self._callmethod('release')
def locked(self):
return self._callmethod('locked')
def __enter__(self):
return self._callmethod('acquire')
def __exit__(self, exc_type, exc_val, exc_tb):
return self._callmethod('release')


class ConditionProxy(AcquirerProxy):
_exposed_ = ('acquire', 'release', 'wait', 'notify', 'notify_all')
_exposed_ = ('acquire', 'release', 'locked', 'wait', 'notify', 'notify_all')
def wait(self, timeout=None):
return self._callmethod('wait', (timeout,))
def notify(self, n=1):
Expand Down
3 changes: 3 additions & 0 deletions Lib/multiprocessing/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def _make_methods(self):
self.acquire = self._semlock.acquire
self.release = self._semlock.release

def locked(self):
return self._semlock._count() != 0

def __enter__(self):
return self._semlock.__enter__()

Expand Down
17 changes: 14 additions & 3 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,8 +1486,10 @@ def test_repr_lock(self):
def test_lock(self):
lock = self.Lock()
self.assertEqual(lock.acquire(), True)
self.assertTrue(lock.locked())
self.assertEqual(lock.acquire(False), False)
self.assertEqual(lock.release(), None)
self.assertFalse(lock.locked())
self.assertRaises((ValueError, threading.ThreadError), lock.release)

@staticmethod
Expand Down Expand Up @@ -1549,16 +1551,23 @@ def test_repr_rlock(self):
def test_rlock(self):
lock = self.RLock()
self.assertEqual(lock.acquire(), True)
self.assertTrue(lock.locked())
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.release(), None)
self.assertTrue(lock.locked())
self.assertEqual(lock.release(), None)
self.assertEqual(lock.release(), None)
self.assertFalse(lock.locked())
self.assertRaises((AssertionError, RuntimeError), lock.release)

def test_lock_context(self):
with self.Lock():
pass
with self.Lock() as locked:
self.assertTrue(locked)

def test_rlock_context(self):
with self.RLock() as locked:
self.assertTrue(locked)


class _TestSemaphore(BaseTestCase):
Expand Down Expand Up @@ -6254,6 +6263,7 @@ def test_event(self):
@classmethod
def _test_lock(cls, obj):
obj.acquire()
obj.locked()

def test_lock(self, lname="Lock"):
o = getattr(self.manager, lname)()
Expand All @@ -6265,8 +6275,9 @@ def test_lock(self, lname="Lock"):
def _test_rlock(cls, obj):
obj.acquire()
obj.release()
obj.locked()

def test_rlock(self, lname="Lock"):
def test_rlock(self, lname="RLock"):
o = getattr(self.manager, lname)()
self.run_worker(self._test_rlock, o)

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/lock_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ def test_release_unacquired(self):
lock.release()
self.assertRaises(RuntimeError, lock.release)

def test_locked(self):
lock = self.locktype()
self.assertFalse(lock.locked())
lock.acquire()
self.assertTrue(lock.locked())
lock.acquire()
self.assertTrue(lock.locked())
lock.release()
self.assertTrue(lock.locked())
lock.release()
self.assertFalse(lock.locked())

def test_release_save_unacquired(self):
# Cannot _release_save an unacquired lock
lock = self.locktype()
Expand Down
7 changes: 6 additions & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ def release(self):
def __exit__(self, t, v, tb):
self.release()

def locked(self):
"""Return whether this object is locked."""
return self._count > 0

# Internal methods used by condition variables

def _acquire_restore(self, state):
Expand Down Expand Up @@ -286,9 +290,10 @@ def __init__(self, lock=None):
if lock is None:
lock = RLock()
self._lock = lock
# Export the lock's acquire() and release() methods
# Export the lock's acquire(), release(), and locked() methods
self.acquire = lock.acquire
self.release = lock.release
self.locked = lock.locked
# If the lock defines _release_save() and/or _acquire_restore(),
# these override the default implementations (which just call
# release() and acquire() on the lock). Ditto for _is_owned().
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add :meth:`threading.RLock.locked`,
:meth:`multiprocessing.Lock.locked`,
:meth:`multiprocessing.RLock.locked`,
and allow :meth:`multiprocessing.managers.SyncManager.Lock` and
:meth:`multiprocessing.managers.SyncManager.RLock` to proxy ``locked()`` call.
15 changes: 15 additions & 0 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,19 @@ PyDoc_STRVAR(rlock_exit_doc,
\n\
Release the lock.");

static PyObject *
rlock_locked(PyObject *op, PyObject *Py_UNUSED(ignored))
{
rlockobject *self = rlockobject_CAST(op);
int is_locked = _PyRecursiveMutex_IsLockedByCurrentThread(&self->lock);
return PyBool_FromLong(is_locked);
}

PyDoc_STRVAR(rlock_locked_doc,
"locked()\n\
\n\
Return a boolean indicating whether this object is locked right now.");

static PyObject *
rlock_acquire_restore(PyObject *op, PyObject *args)
{
Expand Down Expand Up @@ -1204,6 +1217,8 @@ static PyMethodDef rlock_methods[] = {
METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc},
{"release", rlock_release,
METH_NOARGS, rlock_release_doc},
{"locked", rlock_locked,
METH_NOARGS, rlock_locked_doc},
{"_is_owned", rlock_is_owned,
METH_NOARGS, rlock_is_owned_doc},
{"_acquire_restore", rlock_acquire_restore,
Expand Down
Loading