LSSTApplications
10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
home
lsstsw
stack
Linux64
utils
11.0.rc2+6
python
lsst
utils
multithreading
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
lsst::utils.multithreading.lockProtection.LockProtected
Definition:
lockProtection.py:31
lsst::utils.multithreading.lockProtection.LockProtected._lp_lock
_lp_lock
Definition:
lockProtection.py:79
lsst::utils.multithreading.lockProtection.LockProtected.__exit__
def __exit__
Definition:
lockProtection.py:85
lsst::utils.multithreading.lockProtection.LockProtected._checkLocked
def _checkLocked
Definition:
lockProtection.py:90
lsst::utils.multithreading.lockProtection.LockProtected.__enter__
def __enter__
Definition:
lockProtection.py:81
lsst::utils.multithreading.lockProtection.LockProtected.__init__
def __init__
Definition:
lockProtection.py:72
lsst::utils.multithreading.lockProtection.UnsafeAccessError.__init__
def __init__
Definition:
lockProtection.py:99
lsst::utils.multithreading.lockProtection.UnsafeAccessError
Definition:
lockProtection.py:94
Generated on Wed Sep 16 2015 13:35:35 for LSSTApplications by
1.8.5