casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableLocker.h
Go to the documentation of this file.
1 //# TableLocker.h: Class to hold a (user) lock on a table
2 //# Copyright (C) 1998,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: aips2-request@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 //# $Id$
27 
28 #ifndef TABLES_TABLELOCKER_H
29 #define TABLES_TABLELOCKER_H
30 
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
36 
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 // <summary>
41 // Class to hold a (user) lock on a table.
42 // </summary>
43 
44 // <use visibility=export>
45 
46 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tTableLockSync.cc">
47 // </reviewed>
48 
49 // <prerequisite>
50 //# Classes you should understand before using this one.
51 // <li> <linkto class=Table>Table</linkto>
52 // <li> <linkto class=TableLock>TableLock</linkto>
53 // </prerequisite>
54 
55 // <synopsis>
56 // Class TableLocker can be used to acquire a (user) lock on a table.
57 // The lock can be a read or write lock.
58 // The destructor only releases the lock if the lock was acquired by the
59 // constructor.
60 // <p>
61 // TableLocker simply uses the <src>lock</src> and <src>unlock</src>
62 // function of class Table.
63 // The advantage of TableLocker over these functions is that the
64 // destructor of TableLocker is called automatically by the system,
65 // so unlocking the table does not need to be done explicitly and
66 // cannot be forgotten. Especially in case of exception handling this
67 // can be quite an adavantage.
68 // <p>
69 // This class is meant to be used with the UserLocking option.
70 // It can, however, also be used with the other locking options.
71 // In case of PermanentLocking(Wait) it won't do anything at all.
72 // In case of AutoLocking it will acquire and release the lock when
73 // needed. However, it is possible that the system releases an
74 // auto lock before the TableLocker destructor is called.
75 // </synopsis>
76 
77 // <example>
78 // <srcblock>
79 // // Open a table to be updated.
80 // Table myTable ("theTable", TableLock::UserLocking, Table::Update);
81 // // Start of some critical section requiring a lock.
82 // {
83 // TableLocker lock1 (myTable);
84 // ... write the data
85 // }
86 // // The TableLocker destructor invoked by } unlocked the table.
87 // </srcblock>
88 // </example>
89 
90 // <motivation>
91 // TableLocker makes it easier to unlock a table.
92 // </motivation>
93 
94 //# <todo asof="$DATE:$">
95 //# A List of bugs, limitations, extensions or planned refinements.
96 //# </todo>
97 
98 
100 {
101 public:
102  // The constructor acquires a read or write lock on a table
103  // which is released by the destructor.
104  // If the table was already locked, the destructor will
105  // not unlock the table.
106  // <br>
107  // The number of attempts (default = forever) can be specified when
108  // acquiring the lock does not succeed immediately. When nattempts>1,
109  // the system waits 1 second between each attempt, so nattempts
110  // is more or less equal to a wait period in seconds.
111  // An exception is thrown when the lock cannot be acquired.
112  explicit TableLocker (Table& table,
114  uInt nattempts = 0);
115 
116  // If locked, the destructor releases the lock and flushes the data.
117  ~TableLocker();
118 
119  // Has this process the read or write lock, thus can the table
120  // be read or written safely?
122 
123 private:
124  // The copy constructor and assignment are not possible.
125  // Note that only one lock can be held on a table, so copying a
126  // TableLocker object imposes great difficulties which objects should
127  // release the lock.
128  // It can be solved by turning TableLocker into a handle class
129  // with a reference counted body class.
130  // However, that will only be done when the need arises.
131  // <group>
132  TableLocker (const TableLocker&);
134  // </group>
135 
136  //# Variables.
139 };
140 
141 
143 {
144  return itsTable.hasLock (type);
145 }
146 
147 
148 
149 } //# NAMESPACE CASACORE - END
150 
151 #endif
TableLocker(Table &table, FileLocker::LockType=FileLocker::Write, uInt nattempts=0)
The constructor acquires a read or write lock on a table which is released by the destructor...
Main interface class to a read/write table.
Definition: Table.h:157
~TableLocker()
If locked, the destructor releases the lock and flushes the data.
TableLocker & operator=(const TableLocker &)
Acquire a write lock.
Definition: FileLocker.h:99
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Bool hasLock(FileLocker::LockType=FileLocker::Write) const
Has this process the read or write lock, thus can the table be read or written safely?
Definition: Table.h:1121
LockType
Define the possible lock types.
Definition: FileLocker.h:95
Class to hold a (user) lock on a table.
Definition: TableLocker.h:99
unsigned int uInt
Definition: aipstype.h:51
Bool hasLock(FileLocker::LockType=FileLocker::Write) const
Has this process the read or write lock, thus can the table be read or written safely?
Definition: TableLocker.h:142