casacore
TableLockData.h
Go to the documentation of this file.
1//# TableLockData.h: Class to hold table lock data
2//# Copyright (C) 1997,1998,1999,2000,2002,2003
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_TABLELOCKDATA_H
29#define TABLES_TABLELOCKDATA_H
30
31
32//# Includes
33#include <casacore/casa/aips.h>
34#include <casacore/tables/Tables/TableLock.h>
35
36
37namespace casacore { //# NAMESPACE CASACORE - BEGIN
38
39// <summary>
40// Class to hold table lock data.
41// </summary>
42
43// <use visibility=local>
44
45// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tTable" demos="">
46// </reviewed>
47
48// <prerequisite>
49// <li> class <linkto class=Table>TableLock</linkto>
50// </prerequisite>
51
52// <synopsis>
53// This class keeps the <src>LockFile</src> object used to do the
54// actual locking/unlocking.
55// It also keeps the synchronization information.
56// </synopsis>
57
58// <motivation>
59// Encapsulate Table locking data.
60// </motivation>
61
62
64{
65public:
66 // Define the signature of the callback function when a lock is released.
67 // The flag <src>always</src> tells if the callback function should
68 // always write its main data (meant for case that table gets closed).
69 // The callback function has to write the synchronization data
70 // (preferably in canonical format) in a MemoryIO object.
71 // A pointer to this MemoryIO object has to be returned. A zero pointer
72 // can be returned when no synchronization data is available.
73 typedef MemoryIO* ReleaseCallBack (void* parentObject, Bool always);
74
75 // Construct from the given TableLock object.
76 TableLockData (const TableLock& lockOptions, ReleaseCallBack* = 0,
77 void* releaseParentObject = 0);
78
80
81 // Create the <src>LockFile</src> object and acquire a read or write
82 // lock when permanent locking is in effect.
83 // It throws an exception when acquiring the lock failed.
84 void makeLock (const String& name, Bool create, FileLocker::LockType,
85 uInt locknr = 0);
86
87 // Acquire a read or write lock.
88 // It throws an exception when acquire failed while it had to wait.
90
91 // Release the lock. When always==False, the lock is not released
92 // when a permanent lock is used.
93 // It does nothing when permanent locking is used.
94 // It throws an exception when the release failed.
95 // When the lock is released, the release callback function (if defined)
96 // is called to write the synchronization data.
97 void release (Bool always = False);
98
99 // When the inspection interval has expired, inspect if another process
100 // needs the lock. If so, release the lock.
101 // <src>always=True</src> means that the inspection is always done,
102 // thus not every 25th call or so.
103 void autoRelease (Bool always=False);
104
105 // Has this process the read or write lock, thus can the table
106 // be read or written safely?
108
109 // Is the table in use (i.e. open) in another process?
110 Bool isMultiUsed() const;
111
112 // Get or put the info in the lock file.
113 // <group>
114 void getInfo (MemoryIO& info);
115 void putInfo (const MemoryIO& info);
116 // </group>
117
118private:
119 // Copy constructor is forbidden.
121
122 // Assignment is forbidden.
124
125
126 //# Define the lock file.
128 //# Define if the file is already read or write locked.
131};
132
133
135{
136 return (itsLock == 0 ? True : itsLock->hasLock (type));
137}
139{
140 if (option() == AutoLocking && itsLock->inspect(always)) {
141 release();
142 }
143}
145{
146 return itsLock->isMultiUsed();
147}
148
149
151{
152 itsLock->getInfo (info);
153}
154inline void TableLockData::putInfo (const MemoryIO& info)
155{
156 itsLock->putInfo (info);
157}
158
159
160
161} //# NAMESPACE CASACORE - END
162
163#endif
LockType
Define the possible lock types.
Definition: FileLocker.h:95
Bool isMultiUsed()
Is the file associated with the LockFile object in use in another process?
Bool hasLock(FileLocker::LockType=FileLocker::Write) const
Test if the process has a lock for read or write on the file.
Definition: LockFile.h:360
void putInfo(const MemoryIO &info) const
Put the info into the file (after the request id's).
Bool inspect(Bool always=False)
Inspect if another process wants to access the file (i.e.
void getInfo(MemoryIO &info)
Get the request id's and the info from the lock file.
String: the storage and methods of handling collections of characters.
Definition: String.h:225
void autoRelease(Bool always=False)
When the inspection interval has expired, inspect if another process needs the lock.
void release(Bool always=False)
Release the lock.
void getInfo(MemoryIO &info)
Get or put the info in the lock file.
Bool acquire(MemoryIO *info, FileLocker::LockType, uInt nattempts)
Acquire a read or write lock.
MemoryIO * ReleaseCallBack(void *parentObject, Bool always)
Define the signature of the callback function when a lock is released.
Definition: TableLockData.h:73
ReleaseCallBack * itsReleaseCallBack
TableLockData & operator=(const TableLockData &that)
Assignment is forbidden.
Bool isMultiUsed() const
Is the table in use (i.e.
TableLockData(const TableLockData &that)
Copy constructor is forbidden.
void putInfo(const MemoryIO &info)
TableLockData(const TableLock &lockOptions, ReleaseCallBack *=0, void *releaseParentObject=0)
Construct from the given TableLock object.
void makeLock(const String &name, Bool create, FileLocker::LockType, uInt locknr=0)
Create the LockFile object and acquire a read or write lock when permanent locking is in effect.
Bool hasLock(FileLocker::LockType) const
Has this process the read or write lock, thus can the table be read or written safely?
@ AutoLocking
The system takes care of acquiring/releasing locks.
Definition: TableLock.h:99
LockOption option() const
Get the locking option.
Definition: TableLock.h:184
this file contains all the compiler specific defines
Definition: mainpage.dox:28
const Bool False
Definition: aipstype.h:44
unsigned int uInt
Definition: aipstype.h:51
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
const Bool True
Definition: aipstype.h:43