Staging
v0.5.1
https://github.com/python/cpython
Revision 0771591fe1068c9b437bb92d3ac372d7c09b2ff3 authored by Georg Brandl on 02 August 2010, 21:45:43 UTC, committed by Georg Brandl on 02 August 2010, 21:45:43 UTC
svn+ssh://pythondev@svn.python.org/python/branches/release27-maint

................
  r83593 | georg.brandl | 2010-08-02 23:44:25 +0200 (Mo, 02 Aug 2010) | 57 lines

  Merged revisions 83536,83546-83548,83550,83554-83555,83558,83563,83565,83571,83574-83575 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/branches/py3k

  ........
    r83536 | georg.brandl | 2010-08-02 19:49:25 +0200 (Mo, 02 Aug 2010) | 1 line

    #8578: mention danger of not incref'ing weak referenced object.
  ........
    r83546 | georg.brandl | 2010-08-02 21:16:34 +0200 (Mo, 02 Aug 2010) | 1 line

    #7973: Fix distutils options spelling.
  ........
    r83547 | georg.brandl | 2010-08-02 21:19:26 +0200 (Mo, 02 Aug 2010) | 1 line

    #7386: add example that shows that trailing path separators are stripped.
  ........
    r83548 | georg.brandl | 2010-08-02 21:23:34 +0200 (Mo, 02 Aug 2010) | 1 line

    #8172: how does one use a property?
  ........
    r83550 | georg.brandl | 2010-08-02 21:32:43 +0200 (Mo, 02 Aug 2010) | 1 line

    #9451: strengthen warning about __*__ special name usage.
  ........
    r83554 | georg.brandl | 2010-08-02 21:43:05 +0200 (Mo, 02 Aug 2010) | 1 line

    #7280: note about nasmw.exe.
  ........
    r83555 | georg.brandl | 2010-08-02 21:44:48 +0200 (Mo, 02 Aug 2010) | 1 line

    #8861: remove unused variable.
  ........
    r83558 | georg.brandl | 2010-08-02 22:05:19 +0200 (Mo, 02 Aug 2010) | 1 line

    #8648: document UTF-7 codec functions.
  ........
    r83563 | georg.brandl | 2010-08-02 22:21:21 +0200 (Mo, 02 Aug 2010) | 1 line

    #9037: add example how to raise custom exceptions from C code.
  ........
    r83565 | georg.brandl | 2010-08-02 22:27:20 +0200 (Mo, 02 Aug 2010) | 1 line

    #9111: document that do_help() looks at docstrings.
  ........
    r83571 | georg.brandl | 2010-08-02 22:44:34 +0200 (Mo, 02 Aug 2010) | 1 line

    Clarify that abs() is not a namespace.
  ........
    r83574 | georg.brandl | 2010-08-02 22:47:56 +0200 (Mo, 02 Aug 2010) | 1 line

    #6867: epoll.register() returns None.
  ........
    r83575 | georg.brandl | 2010-08-02 22:52:10 +0200 (Mo, 02 Aug 2010) | 1 line

    #9238: zipfile does handle archive comments.
  ........
................
1 parent f287d64
Raw File
Tip revision: 0771591fe1068c9b437bb92d3ac372d7c09b2ff3 authored by Georg Brandl on 02 August 2010, 21:45:43 UTC
Merged revisions 83593 via svnmerge from
Tip revision: 0771591
mutex.py
"""Mutual exclusion -- for use with module sched

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 0 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 lock, where a function is called once the lock is aquired.
"""
from warnings import warnpy3k
warnpy3k("the mutex module has been removed in Python 3.0", stacklevel=2)
del warnpy3k

from collections import deque

class mutex:
    def __init__(self):
        """Create a new mutex -- initially unlocked."""
        self.locked = 0
        self.queue = deque()

    def test(self):
        """Test the locked bit of the mutex."""
        return self.locked

    def testandset(self):
        """Atomic test-and-set -- grab the lock if it is not set,
        return True if it succeeded."""
        if not self.locked:
            self.locked = 1
            return True
        else:
            return False

    def lock(self, function, argument):
        """Lock a mutex, call the function with supplied argument
        when it is acquired.  If the mutex is already locked, place
        function and argument in the queue."""
        if self.testandset():
            function(argument)
        else:
            self.queue.append((function, argument))

    def unlock(self):
        """Unlock a mutex.  If the queue is not empty, call the next
        function with its argument."""
        if self.queue:
            function, argument = self.queue.popleft()
            function(argument)
        else:
            self.locked = 0
back to top