00001 //# TableLocker.h: Class to hold a (user) lock on a table 00002 //# Copyright (C) 1998,2000 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$ 00027 00028 #ifndef TABLES_TABLELOCKER_H 00029 #define TABLES_TABLELOCKER_H 00030 00031 00032 //# Includes 00033 #include <casacore/casa/aips.h> 00034 #include <casacore/tables/Tables/Table.h> 00035 #include <casacore/tables/Tables/TableLock.h> 00036 00037 00038 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00039 00040 // <summary> 00041 // Class to hold a (user) lock on a table. 00042 // </summary> 00043 00044 // <use visibility=export> 00045 00046 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tTableLockSync.cc"> 00047 // </reviewed> 00048 00049 // <prerequisite> 00050 //# Classes you should understand before using this one. 00051 // <li> <linkto class=Table>Table</linkto> 00052 // <li> <linkto class=TableLock>TableLock</linkto> 00053 // </prerequisite> 00054 00055 // <synopsis> 00056 // Class TableLocker can be used to acquire a (user) lock on a table. 00057 // The lock can be a read or write lock. 00058 // The destructor only releases the lock if the lock was acquired by the 00059 // constructor. 00060 // <p> 00061 // TableLocker simply uses the <src>lock</src> and <src>unlock</src> 00062 // function of class Table. 00063 // The advantage of TableLocker over these functions is that the 00064 // destructor of TableLocker is called automatically by the system, 00065 // so unlocking the table does not need to be done explicitly and 00066 // cannot be forgotten. Especially in case of exception handling this 00067 // can be quite an adavantage. 00068 // <p> 00069 // This class is meant to be used with the UserLocking option. 00070 // It can, however, also be used with the other locking options. 00071 // In case of PermanentLocking(Wait) it won't do anything at all. 00072 // In case of AutoLocking it will acquire and release the lock when 00073 // needed. However, it is possible that the system releases an 00074 // auto lock before the TableLocker destructor is called. 00075 // </synopsis> 00076 00077 // <example> 00078 // <srcblock> 00079 // // Open a table to be updated. 00080 // Table myTable ("theTable", TableLock::UserLocking, Table::Update); 00081 // // Start of some critical section requiring a lock. 00082 // { 00083 // TableLocker lock1 (myTable); 00084 // ... write the data 00085 // } 00086 // // The TableLocker destructor invoked by } unlocked the table. 00087 // </srcblock> 00088 // </example> 00089 00090 // <motivation> 00091 // TableLocker makes it easier to unlock a table. 00092 // </motivation> 00093 00094 //# <todo asof="$DATE:$"> 00095 //# A List of bugs, limitations, extensions or planned refinements. 00096 //# </todo> 00097 00098 00099 class TableLocker 00100 { 00101 public: 00102 // The constructor acquires a read or write lock on a table 00103 // which is released by the destructor. 00104 // If the table was already locked, the destructor will 00105 // not unlock the table. 00106 // <br> 00107 // The number of attempts (default = forever) can be specified when 00108 // acquiring the lock does not succeed immediately. When nattempts>1, 00109 // the system waits 1 second between each attempt, so nattempts 00110 // is more or less equal to a wait period in seconds. 00111 // An exception is thrown when the lock cannot be acquired. 00112 explicit TableLocker (Table& table, 00113 FileLocker::LockType = FileLocker::Write, 00114 uInt nattempts = 0); 00115 00116 // If locked, the destructor releases the lock and flushes the data. 00117 ~TableLocker(); 00118 00119 // Has this process the read or write lock, thus can the table 00120 // be read or written safely? 00121 Bool hasLock (FileLocker::LockType = FileLocker::Write) const; 00122 00123 private: 00124 // The copy constructor and assignment are not possible. 00125 // Note that only one lock can be held on a table, so copying a 00126 // TableLocker object imposes great difficulties which objects should 00127 // release the lock. 00128 // It can be solved by turning TableLocker into a handle class 00129 // with a reference counted body class. 00130 // However, that will only be done when the need arises. 00131 // <group> 00132 TableLocker (const TableLocker&); 00133 TableLocker& operator= (const TableLocker&); 00134 // </group> 00135 00136 //# Variables. 00137 Table itsTable; 00138 bool itsHadLock; 00139 }; 00140 00141 00142 inline Bool TableLocker::hasLock (FileLocker::LockType type) const 00143 { 00144 return itsTable.hasLock (type); 00145 } 00146 00147 00148 00149 } //# NAMESPACE CASACORE - END 00150 00151 #endif