/Doc/library/mutex.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 68 lines · 39 code · 29 blank · 0 comment · 0 complexity · 322dd1b2bf0e44ea1a8b9d49012e76c2 MD5 · raw file

  1. :mod:`mutex` --- Mutual exclusion support
  2. =========================================
  3. .. module:: mutex
  4. :synopsis: Lock and queue for mutual exclusion.
  5. :deprecated:
  6. .. deprecated::
  7. The :mod:`mutex` module has been removed in Python 3.0.
  8. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  9. The :mod:`mutex` module defines a class that allows mutual-exclusion via
  10. acquiring and releasing locks. It does not require (or imply)
  11. :mod:`threading` or multi-tasking, though it could be useful for those
  12. purposes.
  13. The :mod:`mutex` module defines the following class:
  14. .. class:: mutex()
  15. Create a new (unlocked) mutex.
  16. A mutex has two pieces of state --- a "locked" bit and a queue. When the mutex
  17. is not locked, the queue is empty. Otherwise, the queue contains zero or more
  18. ``(function, argument)`` pairs representing functions (or methods) waiting to
  19. acquire the lock. When the mutex is unlocked while the queue is not empty, the
  20. first queue entry is removed and its ``function(argument)`` pair called,
  21. implying it now has the lock.
  22. Of course, no multi-threading is implied -- hence the funny interface for
  23. :meth:`lock`, where a function is called once the lock is acquired.
  24. .. _mutex-objects:
  25. Mutex Objects
  26. -------------
  27. :class:`mutex` objects have following methods:
  28. .. method:: mutex.test()
  29. Check whether the mutex is locked.
  30. .. method:: mutex.testandset()
  31. "Atomic" test-and-set, grab the lock if it is not set, and return ``True``,
  32. otherwise, return ``False``.
  33. .. method:: mutex.lock(function, argument)
  34. Execute ``function(argument)``, unless the mutex is locked. In the case it is
  35. locked, place the function and argument on the queue. See :meth:`unlock` for
  36. explanation of when ``function(argument)`` is executed in that case.
  37. .. method:: mutex.unlock()
  38. Unlock the mutex if queue is empty, otherwise execute the first element in the
  39. queue.