casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ExternalLockSync.h
Go to the documentation of this file.
00001 //# ExternalLockSync.h: Class to hold table lock data
00002 //# Copyright (C) 1997,1998
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: ExternalLockSync.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef TABLES_EXTERNALLOCKSYNC_H
00029 #define TABLES_EXTERNALLOCKSYNC_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <tables/Tables/TableLockData.h>
00035 #include <tables/Tables/TableSyncData.h>
00036 
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 // <summary> 
00041 // Class to hold table lock data.
00042 // </summary>
00043 
00044 // <use visibility=local>
00045 
00046 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tTable" demos="">
00047 // </reviewed>
00048 
00049 // <prerequisite> 
00050 //    <li> class <linkto class=Table>TableLock</linkto>
00051 // </prerequisite>
00052 
00053 // <synopsis> 
00054 // This class keeps the <src>LockFile</src> object used to do the
00055 // actual locking/unlocking.
00056 // It also keeps the synchronization information.
00057 // </synopsis>
00058 
00059 // <motivation> 
00060 // Encapsulate Table locking data.
00061 // </motivation>
00062 
00063 
00064 class ExternalLockSync
00065 {
00066 public: 
00067     // Construct from the given TableLock object.
00068     ExternalLockSync (const TableLock& lockOptions);
00069 
00070     ~ExternalLockSync();
00071 
00072     // Create the <src>LockFile</src> object and acquire a read or write
00073     // lock when permanent locking is in effect.
00074     // It throws an exception when acquiring the lock failed.
00075     void makeLock (const String& tableName, Bool create, FileLocker::LockType);
00076 
00077     // Acquire a read or write lock (when needed).
00078     // Nattempts==0 indicates that it has to wait until the lock is acquired.
00079     // Nattempts>0 indicates that it gives up acquiring the lock when
00080     // nattempts have been done (with 1 second intervals).
00081     // It throws an exception when acquire failed while it had to wait.
00082     // It returns a false status when acquiring the lock failed
00083     // while it does not have to wait.
00084     // <br>When a lock is successfully acquired, the number of rows
00085     // (see function nrrow() below) is reset as a result of
00086     // synchronizing the access to the table.
00087     Bool acquire (FileLocker::LockType = FileLocker::Write, uInt nattempts = 0);
00088 
00089     // Get the current number of rows in this object.
00090     uInt nrow() const;
00091     
00092     // Release the lock and synchronize the table access.
00093     // When autolocking is in effect, the lock is only released when
00094     // the inspection-interval (see class
00095     // <linkto class=TableLockData>TableLockData</linkto>) has expired.
00096     // It does nothing when permanent locking is used.
00097     // It throws an exception when the release failed.
00098     void release (uInt nrrow);
00099 
00100     // Check if the table has a read or write lock, thus if the table can
00101     // be read or written safely.
00102     Bool hasLock (FileLocker::LockType) const;
00103 
00104 private:
00105     // Copy constructor is forbidden.
00106     ExternalLockSync (const ExternalLockSync& that);
00107 
00108     // Assignment is forbidden.
00109     ExternalLockSync& operator= (const ExternalLockSync& that);
00110 
00111     // The callback function when releasing a lock.
00112     static MemoryIO* releaseCallBack (void* lockSyncObject, Bool always);
00113 
00114     // The member function executing the callback functionality.
00115     MemoryIO* doReleaseCallBack (Bool always);
00116 
00117 
00118     //# Define the lock and sync data objects.
00119     TableLockData  itsLock;
00120     TableSyncData  itsSync;
00121     uInt           itsNrrow;
00122 };
00123 
00124 
00125 inline Bool ExternalLockSync::hasLock (FileLocker::LockType type) const
00126 {
00127     return itsLock.hasLock (type);
00128 }
00129 inline void ExternalLockSync::release (uInt nrrow)
00130 {
00131     itsNrrow = nrrow;
00132     itsLock.release();
00133 }
00134 inline MemoryIO* ExternalLockSync::doReleaseCallBack (Bool)
00135 {
00136     itsSync.write (itsNrrow);
00137     return &(itsSync.memoryIO());
00138 }
00139 inline uInt ExternalLockSync::nrow() const
00140 {
00141     return itsNrrow;
00142 }
00143 
00144 
00145 
00146 } //# NAMESPACE CASA - END
00147 
00148 #endif