2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
:mod:`mutex` --- Mutual exclusion support
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
.. module:: mutex
|
|
|
|
:synopsis: Lock and queue for mutual exclusion.
|
2008-05-08 16:26:08 -03:00
|
|
|
:deprecated:
|
2009-01-03 16:55:06 -04:00
|
|
|
|
2010-02-23 17:33:17 -04:00
|
|
|
.. deprecated:: 2.6
|
2012-05-03 13:21:40 -03:00
|
|
|
The :mod:`mutex` module has been removed in Python 3.
|
2008-05-08 16:26:08 -03:00
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
|
|
|
|
|
|
|
|
|
|
|
|
The :mod:`mutex` module defines a class that allows mutual-exclusion via
|
2007-09-04 05:16:15 -03:00
|
|
|
acquiring and releasing locks. It does not require (or imply)
|
|
|
|
:mod:`threading` or multi-tasking, though it could be useful for those
|
|
|
|
purposes.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
The :mod:`mutex` module defines the following class:
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: mutex()
|
|
|
|
|
|
|
|
Create a new (unlocked) mutex.
|
|
|
|
|
|
|
|
A mutex has two pieces of state --- a "locked" bit and a queue. When the mutex
|
|
|
|
is not locked, the queue is empty. Otherwise, the queue contains zero or more
|
|
|
|
``(function, argument)`` pairs representing functions (or methods) waiting to
|
|
|
|
acquire the lock. When the mutex is unlocked while the queue is not empty, the
|
|
|
|
first queue entry is removed and its ``function(argument)`` pair called,
|
|
|
|
implying it now has the lock.
|
|
|
|
|
|
|
|
Of course, no multi-threading is implied -- hence the funny interface for
|
|
|
|
:meth:`lock`, where a function is called once the lock is acquired.
|
|
|
|
|
|
|
|
|
|
|
|
.. _mutex-objects:
|
|
|
|
|
|
|
|
Mutex Objects
|
|
|
|
-------------
|
|
|
|
|
2016-12-02 17:13:42 -04:00
|
|
|
:class:`~mutex.mutex` objects have following methods:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: mutex.test()
|
|
|
|
|
|
|
|
Check whether the mutex is locked.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: mutex.testandset()
|
|
|
|
|
|
|
|
"Atomic" test-and-set, grab the lock if it is not set, and return ``True``,
|
|
|
|
otherwise, return ``False``.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: mutex.lock(function, argument)
|
|
|
|
|
|
|
|
Execute ``function(argument)``, unless the mutex is locked. In the case it is
|
|
|
|
locked, place the function and argument on the queue. See :meth:`unlock` for
|
|
|
|
explanation of when ``function(argument)`` is executed in that case.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: mutex.unlock()
|
|
|
|
|
|
|
|
Unlock the mutex if queue is empty, otherwise execute the first element in the
|
|
|
|
queue.
|
|
|
|
|