MVE - Multi-View Environment mve-devel
|
A simple file-based file lock implementation. More...
#include <file_system.h>
Public Types | |
enum | Status { LOCK_CREATED , LOCK_EXISTS , LOCK_PERSISTENT , LOCK_CREATE_ERROR } |
Public Member Functions | |
FileLock (std::string const &filename) | |
Acquires a lock for the given filename. | |
FileLock (void) | |
~FileLock (void) | |
Removes the lock if it exists. | |
Status | acquire (std::string const &filename) |
Tries to acquire a lock for the given filename and returns a status. | |
Status | acquire_retry (std::string const &filename, int retries=50, int sleep=100) |
Tries to acquire a lock for the given filename. | |
std::string const & | get_reason (void) const |
If locking failes, this returns the reason for failure. | |
bool | is_locked (std::string const &filename) |
Returns true if a lock for the given filename exists. | |
bool | release (void) |
Removes the lock if it exists. | |
bool | wait_lock (std::string const &filename, int retries=50, int sleep=100) |
Waits until a lock for given filename is released. | |
A simple file-based file lock implementation.
A file NAME.lock is created when a lock is acquired, and the file is removed when the lock is released. If the lock already exist, 'acquire' returns false. 'acquire_retry' retries for a few seconds and finally gives up returning false. Note that the ".lock" suffix is automatically used and must not be part of the given filenames.
Definition at line 165 of file file_system.h.
Definition at line 168 of file file_system.h.
|
inline |
Definition at line 263 of file file_system.h.
util::fs::FileLock::FileLock | ( | std::string const & | filename | ) |
Acquires a lock for the given filename.
If the lock already exists, the operation is re-attempted using default values. If the lock cannot be created an exception is thrown.
Definition at line 610 of file file_system.cc.
|
inline |
Removes the lock if it exists.
Definition at line 268 of file file_system.h.
FileLock::Status util::fs::FileLock::acquire | ( | std::string const & | filename | ) |
Tries to acquire a lock for the given filename and returns a status.
Definition at line 620 of file file_system.cc.
FileLock::Status util::fs::FileLock::acquire_retry | ( | std::string const & | filename, |
int | retries = 50 , |
||
int | sleep = 100 |
||
) |
Tries to acquire a lock for the given filename.
If a lock exists, the operation is re-attempted 'retry' times with 'sleep' milli seconds delay between the attempts.
Definition at line 646 of file file_system.cc.
|
inline |
If locking failes, this returns the reason for failure.
Definition at line 274 of file file_system.h.
bool util::fs::FileLock::is_locked | ( | std::string const & | filename | ) |
Returns true if a lock for the given filename exists.
Definition at line 671 of file file_system.cc.
bool util::fs::FileLock::release | ( | void | ) |
Removes the lock if it exists.
If removing the lock fails, the method returns false.
Definition at line 691 of file file_system.cc.
bool util::fs::FileLock::wait_lock | ( | std::string const & | filename, |
int | retries = 50 , |
||
int | sleep = 100 |
||
) |
Waits until a lock for given filename is released.
If filename is not locked, the method returns true immediately. If the lock is not released within the specified bounds, the method returns false.
Definition at line 678 of file file_system.cc.