Embedded Multicore Building Blocks V1.0.0
Public Member Functions | List of all members
embb::base::UniqueLock< Mutex > Class Template Reference

Flexible ownership wrapper for a mutex. More...

#include <mutex.h>

Public Member Functions

 UniqueLock ()
 Creates a lock without assigned mutex. More...
 
 UniqueLock (Mutex &mutex)
 Creates a lock from an unlocked mutex and locks it. More...
 
 UniqueLock (Mutex &mutex, DeferLockTag)
 Creates a lock from an unlocked mutex without locking it. More...
 
 UniqueLock (Mutex &mutex, TryLockTag)
 Creates a lock from an unlocked mutex and tries to lock it. More...
 
 UniqueLock (Mutex &mutex, AdoptLockTag)
 Creates a lock from an already locked mutex. More...
 
 ~UniqueLock ()
 Unlocks the mutex if owned. More...
 
void Lock ()
 Waits until the mutex is unlocked and locks it. More...
 
bool TryLock ()
 Tries to lock the mutex and returns immediately. More...
 
void Unlock ()
 Unlocks the mutex. More...
 
void Swap (UniqueLock< Mutex > &other)
 Exchanges ownership of the wrapped mutex with another lock. More...
 
MutexRelease ()
 Gives up ownership of the mutex and returns a pointer to it. More...
 
bool OwnsLock () const
 Checks whether the mutex is owned and locked. More...
 

Detailed Description

template<typename Mutex = embb::base::Mutex>
class embb::base::UniqueLock< Mutex >

Flexible ownership wrapper for a mutex.

Provides exception controlled locking of a mutex with non-recursive semantics, that gives more flexibility than LockGuard but also has slightly increased memory and processing overhead. Each instance of a UniqueLock can be used by one thread only!

Concurrency
Not thread-safe
See also
Mutex, LockGuard
Template Parameters
MutexUsed mutex type. Has to fulfil the Mutex Concept.

Constructor & Destructor Documentation

template<typename Mutex = embb::base::Mutex>
embb::base::UniqueLock< Mutex >::UniqueLock ( )

Creates a lock without assigned mutex.

A mutex can be assigned to the lock using the method Swap().

template<typename Mutex = embb::base::Mutex>
embb::base::UniqueLock< Mutex >::UniqueLock ( Mutex mutex)
explicit

Creates a lock from an unlocked mutex and locks it.

Precondition
mutex is unlocked
Parameters
[in]mutexMutex to be managed.
template<typename Mutex = embb::base::Mutex>
embb::base::UniqueLock< Mutex >::UniqueLock ( Mutex mutex,
DeferLockTag   
)

Creates a lock from an unlocked mutex without locking it.

Precondition
mutex is unlocked
Parameters
[in]mutexMutex to be managed
template<typename Mutex = embb::base::Mutex>
embb::base::UniqueLock< Mutex >::UniqueLock ( Mutex mutex,
TryLockTag   
)

Creates a lock from an unlocked mutex and tries to lock it.

Precondition
mutex is unlocked
Parameters
[in]mutexMutex to be managed
template<typename Mutex = embb::base::Mutex>
embb::base::UniqueLock< Mutex >::UniqueLock ( Mutex mutex,
AdoptLockTag   
)

Creates a lock from an already locked mutex.

Precondition
mutex is locked
Parameters
[in]mutexMutex to be managed
template<typename Mutex = embb::base::Mutex>
embb::base::UniqueLock< Mutex >::~UniqueLock ( )

Unlocks the mutex if owned.

Member Function Documentation

template<typename Mutex = embb::base::Mutex>
void embb::base::UniqueLock< Mutex >::Lock ( )

Waits until the mutex is unlocked and locks it.

Exceptions
ErrorException,ifno mutex is set or it is locked
template<typename Mutex = embb::base::Mutex>
bool embb::base::UniqueLock< Mutex >::TryLock ( )

Tries to lock the mutex and returns immediately.

Returns
true if the mutex could be locked, otherwise false.
Exceptions
ErrorException,ifno mutex is set
template<typename Mutex = embb::base::Mutex>
void embb::base::UniqueLock< Mutex >::Unlock ( )

Unlocks the mutex.

Exceptions
ErrorException,ifno mutex is set or it is not locked
template<typename Mutex = embb::base::Mutex>
void embb::base::UniqueLock< Mutex >::Swap ( UniqueLock< Mutex > &  other)

Exchanges ownership of the wrapped mutex with another lock.

Parameters
[in,out]otherThe lock to exchange ownership with
template<typename Mutex = embb::base::Mutex>
Mutex* embb::base::UniqueLock< Mutex >::Release ( )

Gives up ownership of the mutex and returns a pointer to it.

Returns
A pointer to the owned mutex or NULL, if no mutex was owned
template<typename Mutex = embb::base::Mutex>
bool embb::base::UniqueLock< Mutex >::OwnsLock ( ) const

Checks whether the mutex is owned and locked.

Returns
true if mutex is locked, otherwise false.