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