CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::FileLock Class Reference

File-lock synchronization primitive. More...

#include <file_lock.h>

Public Member Functions

 FileLock ()
 
 FileLock (const Path &path)
 
 FileLock (const FileLock &)=delete
 
 FileLock (FileLock &&lock)=delete
 
 ~FileLock ()
 
FileLockoperator= (const Path &path)
 
FileLockoperator= (const FileLock &)=delete
 
FileLockoperator= (FileLock &&lock)=delete
 
const Pathpath () const noexcept
 Get the file-lock path. More...
 
void Assign (const Path &path)
 Assign a new file-lock path. More...
 
void Reset ()
 Reset file-lock. More...
 
bool TryLockRead ()
 Try to acquire read lock without block. More...
 
bool TryLockWrite ()
 Try to acquire write lock without block. More...
 
bool TryLockReadFor (const Timespan &timespan)
 Try to acquire read lock for the given timespan. More...
 
bool TryLockWriteFor (const Timespan &timespan)
 Try to acquire write lock for the given timespan. More...
 
bool TryLockReadUntil (const UtcTimestamp &timestamp)
 Try to acquire read lock until the given timestamp. More...
 
bool TryLockWriteUntil (const UtcTimestamp &timestamp)
 Try to acquire write lock until the given timestamp. More...
 
void LockRead ()
 Acquire read lock with block. More...
 
void LockWrite ()
 Acquire write lock with block. More...
 
void UnlockRead ()
 Release read lock. More...
 
void UnlockWrite ()
 Release write lock. More...
 

Detailed Description

File-lock synchronization primitive.

A file-lock provide shared and exclusive access to some resource based on the file locking functionality provided by OS. This means that multiple threads or processes can read the data in parallel but an exclusive lock is needed for writing or modifying data. When a writer is writing the data, all other writers or readers will be blocked until the writer is finished writing.

Thread-safe.

https://en.wikipedia.org/wiki/File_locking

Examples
threads_file_lock.cpp.

Definition at line 33 of file file_lock.h.

Constructor & Destructor Documentation

◆ FileLock() [1/4]

CppCommon::FileLock::FileLock ( )

Definition at line 288 of file file_lock.cpp.

◆ FileLock() [2/4]

CppCommon::FileLock::FileLock ( const Path path)
explicit

Definition at line 299 of file file_lock.cpp.

◆ FileLock() [3/4]

CppCommon::FileLock::FileLock ( const FileLock )
delete

◆ FileLock() [4/4]

CppCommon::FileLock::FileLock ( FileLock &&  lock)
delete

◆ ~FileLock()

CppCommon::FileLock::~FileLock ( )

Definition at line 312 of file file_lock.cpp.

Member Function Documentation

◆ Assign()

void CppCommon::FileLock::Assign ( const Path path)

Assign a new file-lock path.

Parameters
path- File-lock path

Definition at line 326 of file file_lock.cpp.

◆ LockRead()

void CppCommon::FileLock::LockRead ( )

Acquire read lock with block.

Will block.

Definition at line 380 of file file_lock.cpp.

◆ LockWrite()

void CppCommon::FileLock::LockWrite ( )

Acquire write lock with block.

Will block.

Definition at line 381 of file file_lock.cpp.

◆ operator=() [1/3]

FileLock& CppCommon::FileLock::operator= ( const FileLock )
delete

◆ operator=() [2/3]

FileLock & CppCommon::FileLock::operator= ( const Path path)

Definition at line 318 of file file_lock.cpp.

◆ operator=() [3/3]

FileLock& CppCommon::FileLock::operator= ( FileLock &&  lock)
delete

◆ path()

const Path & CppCommon::FileLock::path ( ) const
noexcept

Get the file-lock path.

Definition at line 324 of file file_lock.cpp.

◆ Reset()

void CppCommon::FileLock::Reset ( )

Reset file-lock.

Definition at line 327 of file file_lock.cpp.

◆ TryLockRead()

bool CppCommon::FileLock::TryLockRead ( )

Try to acquire read lock without block.

Will not block.

Returns
'true' if the read lock was successfully acquired, 'false' if the read lock is busy

Definition at line 329 of file file_lock.cpp.

◆ TryLockReadFor()

bool CppCommon::FileLock::TryLockReadFor ( const Timespan timespan)

Try to acquire read lock for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the read lock
Returns
'true' if the read lock was successfully acquired, 'false' if the read lock is busy

Definition at line 332 of file file_lock.cpp.

◆ TryLockReadUntil()

bool CppCommon::FileLock::TryLockReadUntil ( const UtcTimestamp timestamp)
inline

Try to acquire read lock until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the read lock
Returns
'true' if the read lock was successfully acquired, 'false' if the read lock is busy

Definition at line 96 of file file_lock.h.

◆ TryLockWrite()

bool CppCommon::FileLock::TryLockWrite ( )

Try to acquire write lock without block.

Will not block.

Returns
'true' if the write lock was successfully acquired, 'false' if the write lock is busy

Definition at line 330 of file file_lock.cpp.

◆ TryLockWriteFor()

bool CppCommon::FileLock::TryLockWriteFor ( const Timespan timespan)

Try to acquire write lock for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the write lock
Returns
'true' if the write lock was successfully acquired, 'false' if the write lock is busy

Definition at line 356 of file file_lock.cpp.

◆ TryLockWriteUntil()

bool CppCommon::FileLock::TryLockWriteUntil ( const UtcTimestamp timestamp)
inline

Try to acquire write lock until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the write lock
Returns
'true' if the write lock was successfully acquired, 'false' if the write lock is busy

Definition at line 105 of file file_lock.h.

◆ UnlockRead()

void CppCommon::FileLock::UnlockRead ( )

Release read lock.

Will not block.

Definition at line 382 of file file_lock.cpp.

◆ UnlockWrite()

void CppCommon::FileLock::UnlockWrite ( )

Release write lock.

Will not block.

Definition at line 383 of file file_lock.cpp.


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