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