Arc::FileLock Class Reference

A general file locking class. More...

#include <FileLock.h>

Public Member Functions

 FileLock (const std::string &filename, unsigned int timeout=DEFAULT_LOCK_TIMEOUT, bool use_pid=true)
bool acquire (bool &lock_removed)
bool acquire ()
bool release (bool force=false)
int check (bool log_error=true)

Static Public Member Functions

static std::string getLockSuffix ()

Static Public Attributes

static const int DEFAULT_LOCK_TIMEOUT
static const std::string LOCK_SUFFIX

Detailed Description

A general file locking class.

This class can be used when protected access is required to files which are used by multiple processes or threads. Call acquire() to obtain a lock and release() to release it when finished. check() can be used to verify if a lock is valid for the current process. Locks are independent of FileLock objects - locks are only created and destroyed through acquire() and release(), not on creation or destruction of FileLock objects.

Unless use_pid is set false, the process ID and hostname of the calling process are stored in a file filename.lock in the form pid. This information is used to determine whether a lock is still valid. It is also possible to specify a timeout on the lock.

To ensure an atomic locking operation, acquire() first creates a temporary lock file filename.lock.XXXXXX, then attempts to rename this file to filename.lock. After a successful rename the lock file is checked to make sure the correct process ID and hostname are inside. This eliminates race conditions where multiple processes compete to obtain the lock.


Constructor & Destructor Documentation

Arc::FileLock::FileLock ( const std::string &  filename,
unsigned int  timeout = DEFAULT_LOCK_TIMEOUT,
bool  use_pid = true 
)

Create a new FileLock object.

Parameters:
filename The name of the file to be locked
timeout The timeout of the lock
use_pid If true, use process id in the lock and to determine lock validity

Member Function Documentation

bool Arc::FileLock::acquire (  ) 

Acquire the lock.

Callers can use this version of acquire() if they do not care whether an invalid lock was removed in the process of obtaining the lock.

bool Arc::FileLock::acquire ( bool &  lock_removed  ) 

Acquire the lock.

Returns true if the lock was acquired successfully. Locks are acquired if no lock file currently exists, or if the current lock file is invalid. A lock is invalid if the process ID inside the lock no longer exists on the host inside the lock, or the age of the lock file is greater than the lock timeout.

Parameters:
lock_removed Set to true if an existing lock was removed due to being invalid. In this case the caller may decide to check or delete the file as it is potentially corrupted.
Returns:
True if lock is successfully acquired
int Arc::FileLock::check ( bool  log_error = true  ) 

Check the lock is valid.

Returns 0 if the lock is valid for the current process, the pid inside the lock if the lock is owned by another process on the same host, and -1 if the lock is owned by another host or any other error occurred. log_error may be set to false to log error messages at INFO level, in cases where the lock not existing or being owned by another host are not errors.

bool Arc::FileLock::release ( bool  force = false  ) 

Release the lock.

Parameters:
force Remove the lock without checking ownership or timeout

The documentation for this class was generated from the following file:
Generated on Wed Jun 27 11:54:20 2012 for Hosting Environment (Daemon) by  doxygen 1.6.3