Browse Source

add Lock.got_exclusive_lock

Thomas Waldmann 9 years ago
parent
commit
26007c0162
2 changed files with 13 additions and 0 deletions
  1. 5 0
      borg/locking.py
  2. 8 0
      borg/testsuite/locking.py

+ 5 - 0
borg/locking.py

@@ -299,6 +299,8 @@ class UpgradableLock:
                 self._roster.modify(SHARED, REMOVE)
 
     def upgrade(self):
+        # WARNING: if multiple read-lockers want to upgrade, it will deadlock because they
+        # all will wait until the other read locks go away - and that won't happen.
         if not self.is_exclusive:
             self.acquire(exclusive=True, remove=SHARED)
 
@@ -306,6 +308,9 @@ class UpgradableLock:
         if self.is_exclusive:
             self.acquire(exclusive=False, remove=EXCLUSIVE)
 
+    def got_exclusive_lock(self):
+        return self.is_exclusive and self._lock.is_locked() and self._lock.by_me()
+
     def break_lock(self):
         self._roster.remove()
         self._lock.break_lock()

+ 8 - 0
borg/testsuite/locking.py

@@ -86,6 +86,14 @@ class TestUpgradableLock:
             assert len(lock._roster.get(SHARED)) == 1
             assert len(lock._roster.get(EXCLUSIVE)) == 0
 
+    def test_got_exclusive_lock(self, lockpath):
+        lock = UpgradableLock(lockpath, exclusive=True, id=ID1)
+        assert not lock.got_exclusive_lock()
+        lock.acquire()
+        assert lock.got_exclusive_lock()
+        lock.release()
+        assert not lock.got_exclusive_lock()
+
     def test_break(self, lockpath):
         lock = UpgradableLock(lockpath, exclusive=True, id=ID1).acquire()
         lock.break_lock()