casacore
Loading...
Searching...
No Matches
LatticeLocker.h
Go to the documentation of this file.
1//# LatticeLocker.h: Class to hold a (user) lock on a lattice
2//# Copyright (C) 1999,2000
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef LATTICES_LATTICELOCKER_H
27#define LATTICES_LATTICELOCKER_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/lattices/Lattices/LatticeBase.h>
33#include <casacore/tables/Tables/TableLock.h>
34
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary>
39// Class to hold a (user) lock on a lattice.
40// </summary>
41
42// <use visibility=export>
43
44// <reviewed reviewer="" date="" tests="tTableLockSync.cc">
45// </reviewed>
46
47// <prerequisite>
48//# Classes you should understand before using this one.
49// <li> <linkto class=Lattice>Lattice</linkto>
50// <li> <linkto class=TableLock>TableLock</linkto>
51// </prerequisite>
52
53// <synopsis>
54// Class LatticeLocker can be used to acquire a (user) lock on a lattice.
55// The lock can be a read or write lock.
56// The destructor releases the lock when needed.
57// <p>
58// LatticeLocker simply uses the <src>lock</src> and <src>unlock</src>
59// function of class Lattice.
60// The advantage of LatticeLocker over these functions is that the
61// destructor of LatticeLocker is called automatically by the system,
62// so unlocking the lattice does not need to be done explicitly and
63// cannot be forgotten. Especially in case of exception handling this
64// can be quite an adavantage.
65// <p>
66// This class is meant to be used with the UserLocking option.
67// It can, however, also be used with the other locking options.
68// In case of PermanentLocking(Wait) it won't do anything at all.
69// In case of AutoLocking it will acquire and release the lock when
70// needed. However, it is possible that the system releases an
71// auto lock before the LatticeLocker destructor is called.
72// <p>
73// The constructor of LatticeLocker will look if the lattice is
74// already appropriately locked. If so, it will set a flag to
75// prevent the destructor from unlocking the lattice. In this way
76// nested locks can be used. I.e. one can safely use LatticeLocker
77// in a function without having to be afraid that its destructor
78// would undo a lock set in a higher function.
79// <br>Similarly LatticeLocker will remember if a lattice was
80// already read-locked, when a write-lock is acquired. In such a
81// case the destructor will try to ensure that the lattice remains
82// read-locked.
83// </synopsis>
84
85// <example>
86// <srcblock>
87// // Open a lattice to be updated.
88// PagedArray<Float> myLattice (Table ("theLattice",
89// LatticeLock::UserLocking,
90// Lattice::Update);
91// // Start of some critical section requiring a lock.
92// {
93// LatticeLocker lock1 (myLattice, FileLocker::Write);
94// ... write the data
95// }
96// // The LatticeLocker destructor invoked by } unlocks the table.
97// </srcblock>
98// </example>
99
100// <motivation>
101// LatticeLocker makes it easier to unlock a lattice.
102// It also makes it easier to use locking in a nested way.
103// </motivation>
104
105//# <todo asof="$DATE:$">
106//# A List of bugs, limitations, extensions or planned refinements.
107//# </todo>
108
109
111{
112public:
113 // The constructor acquires a read or write lock on a lattice.
114 // If the lattice was already locked, the destructor will
115 // not unlock the lattice. This means that the class can be used in
116 // a nested way.
117 // <br>
118 // The number of attempts (default = forever) can be specified when
119 // acquiring the lock does not succeed immediately. When nattempts>1,
120 // the system waits 1 second between each attempt, so nattempts
121 // is more or less equal to a wait period in seconds.
122 // An exception is thrown when the lock cannot be acquired.
123 explicit LatticeLocker (LatticeBase& lattice,
125 uInt nattempts = 0);
126
127 // If the constructor acquired the lock, the destructor releases
128 // the lock and flushes the data if changed.
130
131 // Has this process the read or write lock, thus can the table
132 // be read or written safely?
134
135private:
136 // The copy constructor and assignment are not possible.
137 // Note that only one lock can be held on a lattice, so copying a
138 // TableLocker object imposes great difficulties which object should
139 // release the lock.
140 // It can be solved by turning LatticeLocker into a handle class
141 // with a reference counted body class.
142 // However, that will only be done when the need arises.
143 // <group>
146 // </group>
147
148 //# Variables.
152};
153
154
156{
157 return itsLatticePtr->hasLock (type);
158}
159
160
161
162} //# NAMESPACE CASACORE - END
163
164#endif
LockType
Define the possible lock types.
Definition FileLocker.h:93
virtual Bool hasLock(FileLocker::LockType) const
~LatticeLocker()
If the constructor acquired the lock, the destructor releases the lock and flushes the data if change...
LatticeLocker(LatticeBase &lattice, FileLocker::LockType, uInt nattempts=0)
The constructor acquires a read or write lock on a lattice.
LatticeLocker & operator=(const LatticeLocker &)
LatticeLocker(const LatticeLocker &)
The copy constructor and assignment are not possible.
LatticeBase * itsLatticePtr
Bool hasLock(FileLocker::LockType) const
Has this process the read or write lock, thus can the table be read or written safely?
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40