L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
L4::Lock_guard Class Reference

Basic lock guard implementation that prevents forgotten unlocks on exit paths from a method or a block of code. More...

#include <lock_guard.h>

+ Collaboration diagram for L4::Lock_guard:

Public Member Functions

 Lock_guard (pthread_mutex_t &lock)
 Construct the lock guard and lock the associated mutex.
 
 Lock_guard (Lock_guard &&guard)
 Move constructor from other lock guard.
 
Lock_guardoperator= (Lock_guard &&guard)
 Move assignment from other lock guard.
 
int status () const
 Return last lock/unlock operation error status.
 
 ~Lock_guard ()
 Lock guard destructor.
 

Detailed Description

Basic lock guard implementation that prevents forgotten unlocks on exit paths from a method or a block of code.

Targeting pthread_mutex_t.

An instance of lock guard cannot be copied, but it can be moved.

The typical usage pattern of the lock guard is:

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
{
auto guard = L4Re::Lock_guard(mtx);
// Correctness check.
assert(guard.status() == 0);
// Critical section protected by mtx.
// The mtx is automatically unlocked when guard goes out of scope.
}

Definition at line 44 of file lock_guard.h.

Constructor & Destructor Documentation

◆ Lock_guard() [1/2]

L4::Lock_guard::Lock_guard ( pthread_mutex_t &  lock)
inlineexplicit

Construct the lock guard and lock the associated mutex.

The error condition of the locking operation can be checked by the status() method.

Parameters
lockAssociated mutex to be locked.

Definition at line 59 of file lock_guard.h.

◆ Lock_guard() [2/2]

L4::Lock_guard::Lock_guard ( Lock_guard &&  guard)
inline

Move constructor from other lock guard.

The mutex associated with the other lock guard is kept locked.

Parameters
guardLock guard to be moved.

Definition at line 71 of file lock_guard.h.

◆ ~Lock_guard()

L4::Lock_guard::~Lock_guard ( )
inline

Lock guard destructor.

The associated mutex (if any) is unlocked.

There is no mechanism for indicating any error conditions. However, if the mutex has been previously locked successfully by this class and if the implementation of the mutex behaves according to the POSIX specification, the construction of this class guarantees that the unlock operation does not fail.

Definition at line 126 of file lock_guard.h.

Member Function Documentation

◆ operator=()

Lock_guard & L4::Lock_guard::operator= ( Lock_guard &&  guard)
inline

Move assignment from other lock guard.

The mutex currently associated with this lock guard is unlocked. The mutex associated with the other lock guard is kept locked.

There is no mechanism for indicating any error conditions of the unlocking operation. However, if the mutex has been previously locked successfully by this class and if the implementation of the mutex behaves according to the POSIX specification, the construction of this class guarantees that the unlock operation does not fail.

Parameters
guardLock guard to be moved.

Definition at line 90 of file lock_guard.h.

◆ status()

int L4::Lock_guard::status ( ) const
inline

Return last lock/unlock operation error status.

Returns
Zero indicating no errors, any other value indicating an error.

Definition at line 110 of file lock_guard.h.


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