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
lockProtection.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 """
24 classes used to protect method from simultaneous access by different
25 threads. The primary class is LockProtected.
26 """
27 import threading
28 
29 SharedLock = threading.RLock
30 
31 class LockProtected(object):
32  """
33  a class that assists in keeping methods thread-safe.
34 
35  This class is intended to be enlisted as a base class to the class being
36  protected. A method that is to be protected from simultaneous access
37  would include (at its beginning) a call to _checkLocked():
38 
39  class MyClass(BaseClass, LockProtected):
40  def __init__(self, lock=SharedLock):
41  LockProtected.__init__(self, lock)
42  ...
43 
44  def dangerous(self):
45  self._checkLocked()
46  ...
47 
48  Doing so will require that the protected class be "locked" before a
49  protected method can be called or else an UnsafeAccessError will be
50  raised. Locking is done via the with statement:
51 
52  mc = MyClass()
53  with mc:
54  mc.dangerous()
55 
56  For the locking to work, the protected class must provide to the
57  LockProtected constructor a lock object. Typically this is a
58  lsst.utils.multithreading.SharedLock instance or, if one wants to
59  employ thread notification techniques, a
60  lsst.utils.multithreading.SharedData instance. It should at least
61  be a reentrant lock--that is, having the behavior of threading.RLock
62  (from the standard Python Library).
63 
64  This class is primarily intended for protecting methods across multiple
65  classes together via a single lock. That is, it can prevent simultaneous
66  access to any protected method across multiple class instances. To
67  accomplish this, each class to be protected should accept a lock as
68  a constructor argument. Then the same lock is passed into all of the
69  constructors that need protection together.
70  """
71 
72  def __init__(self, lock=None):
73  """
74  initialize the lock protection
75  @param lock a reentrant lock instance to use. If None, the
76  protection behavior is disabled.
77  """
78  # the shared lock
79  self._lp_lock = lock
80 
81  def __enter__(self):
82  if self._lp_lock:
83  self._lp_lock.acquire()
84 
85  def __exit__(self, exc_type, exc_value, traceback):
86  if self._lp_lock:
87  self._lp_lock.release()
88  return False
89 
90  def _checkLocked(self):
91  if self._lp_lock and not self._lp_lock._is_owned():
92  raise UnsafeAccessError()
93 
94 class UnsafeAccessError(Exception):
95  """
96  an exception that is raised when one attempts to access a protected
97  method without first acquiring the lock on its class.
98  """
99  def __init__(self, msg=None):
100  if not msg:
101  msg = "Programmer Error: failed to obtain lock via with statement"
102  Exception.__init__(self, msg)
103