casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TableLockData.h
Go to the documentation of this file.
00001 //# TableLockData.h: Class to hold table lock data
00002 //# Copyright (C) 1997,1998,1999,2000,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: TableLockData.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef TABLES_TABLELOCKDATA_H
00029 #define TABLES_TABLELOCKDATA_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <tables/Tables/TableLock.h>
00035 
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 // <summary> 
00040 // Class to hold table lock data.
00041 // </summary>
00042 
00043 // <use visibility=local>
00044 
00045 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tTable" demos="">
00046 // </reviewed>
00047 
00048 // <prerequisite> 
00049 //    <li> class <linkto class=Table>TableLock</linkto>
00050 // </prerequisite>
00051 
00052 // <synopsis> 
00053 // This class keeps the <src>LockFile</src> object used to do the
00054 // actual locking/unlocking.
00055 // It also keeps the synchronization information.
00056 // </synopsis>
00057 
00058 // <motivation> 
00059 // Encapsulate Table locking data.
00060 // </motivation>
00061 
00062 
00063 class TableLockData : public TableLock
00064 {
00065 public: 
00066     // Define the signature of the callback function when a lock is released.
00067     // The flag <src>always</src> tells if the callback function should
00068     // always write its main data (meant for case that table gets closed).
00069     // The callback function has to write the synchronization data
00070     // (preferably in canonical format) in a MemoryIO object.
00071     // A pointer to this MemoryIO object has to be returned. A zero pointer
00072     // can be returned when no synchronization data is available.
00073     typedef MemoryIO* ReleaseCallBack (void* parentObject, Bool always);
00074 
00075     // Construct from the given TableLock object.
00076     TableLockData (const TableLock& lockOptions, ReleaseCallBack* = 0,
00077                    void* releaseParentObject = 0);
00078 
00079     ~TableLockData();
00080 
00081     // Create the <src>LockFile</src> object and acquire a read or write
00082     // lock when permanent locking is in effect.
00083     // It throws an exception when acquiring the lock failed.
00084     void makeLock (const String& name, Bool create, FileLocker::LockType,
00085                    uInt locknr = 0);
00086 
00087     // Acquire a read or write lock.
00088     // It throws an exception when acquire failed while it had to wait.
00089     Bool acquire (MemoryIO* info, FileLocker::LockType, uInt nattempts);
00090 
00091     // Release the lock. When always==False, the lock is not released
00092     // when a permanent lock is used.
00093     // It does nothing when permanent locking is used.
00094     // It throws an exception when the release failed.
00095     // When the lock is released, the release callback function (if defined)
00096     // is called to write the synchronization data.
00097     void release (Bool always = False);
00098 
00099     // When the inspection interval has expired, inspect if another process
00100     // needs the lock. If so, release the lock.
00101     // <src>always=True</src> means that the inspection is always done,
00102     // thus not every 25th call or so.
00103     void autoRelease (Bool always=False);
00104 
00105     // Has this process the read or write lock, thus can the table
00106     // be read or written safely?
00107     Bool hasLock (FileLocker::LockType) const;
00108 
00109     // Is the table in use (i.e. open) in another process?
00110     Bool isMultiUsed() const;
00111 
00112     // Get or put the info in the lock file.
00113     // <group>
00114     void getInfo (MemoryIO& info);
00115     void putInfo (const MemoryIO& info);
00116     // </group>
00117 
00118 private:
00119     // Copy constructor is forbidden.
00120     TableLockData (const TableLockData& that);
00121 
00122     // Assignment is forbidden.
00123     TableLockData& operator= (const TableLockData& that);
00124 
00125 
00126     //# Define the lock file.
00127     LockFile*        itsLock;
00128     //# Define if the file is already read or write locked.
00129     ReleaseCallBack* itsReleaseCallBack;
00130     void*            itsReleaseParent;
00131 };
00132 
00133 
00134 inline Bool TableLockData::hasLock (FileLocker::LockType type) const
00135 {
00136     return (itsLock == 0  ?  True : itsLock->hasLock (type));
00137 }
00138 inline void TableLockData::autoRelease (Bool always)
00139 {
00140     if (option() == AutoLocking  &&  itsLock->inspect(always)) {
00141         release();
00142     }
00143 }
00144 inline Bool TableLockData::isMultiUsed() const
00145 {
00146     return itsLock->isMultiUsed();
00147 }
00148 
00149 
00150 inline void TableLockData::getInfo (MemoryIO& info)
00151 {
00152     itsLock->getInfo (info);
00153 }
00154 inline void TableLockData::putInfo (const MemoryIO& info)
00155 {
00156     itsLock->putInfo (info);
00157 }
00158 
00159 
00160 
00161 } //# NAMESPACE CASA - END
00162 
00163 #endif