Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
MoinMoin.util.lock.WriteLock Class Reference
Inheritance diagram for MoinMoin.util.lock.WriteLock:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.util.lock.WriteLock:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def acquire
def release
def isLocked
def exists
def isExpired
def expire

Public Attributes

 readlocktimeout
 dir
 timeout
 lockDir

Static Public Attributes

string fileName = 'write_lock'
 timerClass = Timer

Private Member Functions

def _expireReadLocks
def _haveReadLocks

Detailed Description

Exclusive Read/Write Lock

When a resource is locked with this lock, clients can't read
or write the resource.

This super-exclusive lock can't be acquired if there are any other
locks, either WriteLock or ReadLocks. When trying to acquire, this
lock will try to expire all existing ReadLocks.

Definition at line 187 of file lock.py.


Constructor & Destructor Documentation

def MoinMoin.util.lock.WriteLock.__init__ (   self,
  dir,
  timeout = None,
  readlocktimeout = None 
)
Init a write lock

@param dir: the lock directory. Every resource should have one
    lock directory, which may contain read or write locks.
@param timeout: while trying to acquire, the lock will expire
    other unreleased write locks older than timeout.
@param readlocktimeout: while trying to acquire, the lock will
    expire other read locks older than readlocktimeout.

Definition at line 199 of file lock.py.

00199 
00200     def __init__(self, dir, timeout=None, readlocktimeout=None):
00201         """ Init a write lock
00202 
00203         @param dir: the lock directory. Every resource should have one
00204             lock directory, which may contain read or write locks.
00205         @param timeout: while trying to acquire, the lock will expire
00206             other unreleased write locks older than timeout.
00207         @param readlocktimeout: while trying to acquire, the lock will
00208             expire other read locks older than readlocktimeout.
00209         """
00210         ExclusiveLock.__init__(self, dir, timeout)
00211         if readlocktimeout is None:
00212             self.readlocktimeout = timeout
00213         else:
00214             self.readlocktimeout = readlocktimeout


Member Function Documentation

Expire old read locks 

Definition at line 248 of file lock.py.

00248 
00249     def _expireReadLocks(self):
00250         """ Expire old read locks """
00251         readLockFileName = ReadLock.fileName
00252         for name in os.listdir(self.dir):
00253             if not name.startswith(readLockFileName):
00254                 continue
00255             LockDir = os.path.join(self.dir, name)
00256             ExclusiveLock(LockDir, self.readlocktimeout).expire()

Here is the call graph for this function:

Here is the caller graph for this function:

Return True if read locks exists; False otherwise 

Definition at line 257 of file lock.py.

00257 
00258     def _haveReadLocks(self):
00259         """ Return True if read locks exists; False otherwise """
00260         readLockFileName = ReadLock.fileName
00261         for name in os.listdir(self.dir):
00262             if name.startswith(readLockFileName):
00263                 return True
00264         return False

Here is the caller graph for this function:

def MoinMoin.util.lock.WriteLock.acquire (   self,
  timeout = None 
)
Acquire an exclusive write lock

Try to acquire an exclusive lock, then try to expire existing
read locks. If timeout has not passed, the lock is acquired.
Otherwise, the exclusive lock is released and the lock is not
acquired.

Return True if lock acquired, False otherwise.

Reimplemented from MoinMoin.util.lock.ExclusiveLock.

Reimplemented in MoinMoin.util.lock.LazyWriteLock.

Definition at line 215 of file lock.py.

00215 
00216     def acquire(self, timeout=None):
00217         """ Acquire an exclusive write lock
00218 
00219         Try to acquire an exclusive lock, then try to expire existing
00220         read locks. If timeout has not passed, the lock is acquired.
00221         Otherwise, the exclusive lock is released and the lock is not
00222         acquired.
00223 
00224         Return True if lock acquired, False otherwise.
00225         """
00226         if self._locked:
00227             raise RuntimeError("lock already locked")
00228         result = False
00229         timer = self.timerClass(timeout)
00230         timer.start()
00231         if ExclusiveLock.acquire(self, timeout):
00232             try:
00233                 while timer.haveTime():
00234                     self._expireReadLocks()
00235                     if not self._haveReadLocks():
00236                         result = timer.haveTime()
00237                         break
00238                     timer.sleep()
00239             finally:
00240                 if result:
00241                     logging.debug('acquired write lock: %s' % self.lockDir)
00242                     return True
00243                 else:
00244                     self.release()
00245         return False

Here is the call graph for this function:

def MoinMoin.util.lock.ExclusiveLock.exists (   self) [inherited]

Reimplemented in MoinMoin.util.lock.LazyWriteLock, and MoinMoin.util.lock.LazyReadLock.

Definition at line 137 of file lock.py.

00137 
00138     def exists(self):
00139         return os.path.exists(self.lockDir)

Here is the caller graph for this function:

def MoinMoin.util.lock.ExclusiveLock.expire (   self) [inherited]
Return True if the lock is expired or missing; False otherwise. 

Reimplemented in MoinMoin.util.lock.LazyWriteLock, and MoinMoin.util.lock.LazyReadLock.

Definition at line 158 of file lock.py.

00158 
00159     def expire(self):
00160         """ Return True if the lock is expired or missing; False otherwise. """
00161         if self.isExpired():
00162             self._removeLockDir()
00163             logging.debug("expired lock: %s" % self.lockDir)
00164             return True
00165         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.lock.ExclusiveLock.isExpired (   self) [inherited]
Return True if too old or missing; False otherwise

TODO: Since stat returns times using whole seconds, this is
quite broken. Maybe use OS specific calls like Carbon.File on
Mac OS X?

Reimplemented in MoinMoin.util.lock.LazyWriteLock, and MoinMoin.util.lock.LazyReadLock.

Definition at line 140 of file lock.py.

00140 
00141     def isExpired(self):
00142         """ Return True if too old or missing; False otherwise
00143 
00144         TODO: Since stat returns times using whole seconds, this is
00145         quite broken. Maybe use OS specific calls like Carbon.File on
00146         Mac OS X?
00147         """
00148         if self.timeout is None:
00149             return not self.exists()
00150         try:
00151             lock_age = time.time() - filesys.stat(self.lockDir).st_mtime
00152             return lock_age > self.timeout
00153         except OSError, err:
00154             if err.errno == errno.ENOENT:
00155                 # No such lock file, therefore "expired"
00156                 return True
00157             raise

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.lock.ExclusiveLock.isLocked (   self) [inherited]

Definition at line 134 of file lock.py.

00134 
00135     def isLocked(self):
00136         return self._locked

def MoinMoin.util.lock.ExclusiveLock.release (   self) [inherited]
Release the lock 

Reimplemented in MoinMoin.util.lock.LazyWriteLock, and MoinMoin.util.lock.LazyReadLock.

Definition at line 126 of file lock.py.

00126 
00127     def release(self):
00128         """ Release the lock """
00129         if not self._locked:
00130             raise RuntimeError('lock already released: %s' % self.lockDir)
00131         self._removeLockDir()
00132         self._locked = False
00133         logging.debug('released lock: %s' % self.lockDir)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 89 of file lock.py.

string MoinMoin.util.lock.WriteLock.fileName = 'write_lock' [static]

Reimplemented from MoinMoin.util.lock.ExclusiveLock.

Definition at line 197 of file lock.py.

Reimplemented in MoinMoin.util.lock.ReadLock.

Definition at line 94 of file lock.py.

Definition at line 211 of file lock.py.

Definition at line 92 of file lock.py.

Definition at line 77 of file lock.py.


The documentation for this class was generated from the following file: