Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
MoinMoin.util.lock.ExclusiveLock Class Reference
Inheritance diagram for MoinMoin.util.lock.ExclusiveLock:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.util.lock.ExclusiveLock:
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

 dir
 timeout
 lockDir

Static Public Attributes

string fileName = ''
 timerClass = Timer

Private Member Functions

def _makeDir
def _removeLockDir

Private Attributes

 _locked

Detailed Description

Exclusive lock

Uses a directory as portable lock method. On all platforms,
creating a directory will fail if the directory exists.

Only one exclusive lock per resource is allowed. This lock is not
used directly by clients, but used by both ReadLock and WriteLock.

If created with a timeout, the lock will expire timeout seconds
after it has been acquired. Without a timeout, it will never expire.

Definition at line 64 of file lock.py.


Constructor & Destructor Documentation

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

@param dir: the lock directory. Since this lock uses a empty
    filename, the dir is the lockDir.
@param timeout: while trying to acquire, the lock will expire
    other exclusive locks older than timeout.
    WARNING: because of file system timing limitations, timeouts
    must be at least 2 seconds.

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

Definition at line 79 of file lock.py.

00079 
00080     def __init__(self, dir, timeout=None):
00081         """ Init a write lock
00082 
00083         @param dir: the lock directory. Since this lock uses a empty
00084             filename, the dir is the lockDir.
00085         @param timeout: while trying to acquire, the lock will expire
00086             other exclusive locks older than timeout.
00087             WARNING: because of file system timing limitations, timeouts
00088             must be at least 2 seconds.
00089         """
00090         self.dir = dir
00091         if timeout is not None and timeout < 2.0:
00092             raise ValueError('timeout must be at least 2 seconds')
00093         self.timeout = timeout
00094         if self.fileName:
00095             self.lockDir = os.path.join(dir, self.fileName)
00096             self._makeDir()
00097         else:
00098             self.lockDir = dir
00099         self._locked = False


Member Function Documentation

Make sure directory exists 

Definition at line 168 of file lock.py.

00168 
00169     def _makeDir(self):
00170         """ Make sure directory exists """
00171         try:
00172             filesys.mkdir(self.dir)
00173             logging.debug('created directory: %s' % self.dir)
00174         except OSError, err:
00175             if err.errno != errno.EEXIST:
00176                 raise

Remove lockDir ignoring 'No such file or directory' errors 

Definition at line 177 of file lock.py.

00177 
00178     def _removeLockDir(self):
00179         """ Remove lockDir ignoring 'No such file or directory' errors """
00180         try:
00181             filesys.rmdir(self.lockDir)
00182             logging.debug('removed directory: %s' % self.dir)
00183         except OSError, err:
00184             if err.errno != errno.ENOENT:
00185                 raise
00186 

Here is the caller graph for this function:

def MoinMoin.util.lock.ExclusiveLock.acquire (   self,
  timeout = None 
)
Try to acquire a lock.

Try to create the lock directory. If it fails because another
lock exists, try to expire the other lock. Repeat after little
sleep until timeout passed.

Return True if a lock was acquired; False otherwise.

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

Definition at line 100 of file lock.py.

00100 
00101     def acquire(self, timeout=None):
00102         """ Try to acquire a lock.
00103 
00104         Try to create the lock directory. If it fails because another
00105         lock exists, try to expire the other lock. Repeat after little
00106         sleep until timeout passed.
00107 
00108         Return True if a lock was acquired; False otherwise.
00109         """
00110         timer = self.timerClass(timeout)
00111         timer.start()
00112         while timer.haveTime():
00113             try:
00114                 filesys.mkdir(self.lockDir)
00115                 self._locked = True
00116                 logging.debug('acquired exclusive lock: %s' % (self.lockDir, ))
00117                 return True
00118             except OSError, err:
00119                 if err.errno != errno.EEXIST:
00120                     raise
00121                 if self.expire():
00122                     continue # Try immediately to acquire
00123                 timer.sleep()
00124         logging.debug('failed to acquire exclusive lock: %s' % (self.lockDir, ))
00125         return False

Here is the call graph for this function:

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:

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:

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:

Definition at line 134 of file lock.py.

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

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.

Reimplemented in MoinMoin.util.lock.ReadLock, and MoinMoin.util.lock.WriteLock.

Definition at line 76 of file lock.py.

Reimplemented in MoinMoin.util.lock.ReadLock.

Definition at line 94 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: