Issue #5262: Improved fix.
[python.git] / Doc / library / mutex.rst
blob53656c3854219f6901b81d163d06b8ed1b073e57
2 :mod:`mutex` --- Mutual exclusion support
3 =========================================
5 .. module:: mutex
6    :synopsis: Lock and queue for mutual exclusion.
7    :deprecated:
9 .. deprecated::
10    The :mod:`mutex` module has been removed in Python 3.0.
12 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
15 The :mod:`mutex` module defines a class that allows mutual-exclusion via
16 acquiring and releasing locks. It does not require (or imply)
17 :mod:`threading` or multi-tasking, though it could be useful for those
18 purposes.
20 The :mod:`mutex` module defines the following class:
23 .. class:: mutex()
25    Create a new (unlocked) mutex.
27    A mutex has two pieces of state --- a "locked" bit and a queue. When the mutex
28    is not locked, the queue is empty. Otherwise, the queue contains zero or more
29    ``(function, argument)`` pairs representing functions (or methods) waiting to
30    acquire the lock. When the mutex is unlocked while the queue is not empty, the
31    first queue entry is removed and its  ``function(argument)`` pair called,
32    implying it now has the lock.
34    Of course, no multi-threading is implied -- hence the funny interface for
35    :meth:`lock`, where a function is called once the lock is acquired.
38 .. _mutex-objects:
40 Mutex Objects
41 -------------
43 :class:`mutex` objects have following methods:
46 .. method:: mutex.test()
48    Check whether the mutex is locked.
51 .. method:: mutex.testandset()
53    "Atomic" test-and-set, grab the lock if it is not set, and return ``True``,
54    otherwise, return ``False``.
57 .. method:: mutex.lock(function, argument)
59    Execute ``function(argument)``, unless the mutex is locked. In the case it is
60    locked, place the function and argument on the queue. See :meth:`unlock` for
61    explanation of when ``function(argument)`` is executed in that case.
64 .. method:: mutex.unlock()
66    Unlock the mutex if queue is empty, otherwise execute the first element in the
67    queue.