This class is a simple wrapper around C++ standard library type std::timed_mutex
. Thus, it is used to implement mutual exclusive access to resources by protecting critical code sections from being executed in parallel in concurrent threads.
With release-compilations, the only difference to using std::timed_mutex
directly is that "spurious wake-ups" are detected and mitigated by this implementation.
When a pair of Acquire and Release invocations is performed within the same code block, then it is recommended to use a stack instantiation of class Owner to acquire and release objects of this class. Such a use is highly simplified with macros ALIB_LOCK and ALIB_LOCK_WITH.
This class does not allow nested calls to the method Acquire - method Release has to be invoked (from within the same thread that acquired this mutex), before any other thread can again gain access. Nested acquisitions constitute undefined behavior.
Definition at line 55 of file timedlock.hpp.
#include <timedlock.hpp>
Public Field Index: | |
DbgLockAsserter | Dbg |
The debug tool instance. | |
Public Method Index: | |
~TimedLock () override | |
Destructor. With debug-compilations, asserts that this lock is not acquired. | |
ALIB_API void | Acquire (ALIB_DBG_TAKE_CI) |
virtual ALIB_API bool | DCSIsAcquired () const override |
virtual ALIB_API bool | DCSIsSharedAcquired () const override |
ALIB_API void | Release (ALIB_DBG_TAKE_CI) |
ALIB_API bool | TryAcquire (ALIB_DBG_TAKE_CI) |
bool | TryAcquireTimed (const Ticks &pointInTime, const CallerInfo &ci) |
ALIB_API bool | TryAcquireTimed (const Ticks::Duration &waitDuration, const CallerInfo &ci) |
bool | TryAcquireTimed (const Ticks::Duration::TDuration &waitDuration, const CallerInfo &ci) |
bool | TryAcquireTimed (const Ticks::TTimePoint &pointInTime, const CallerInfo &ci) |
Public Method Index: inherited from DbgCriticalSections::AssociatedLock | |
virtual | ~AssociatedLock () |
Virtual Destructor. | |
Protected Field Index: | |
std::timed_mutex | mutex |
The internal object to lock on. | |
DbgLockAsserter Dbg |
The debug tool instance.
Definition at line 67 of file timedlock.hpp.
|
protected |
The internal object to lock on.
Definition at line 62 of file timedlock.hpp.
|
inlineoverride |
Destructor. With debug-compilations, asserts that this lock is not acquired.
Definition at line 72 of file timedlock.hpp.
void Acquire | ( | ALIB_DBG_TAKE_CI | ) |
Same as TryAcquireTimed(const Ticks::Duration&, const CallerInfo&) but misses the parameter waitTime. Using this method, the behavior is equivalent to that of sibling type Lock.
Definition at line 391 of file locks.cpp.
|
overridevirtual |
true
if the lock is acquired (in non-shared mode), false
otherwise. Implements DbgCriticalSections::AssociatedLock.
|
overridevirtual |
true
if the lock is shared-acquired (by at least any thread). Otherwise, returns false
. Implements DbgCriticalSections::AssociatedLock.
void Release | ( | ALIB_DBG_TAKE_CI | ) |
Releases ownership of this object. If this method is invoked on an object that is not acquired, in debug-compilations an assertion is raised. In release compilations, this leads to undefined behavior.
Definition at line 460 of file locks.cpp.
|
nodiscard |
Tries to acquire this lock. Multiple (nested) successful calls to this method or method Acquire are not supported and lead to undefined behavior.
true
if the lock was not acquired by a different thread and thus, this call was successful. false
otherwise. Definition at line 425 of file locks.cpp.
|
inlinenodiscard |
Same as overloaded sibling, but expects a point in time rather than an Ticks::Duration.
pointInTime | The point in time, when this method stops waiting. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
true
if the lock was acquired, false
if the pointInTime was reached without successful acquisition. Definition at line 141 of file timedlock.hpp.
|
nodiscard |
A thread which invokes this method gets registered as the current owner of this object, until the same thread releases the ownership invoking Release. In the case that this object is already owned by another thread, the invoking thread is suspended until ownership can be gained. Multiple (nested) calls to this method are not supported and lead to undefined behavior.
waitDuration | The maximum time-span to wait. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
true
if the lock was acquired, false
if the waitDuration expired without successful acquisition Definition at line 439 of file locks.cpp.
|
inlinenodiscard |
Same as overloaded sibling, but expects a C++ standard library duration type rather than an Ticks::Duration.
waitDuration | The point in time, when this method stops waiting. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
true
if the lock was acquired, false
if the pointInTime was reached without successful acquisition. Definition at line 131 of file timedlock.hpp.
|
inlinenodiscard |
Same as overloaded sibling, but expects a C++ standard library point in time type rather than an Ticks::Duration.
pointInTime | The point in time, when this method stops waiting. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
true
if the lock was acquired, false
if the pointInTime was reached without successful acquisition. Definition at line 152 of file timedlock.hpp.