casacore
Loading...
Searching...
No Matches
FileLocker.h
Go to the documentation of this file.
1//# FileLocker.h: Class to handle file locking
2//# Copyright (C) 1997,1998,1999,2000
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: casa-feedback@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#ifndef CASA_FILELOCKER_H
27#define CASA_FILELOCKER_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35//# Forward Declarations
36class String;
37
38
39// <summary>
40// Class to handle file locking.
41// </summary>
42
43// <use visibility=export>
44
45// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tLockFile" demos="">
46// </reviewed>
47
48// <prerequisite>
49// <li> man page of fcntl
50// </prerequisite>
51
52// <synopsis>
53// This class handles file locking by means of the fcntl SETLK function.
54// Locking of files on NFS-mounted file systems works correctly
55// as long as the NFS lockd and statd deamons are configured correctly.
56// Otherwise lock requests may be granted incorrectly.
57// <p>
58// Acquiring a lock can be done for a read or a write lock.
59// Multiple locks on a file can exist as long as they are all
60// read locks. When a write lock is involved, no other lock can exist.
61// It is possible to acquire a lock in 2 ways:
62// <ul>
63// <li>Wait until the lock request is granted; i.e. until the processes
64// holding a lock on the file release their lock.
65// <li>Do several attempts; between each attempt it sleeps 1 second.
66// Note that nattempts=1 means it returns immediately when the
67// lock request could not be granted.
68// </ul>
69// </synopsis>
70
71// <example>
72// <srcblock>
73// int fd = open ("file.name");
74// FileLocker lock(fd);
75// if (lock.acquire()) {
76// ... do something with the file ...
77// lock.release();
78// }else{
79// cout << lock.lastMessage() << endl;
80// }
81// </srcblock>
82// </example>
83
84// <motivation>
85// Make it possible to lock files in a standard way.
86// </motivation>
87
88
90{
91public:
92 // Define the possible lock types.
93 enum LockType {
94 // Acquire a read lock.
96 // Acquire a write lock.
97 Write
98 };
99
100 // Default constructor creates an invalid fd.
102
103 // Construct the FileLocker object for the given file descriptor.
104 // This can be used to lock a segment of the given file.
105 // The segment is given by start and length. Length=0 means till the
106 // end of the file.
107 explicit FileLocker (int fd, uInt start=0, uInt length=0);
108
110
111 // Acquire a write or read lock.
112 // <src>nattempts</src> defines how often it tries to acquire the lock.
113 // A zero value indicates an infinite number of times (i.e. wait until
114 // the lock is acquired).
115 // A positive value means it waits 1 second between each attempt.
116 Bool acquire (LockType = Write, uInt nattempts = 0);
117
118 // Release a lock.
119 // The return status indicates if an error occurred.
121
122 // Test if the file can be locked for read or write.
123 // Optionally the PID of the process holding the lock is returned.
124 // <group>
127 // </group>
128
129 // Test if the process has a lock for read or write on the file.
130 Bool hasLock (LockType = Write) const;
131
132 // Get the fd in use.
133 int fd() const;
134
135 // Get the last error.
136 int lastError() const;
137
138 // Get the message belonging to the last error.
140
141private:
142 int itsFD;
149};
150
151
153{
154 return (type == Write ? itsWriteLocked : itsReadLocked);
155}
156inline int FileLocker::fd() const
157{
158 return itsFD;
159}
160inline int FileLocker::lastError() const
161{
162 return itsError;
163}
164
165
166
167} //# NAMESPACE CASACORE - END
168
169#endif
170
Bool canLock(uInt &pid, LockType=Write)
int lastError() const
Get the last error.
Definition FileLocker.h:160
Bool itsReadLocked
temporary for SUSE 6.1
Definition FileLocker.h:147
Bool canLock(LockType=Write)
Test if the file can be locked for read or write.
Bool release()
Release a lock.
int fd() const
Get the fd in use.
Definition FileLocker.h:156
Bool hasLock(LockType=Write) const
Test if the process has a lock for read or write on the file.
Definition FileLocker.h:152
LockType
Define the possible lock types.
Definition FileLocker.h:93
@ Write
Acquire a write lock.
Definition FileLocker.h:97
@ Read
Acquire a read lock.
Definition FileLocker.h:95
String lastMessage() const
Get the message belonging to the last error.
Bool acquire(LockType=Write, uInt nattempts=0)
Acquire a write or read lock.
FileLocker(int fd, uInt start=0, uInt length=0)
Construct the FileLocker object for the given file descriptor.
FileLocker()
Default constructor creates an invalid fd.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40