casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Private Attributes | List of all members
casacore::FileLocker Class Reference

Class to handle file locking. More...

#include <FileLocker.h>

Public Types

enum  LockType {
  Read,
  Write
}
 Define the possible lock types. More...
 

Public Member Functions

 FileLocker ()
 Default constructor creates an invalid fd. More...
 
 FileLocker (int fd, uInt start=0, uInt length=0)
 Construct the FileLocker object for the given file descriptor. More...
 
 ~FileLocker ()
 
Bool acquire (LockType=Write, uInt nattempts=0)
 Acquire a write or read lock. More...
 
Bool release ()
 Release a lock. More...
 
Bool canLock (LockType=Write)
 Test if the file can be locked for read or write. More...
 
Bool canLock (uInt &pid, LockType=Write)
 
Bool hasLock (LockType=Write) const
 Test if the process has a lock for read or write on the file. More...
 
int fd () const
 Get the fd in use. More...
 
int lastError () const
 Get the last error. More...
 
String lastMessage () const
 Get the message belonging to the last error. More...
 

Private Attributes

int itsFD
 
int itsError
 
int itsStart
 
int itsLength
 
Bool itsMsgShown
 
Bool itsReadLocked
 temporary for SUSE 6.1 More...
 
Bool itsWriteLocked
 

Detailed Description

Class to handle file locking.

Intended use:

Public interface

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25
Test programs:
tLockFile

Prerequisite

Synopsis

This class handles file locking by means of the fcntl SETLK function. Locking of files on NFS-mounted file systems works correctly as long as the NFS lockd and statd deamons are configured correctly. Otherwise lock requests may be granted incorrectly.

Acquiring a lock can be done for a read or a write lock. Multiple locks on a file can exist as long as they are all read locks. When a write lock is involved, no other lock can exist. It is possible to acquire a lock in 2 ways:

Example

int fd = open ("file.name");
FileLocker lock(fd);
if (lock.acquire()) {
... do something with the file..\.
lock.release();
}else{
cout << lock.lastMessage() << endl;
}

Motivation

Make it possible to lock files in a standard way.

Definition at line 91 of file FileLocker.h.

Member Enumeration Documentation

Define the possible lock types.

Enumerator
Read 

Acquire a read lock.

Write 

Acquire a write lock.

Definition at line 95 of file FileLocker.h.

Constructor & Destructor Documentation

casacore::FileLocker::FileLocker ( )

Default constructor creates an invalid fd.

casacore::FileLocker::FileLocker ( int  fd,
uInt  start = 0,
uInt  length = 0 
)
explicit

Construct the FileLocker object for the given file descriptor.

This can be used to lock a segment of the given file. The segment is given by start and length. Length=0 means till the end of the file.

casacore::FileLocker::~FileLocker ( )

Member Function Documentation

Bool casacore::FileLocker::acquire ( LockType  = Write,
uInt  nattempts = 0 
)

Acquire a write or read lock.

nattempts defines how often it tries to acquire the lock. A zero value indicates an infinite number of times (i.e. wait until the lock is acquired). A positive value means it waits 1 second between each attempt.

Bool casacore::FileLocker::canLock ( LockType  = Write)

Test if the file can be locked for read or write.

Optionally the PID of the process holding the lock is returned.

Referenced by casacore::LockFile::canLock().

Bool casacore::FileLocker::canLock ( uInt pid,
LockType  = Write 
)
int casacore::FileLocker::fd ( ) const
inline

Get the fd in use.

Definition at line 158 of file FileLocker.h.

References itsFD.

Bool casacore::FileLocker::hasLock ( LockType  type = Write) const
inline

Test if the process has a lock for read or write on the file.

Definition at line 154 of file FileLocker.h.

References itsReadLocked, itsWriteLocked, and Write.

Referenced by casacore::LockFile::hasLock().

int casacore::FileLocker::lastError ( ) const
inline

Get the last error.

Definition at line 162 of file FileLocker.h.

References itsError.

Referenced by casacore::LockFile::lastError().

String casacore::FileLocker::lastMessage ( ) const

Get the message belonging to the last error.

Referenced by casacore::LockFile::lastMessage().

Bool casacore::FileLocker::release ( )

Release a lock.

The return status indicates if an error occurred.

Member Data Documentation

int casacore::FileLocker::itsError
private

Definition at line 145 of file FileLocker.h.

Referenced by lastError().

int casacore::FileLocker::itsFD
private

Definition at line 144 of file FileLocker.h.

Referenced by fd().

int casacore::FileLocker::itsLength
private

Definition at line 147 of file FileLocker.h.

Bool casacore::FileLocker::itsMsgShown
private

Definition at line 148 of file FileLocker.h.

Bool casacore::FileLocker::itsReadLocked
private

temporary for SUSE 6.1

Definition at line 149 of file FileLocker.h.

Referenced by hasLock().

int casacore::FileLocker::itsStart
private

Definition at line 146 of file FileLocker.h.

Bool casacore::FileLocker::itsWriteLocked
private

Definition at line 150 of file FileLocker.h.

Referenced by hasLock().


The documentation for this class was generated from the following file: