LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst.utils.multithreading.lockProtection.LockProtected Class Reference
Inheritance diagram for lsst.utils.multithreading.lockProtection.LockProtected:

Public Member Functions

def __init__
 
def __enter__
 
def __exit__
 

Private Member Functions

def _checkLocked
 

Private Attributes

 _lp_lock
 

Detailed Description

a class that assists in keeping methods thread-safe.

This class is intended to be enlisted as a base class to the class being
protected.  A method that is to be protected from simultaneous access 
would include (at its beginning) a call to _checkLocked():

class MyClass(BaseClass, LockProtected):
    def __init__(self, lock=SharedLock):
        LockProtected.__init__(self, lock)
        ...

    def dangerous(self):
        self._checkLocked()
        ...

Doing so will require that the protected class be "locked" before a 
protected method can be called or else an UnsafeAccessError will be
raised.  Locking is done via the with statement:

mc = MyClass()
with mc:
    mc.dangerous()

For the locking to work, the protected class must provide to the
LockProtected constructor a lock object.  Typically this is a
lsst.utils.multithreading.SharedLock instance or, if one wants to
employ thread notification techniques, a
lsst.utils.multithreading.SharedData instance.  It should at least
be a reentrant lock--that is, having the behavior of threading.RLock
(from the standard Python Library).

This class is primarily intended for protecting methods across multiple
classes together via a single lock.  That is, it can prevent simultaneous
access to any protected method across multiple class instances.  To
accomplish this, each class to be protected should accept a lock as
a constructor argument.  Then the same lock is passed into all of the
constructors that need protection together. 

Definition at line 31 of file lockProtection.py.

Constructor & Destructor Documentation

def lsst.utils.multithreading.lockProtection.LockProtected.__init__ (   self,
  lock = None 
)
initialize the lock protection
@param lock   a reentrant lock instance to use.  If None, the
        protection behavior is disabled.

Definition at line 72 of file lockProtection.py.

72 
73  def __init__(self, lock=None):
74  """
75  initialize the lock protection
76  @param lock a reentrant lock instance to use. If None, the
77  protection behavior is disabled.
78  """
79  # the shared lock
80  self._lp_lock = lock

Member Function Documentation

def lsst.utils.multithreading.lockProtection.LockProtected.__enter__ (   self)

Definition at line 81 of file lockProtection.py.

81 
82  def __enter__(self):
83  if self._lp_lock:
84  self._lp_lock.acquire()
def lsst.utils.multithreading.lockProtection.LockProtected.__exit__ (   self,
  exc_type,
  exc_value,
  traceback 
)

Definition at line 85 of file lockProtection.py.

85 
86  def __exit__(self, exc_type, exc_value, traceback):
87  if self._lp_lock:
88  self._lp_lock.release()
89  return False
def lsst.utils.multithreading.lockProtection.LockProtected._checkLocked (   self)
private

Definition at line 90 of file lockProtection.py.

90 
91  def _checkLocked(self):
92  if self._lp_lock and not self._lp_lock._is_owned():
93  raise UnsafeAccessError()

Member Data Documentation

lsst.utils.multithreading.lockProtection.LockProtected._lp_lock
private

Definition at line 79 of file lockProtection.py.


The documentation for this class was generated from the following file: