blob: 57c39714347b70f04ef0e180159a2d5fc3100ac0 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`mutex` --- Mutual exclusion support
3=========================================
4
5.. module:: mutex
6 :synopsis: Lock and queue for mutual exclusion.
Brett Cannon9d441822008-05-08 19:26:08 +00007 :deprecated:
Georg Brandlc62ef8b2009-01-03 20:55:06 +00008
Georg Brandl6ae19ad2010-02-23 21:33:17 +00009.. deprecated:: 2.6
Ezio Melotti510ff542012-05-03 19:21:40 +030010 The :mod:`mutex` module has been removed in Python 3.
Brett Cannon9d441822008-05-08 19:26:08 +000011
Georg Brandl8ec7f652007-08-15 14:28:01 +000012.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
13
14
15The :mod:`mutex` module defines a class that allows mutual-exclusion via
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000016acquiring and releasing locks. It does not require (or imply)
17:mod:`threading` or multi-tasking, though it could be useful for those
18purposes.
Georg Brandl8ec7f652007-08-15 14:28:01 +000019
20The :mod:`mutex` module defines the following class:
21
22
23.. class:: mutex()
24
25 Create a new (unlocked) mutex.
26
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.
33
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.
36
37
38.. _mutex-objects:
39
40Mutex Objects
41-------------
42
43:class:`mutex` objects have following methods:
44
45
46.. method:: mutex.test()
47
48 Check whether the mutex is locked.
49
50
51.. method:: mutex.testandset()
52
53 "Atomic" test-and-set, grab the lock if it is not set, and return ``True``,
54 otherwise, return ``False``.
55
56
57.. method:: mutex.lock(function, argument)
58
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.
62
63
64.. method:: mutex.unlock()
65
66 Unlock the mutex if queue is empty, otherwise execute the first element in the
67 queue.
68