Coverage for drivers/flock : 66%

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
# # 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 #
Fcntl-based Advisory Locking with a proper .trylock()
Python's fcntl module is not good at locking. In particular, proper testing and trying of locks isn't well supported. Looks as if we've got to grow our own. """
"""A C flock struct."""
"""See fcntl(2) for field details."""
# struct flock(2) format, tested with python2.4/i686 and # python2.5/x86_64. http://docs.python.org/lib/posix-large-files.html
"""Issues a system fcntl(fd, cmd, self). Updates self with what was returned by the kernel. Otherwise raises IOError(errno)."""
'l_whence': 1, 'l_start': 2, 'l_len': 3, 'l_pid': 4 }
idx = self.FIELDS[name] return self.fields[idx]
else: self.fields[idx] = value
"""Abstract base class for either reader or writer locks. A respective definition of LOCK_TYPE (fcntl.{F_RDLCK|F_WRLCK}) determines the type."""
if __debug__:
"""Creates a new, unheld lock.""" # # Subtle: fcntl(2) permits re-locking it as often as you want # once you hold it. This is slightly counterintuitive and we # want clean code, so we add one bit of our own bookkeeping. #
"""Blocking lock aquisition.""" assert not self._held, self.ERROR_ISLOCKED Flock(self.LOCK_TYPE).fcntl(self.fd, fcntl.F_SETLKW) self._held = True
"""Non-blocking lock aquisition. Returns True on success, False otherwise.""" if e.errno in [errno.EACCES, errno.EAGAIN]: return False raise
"""Returns True if @self holds the lock, False otherwise."""
"""Release a previously acquired lock."""
"""Returns the PID of the process holding the lock or -1 if the lock is not held.""" if self._held: return os.getpid() flock = Flock(self.LOCK_TYPE) flock.fcntl(self.fd, fcntl.F_GETLK) if flock.l_type == fcntl.F_UNLCK: return -1 return flock.l_pid
"""A simple global writer (i.e. exclusive) lock."""
"""A simple global reader (i.e. shared) lock."""
|