-# A file locker (Linux only)
-import fcntl
-class PortManagerLock:
- def __init__(self, filename, readonly=False, blocking=True):
- # This will create it if it does not exist already
- logger.debug("Create lock on %s"%filename)
- self.__readonly = readonly
- self.__blocking = blocking
- self.__filename = filename
- flag = 'w'
- if self.__readonly:
- flag = 'r'
- self.handle = open(self.__filename, 'a+')
-
- def acquire(self):
- mode = fcntl.LOCK_EX
- if not self.__blocking: # Raise an IOError exception if file has already been locked
- mode = mode | fcntl.LOCK_NB
- fcntl.flock(self.handle, mode)
- logger.debug("lock acquired %s"%self.__blocking)
-
- def release(self):
- fcntl.flock(self.handle, fcntl.LOCK_UN)
- logger.debug("lock released")
-
- def __del__(self):
- logger.debug("Close lock file")
- self.handle.close()
- os.remove(self.__filename)
+# A file locker
+def __acquire_lock(lock):
+ logger.debug("ACQUIRE LOCK")
+ if sys.platform == "win32":
+ import msvcrt
+ # lock 1 byte: file is supposed to be zero-byte long
+ msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1)
+ else:
+ import fcntl
+ fcntl.flock(lock, fcntl.LOCK_EX)
+ logger.debug("LOCK ACQUIRED")
+#
+def __release_lock(lock):
+ logger.debug("RELEASE LOCK")
+ if sys.platform == "win32":
+ import msvcrt
+ msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
+ else:
+ import fcntl
+ fcntl.flock(lock, fcntl.LOCK_UN)
+ logger.debug("LOCK RELEASED")