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// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/lattices/Lattices/LatticeBase.h>
32#include <casacore/tables/Tables/TableLock.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// <summary>
37// Class to hold a (user) lock on a lattice.
38// </summary>
39
40// <use visibility=export>
41
42// <reviewed reviewer="" date="" tests="tTableLockSync.cc">
43// </reviewed>
44
45// <prerequisite>
46// # Classes you should understand before using this one.
47// <li> <linkto class=Lattice>Lattice</linkto>
48// <li> <linkto class=TableLock>TableLock</linkto>
49// </prerequisite>
50
51// <synopsis>
52// Class LatticeLocker can be used to acquire a (user) lock on a lattice.
53// The lock can be a read or write lock.
54// The destructor releases the lock when needed.
55// <p>
56// LatticeLocker simply uses the <src>lock</src> and <src>unlock</src>
57// function of class Lattice.
58// The advantage of LatticeLocker over these functions is that the
59// destructor of LatticeLocker is called automatically by the system,
60// so unlocking the lattice does not need to be done explicitly and
61// cannot be forgotten. Especially in case of exception handling this
62// can be quite an adavantage.
63// <p>
64// This class is meant to be used with the UserLocking option.
65// It can, however, also be used with the other locking options.
66// In case of PermanentLocking(Wait) it won't do anything at all.
67// In case of AutoLocking it will acquire and release the lock when
68// needed. However, it is possible that the system releases an
69// auto lock before the LatticeLocker destructor is called.
70// <p>
71// The constructor of LatticeLocker will look if the lattice is
72// already appropriately locked. If so, it will set a flag to
73// prevent the destructor from unlocking the lattice. In this way
74// nested locks can be used. I.e. one can safely use LatticeLocker
75// in a function without having to be afraid that its destructor
76// would undo a lock set in a higher function.
77// <br>Similarly LatticeLocker will remember if a lattice was
78// already read-locked, when a write-lock is acquired. In such a
79// case the destructor will try to ensure that the lattice remains
80// read-locked.
81// </synopsis>
82
83// <example>
84// <srcblock>
85// // Open a lattice to be updated.
86// PagedArray<Float> myLattice (Table ("theLattice",
87// LatticeLock::UserLocking,
88// Lattice::Update);
89// // Start of some critical section requiring a lock.
90// {
91// LatticeLocker lock1 (myLattice, FileLocker::Write);
92// ... write the data
93// }
94// // The LatticeLocker destructor invoked by } unlocks the table.
95// </srcblock>
96// </example>
97
98// <motivation>
99// LatticeLocker makes it easier to unlock a lattice.
100// It also makes it easier to use locking in a nested way.
101// </motivation>
102
103// # <todo asof="$DATE:$">
104// # A List of bugs, limitations, extensions or planned refinements.
105// # </todo>
106
108 public:
109 // The constructor acquires a read or write lock on a lattice.
110 // If the lattice was already locked, the destructor will
111 // not unlock the lattice. This means that the class can be used in
112 // a nested way.
113 // <br>
114 // The number of attempts (default = forever) can be specified when
115 // acquiring the lock does not succeed immediately. When nattempts>1,
116 // the system waits 1 second between each attempt, so nattempts
117 // is more or less equal to a wait period in seconds.
118 // An exception is thrown when the lock cannot be acquired.
119 explicit LatticeLocker(LatticeBase& lattice, FileLocker::LockType, uInt nattempts = 0);
120
121 // If the constructor acquired the lock, the destructor releases
122 // the lock and flushes the data if changed.
124
125 // Has this process the read or write lock, thus can the table
126 // be read or written safely?
128
129 private:
130 // The copy constructor and assignment are not possible.
131 // Note that only one lock can be held on a lattice, so copying a
132 // TableLocker object imposes great difficulties which object should
133 // release the lock.
134 // It can be solved by turning LatticeLocker into a handle class
135 // with a reference counted body class.
136 // However, that will only be done when the need arises.
137 // <group>
140 // </group>
141
142 // # Variables.
146};
147
149 return itsLatticePtr->hasLock(type);
150}
151
152} // namespace casacore
153
154#endif
LockType
Define the possible lock types.
Definition FileLocker.h:89
~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?
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40