PreserveLibsRegistry: add lock/unlock assertions
authorZac Medico <zmedico@gentoo.org>
Sat, 7 May 2011 17:25:20 +0000 (10:25 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 7 May 2011 17:25:20 +0000 (10:25 -0700)
Also, add comments to store() about unobvious interaction with
locking due to atomic replacement of the inode.

pym/portage/util/_dyn_libs/PreservedLibsRegistry.py

index f3cbb3390aecd4579a138a0fc1e8d3bf258d38df..3fb8120487ea0355d77b1a94beb5892191d4b630 100644 (file)
@@ -36,11 +36,16 @@ class PreservedLibsRegistry(object):
 
        def lock(self):
                """Grab an exclusive lock on the preserved libs registry."""
+               if self._lock is not None:
+                       raise AssertionError("already locked")
                self._lock = lockfile(self._filename)
 
        def unlock(self):
                """Release our exclusive lock on the preserved libs registry."""
+               if self._lock is None:
+                       raise AssertionError("not locked")
                unlockfile(self._lock)
+               self._lock = None
 
        def load(self):
                """ Reload the registry data from file """
@@ -65,7 +70,13 @@ class PreservedLibsRegistry(object):
                self.pruneNonExisting()
 
        def store(self):
-               """ Store the registry data to file """
+               """
+               Store the registry data to the file. The existing inode will be
+               replaced atomically, so if that inode is currently being used
+               for a lock then that lock will be rendered useless. Therefore,
+               it is important not to call this method until the current lock
+               is ready to be immediately released.
+               """
                if os.environ.get("SANDBOX_ON") == "1" or \
                        self._data == self._data_orig:
                        return