Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: mutex.py
"""Mutual exclusion -- for use with module sched
[0] Fix | Delete
[1] Fix | Delete
A mutex has two pieces of state -- a 'locked' bit and a queue.
[2] Fix | Delete
When the mutex is not locked, the queue is empty.
[3] Fix | Delete
Otherwise, the queue contains 0 or more (function, argument) pairs
[4] Fix | Delete
representing functions (or methods) waiting to acquire the lock.
[5] Fix | Delete
When the mutex is unlocked while the queue is not empty,
[6] Fix | Delete
the first queue entry is removed and its function(argument) pair called,
[7] Fix | Delete
implying it now has the lock.
[8] Fix | Delete
[9] Fix | Delete
Of course, no multi-threading is implied -- hence the funny interface
[10] Fix | Delete
for lock, where a function is called once the lock is acquired.
[11] Fix | Delete
"""
[12] Fix | Delete
from warnings import warnpy3k
[13] Fix | Delete
warnpy3k("the mutex module has been removed in Python 3.0", stacklevel=2)
[14] Fix | Delete
del warnpy3k
[15] Fix | Delete
[16] Fix | Delete
from collections import deque
[17] Fix | Delete
[18] Fix | Delete
class mutex:
[19] Fix | Delete
def __init__(self):
[20] Fix | Delete
"""Create a new mutex -- initially unlocked."""
[21] Fix | Delete
self.locked = False
[22] Fix | Delete
self.queue = deque()
[23] Fix | Delete
[24] Fix | Delete
def test(self):
[25] Fix | Delete
"""Test the locked bit of the mutex."""
[26] Fix | Delete
return self.locked
[27] Fix | Delete
[28] Fix | Delete
def testandset(self):
[29] Fix | Delete
"""Atomic test-and-set -- grab the lock if it is not set,
[30] Fix | Delete
return True if it succeeded."""
[31] Fix | Delete
if not self.locked:
[32] Fix | Delete
self.locked = True
[33] Fix | Delete
return True
[34] Fix | Delete
else:
[35] Fix | Delete
return False
[36] Fix | Delete
[37] Fix | Delete
def lock(self, function, argument):
[38] Fix | Delete
"""Lock a mutex, call the function with supplied argument
[39] Fix | Delete
when it is acquired. If the mutex is already locked, place
[40] Fix | Delete
function and argument in the queue."""
[41] Fix | Delete
if self.testandset():
[42] Fix | Delete
function(argument)
[43] Fix | Delete
else:
[44] Fix | Delete
self.queue.append((function, argument))
[45] Fix | Delete
[46] Fix | Delete
def unlock(self):
[47] Fix | Delete
"""Unlock a mutex. If the queue is not empty, call the next
[48] Fix | Delete
function with its argument."""
[49] Fix | Delete
if self.queue:
[50] Fix | Delete
function, argument = self.queue.popleft()
[51] Fix | Delete
function(argument)
[52] Fix | Delete
else:
[53] Fix | Delete
self.locked = False
[54] Fix | Delete
[55] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function