MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
util::fs::FileLock Class Reference

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.
 

Detailed Description

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.

Member Enumeration Documentation

◆ Status

Enumerator
LOCK_CREATED 

The lock has been created successfully.

LOCK_EXISTS 

The lock has NOT been created because a lock already exists.

LOCK_PERSISTENT 

The lock has NOT been created because an existing lock persisted.

LOCK_CREATE_ERROR 

The lock has NOT been created because of file system issues.

Definition at line 168 of file file_system.h.

Constructor & Destructor Documentation

◆ FileLock() [1/2]

util::fs::FileLock::FileLock ( void  )
inline

Definition at line 263 of file file_system.h.

◆ FileLock() [2/2]

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.

◆ ~FileLock()

util::fs::FileLock::~FileLock ( void  )
inline

Removes the lock if it exists.

Definition at line 268 of file file_system.h.

Member Function Documentation

◆ acquire()

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.

◆ acquire_retry()

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.

◆ get_reason()

std::string const & util::fs::FileLock::get_reason ( void  ) const
inline

If locking failes, this returns the reason for failure.

Definition at line 274 of file file_system.h.

◆ is_locked()

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.

◆ release()

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.

◆ wait_lock()

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.


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