changeset 1309:238c43cb8877

lock: LazyReadLock, LazyWriteLock == do nothing. Only for POSIX OS and POSIX style of code using that kind of locks (see comments there).
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 17 Aug 2006 13:03:53 +0200
parents 9c6c07e63832
children fca5b70800b2
files MoinMoin/caching.py MoinMoin/util/lock.py
diffstat 2 files changed, 101 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/caching.py	Thu Aug 17 12:12:31 2006 +0200
+++ b/MoinMoin/caching.py	Thu Aug 17 13:03:53 2006 +0200
@@ -43,8 +43,8 @@
             filesys.makeDirs(self.arena_dir)
         if self.locking:
             self.lock_dir = os.path.join(self.arena_dir, '__lock__')
-            self.rlock = lock.ReadLock(self.lock_dir, 60.0)
-            self.wlock = lock.WriteLock(self.lock_dir, 60.0)
+            self.rlock = lock.LazyReadLock(self.lock_dir, 60.0)
+            self.wlock = lock.LazyWriteLock(self.lock_dir, 60.0)
 
     def _filename(self):
         return os.path.join(self.arena_dir, self.key)
--- a/MoinMoin/util/lock.py	Thu Aug 17 12:12:31 2006 +0200
+++ b/MoinMoin/util/lock.py	Thu Aug 17 13:03:53 2006 +0200
@@ -6,7 +6,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-import os, tempfile, time, errno
+import os, sys, tempfile, time, errno
 
 
 # Temporary debugging aid, to be replaced with system wide debuging
@@ -262,7 +262,6 @@
                 return True
         return False
 
-
 class ReadLock(ExclusiveLock):
     """ Read lock
     
@@ -305,3 +304,101 @@
                 self.writeLock.release()
         return False
 
+
+class LazyReadLock(ReadLock):
+    """ Lazy Read lock
+    
+    See ReadLock, but we do an optimization here:
+    If (and ONLY if) the resource protected by this lock is updated in a POSIX
+    style "write new content to tmpfile, rename tmpfile -> origfile", then reading
+    from an open origfile handle will give either the old content (when opened
+    before the rename happens) or the new content (when opened after the rename
+    happened), but never cause any trouble. This means that we don't have to lock
+    at all in that case.
+
+    Of course this doesn't work for us on the win32 platform:
+    * using MoveFileEx requires opening the file with some FILE_SHARE_DELETE
+      mode - we currently don't do that
+    * Win 95/98/ME do not have MoveFileEx
+    We currently solve by using the non-lazy locking code in ReadLock class.
+    """
+    def __init__(self, dir, timeout=None):
+        if sys.platform == 'win32':
+            ReadLock.__init__(self, dir, timeout)
+        else: # POSIX
+            self._locked = False
+
+    def acquire(self, timeout=None):
+        if sys.platform == 'win32':
+            return ReadLock.acquire(self, timeout)
+        else: # POSIX
+            self._locked = True
+            return True
+
+    def release(self):
+        if sys.platform == 'win32':
+            return ReadLock.release(self)
+        else:  # POSIX
+            self._locked = False
+
+    def exists(self):
+        if sys.platform == 'win32':
+            return ReadLock.exists(self)
+        else: # POSIX
+            return True
+
+    def isExpired(self):
+        if sys.platform == 'win32':
+            return ReadLock.isExpired(self)
+        else: # POSIX
+            return True
+
+    def expire(self):
+        if sys.platform == 'win32':
+            return ReadLock.expire(self)
+        else: # POSIX
+            return True
+
+class LazyWriteLock(WriteLock):
+    """ Lazy Write lock
+    
+    See WriteLock and LazyReadLock docs.
+    """
+    def __init__(self, dir, timeout=None):
+        if sys.platform == 'win32':
+            WriteLock.__init__(self, dir, timeout)
+        else: # POSIX
+            self._locked = False
+
+    def acquire(self, timeout=None):
+        if sys.platform == 'win32':
+            return WriteLock.acquire(self, timeout)
+        else: # POSIX
+            self._locked = True
+            return True
+
+    def release(self):
+        if sys.platform == 'win32':
+            return WriteLock.release(self)
+        else:  # POSIX
+            self._locked = False
+
+    def exists(self):
+        if sys.platform == 'win32':
+            return WriteLock.exists(self)
+        else: # POSIX
+            return True
+
+    def isExpired(self):
+        if sys.platform == 'win32':
+            return WriteLock.isExpired(self)
+        else: # POSIX
+            return True
+
+    def expire(self):
+        if sys.platform == 'win32':
+            return WriteLock.expire(self)
+        else: # POSIX
+            return True
+
+