Coverage for drivers/lock : 52%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/python # # Copyright (C) Citrix Systems Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; version 2.1 only. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Simple file-based lock on a local FS. With shared reader/writer attributes."""
"""Create and open the lockable attribute base, if it doesn't exist. (But don't lock it yet.)"""
# one directory per namespace
# the lockfile inside that namespace directory per namespace
# If another lock within the namespace has already # cleaned up the namespace by removing the directory, # _open_lockfile raises an ENOENT, in this case we retry. raise
"""Provide a seam, so extreme situations could be tested"""
"""Close the lock, which implies releasing the lock.""" self.release()
ns = Lock._mknamespace(ns) path = os.path.join(Lock.BASE_DIR, ns, name) if os.path.exists(path): Lock._unlink(path)
ns = Lock._mknamespace(ns) nspath = os.path.join(Lock.BASE_DIR, ns)
if not os.path.exists(nspath): return
for file in os.listdir(nspath): path = os.path.join(nspath, file) Lock._unlink(path)
Lock._rmdir(nspath)
# # Lock and attribute file management #
"""Concurrent makedirs() catching EEXIST.""" except OSError, e: if e.errno != errno.EEXIST: raise LockException("Failed to makedirs(%s)" % path)
"""Non-raising unlink().""" util.SMlog("lock: unlinking lock file %s" % path) try: os.unlink(path) except Exception, e: util.SMlog("Failed to unlink(%s): %s" % (path, e))
"""Non-raising rmdir().""" util.SMlog("lock: removing lock dir %s" % path) try: os.rmdir(path) except Exception, e: util.SMlog("Failed to rmdir(%s): %s" % (path, e))
# # Actual Locking #
"""Blocking lock aquisition, with warnings. We don't expect to lock a lot. If so, not to collide. Coarse log statements should be ok and aid debugging.""" util.SMlog("Failed to lock %s on first attempt, " % self.lockpath + "blocked by PID %d" % self.lock.test()) self.lock.lock() if VERBOSE: util.SMlog("lock: acquired %s" % self.lockpath)
"""Acquire lock if possible, or return false if lock already held""" exists = os.path.exists(self.lockpath) ret = self.lock.trylock() if VERBOSE: util.SMlog("lock: tried lock %s, acquired: %s (exists: %s)" % \ (self.lockpath, ret, exists)) return ret
"""True if @self acquired the lock, False otherwise."""
"""Release a previously acquired lock.""" self.lock.unlock() if VERBOSE: util.SMlog("lock: released %s" % self.lockpath)
if __debug__:
# Create a Lock lock = Lock("test");
# Should not be yet held. assert lock.held() == False
# Go get it lock.acquire()
# Second lock shall throw in debug mode. try: lock.acquire() except AssertionError, e: if str(e) != flock.WriteLock.ERROR_ISLOCKED: raise else: raise AssertionError("Reaquired a locked lock")
lock.release()
Lock.cleanup()
print >>sys.stderr, "Running self tests..." test() print >>sys.stderr, "OK." |