ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
SharedLock Class Reference

Description:

This class is a simple wrapper around C++ standard library type std::shared_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, while allowing typical read-operations to continue to be executed in parallel.

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.
The same is recommended for paired invocations of AcquireShared and ReleaseShared. Here, class OwnerShared is to be used, best using macros ALIB_LOCK_SHARED and ALIB_LOCK_SHARED_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.

Typically macros ALIB_LOCK and ALIB_LOCK_WITH should be used to acquire this lock in non-shared mode and macros ALIB_LOCK_SHARED and ALIB_LOCK_SHARED_WITH for shared acquisitions.

Debug-Features
Public member Dbg is available with debug-compilations. It offers the following features:
  • An assertion is raised when nested use is performed.
  • The object stores the actual owning thread and the source code position of the last acquirement. Likewise the last shared acquirement's caller information is stored.
  • Releasing non-acquired instances, as well as destructing acquired one, raise an assertion.
  • Field WaitTimeLimit enables the raise of ALib warnings in case a certain wait time is exceeded. Note that instead of wrapping std::shared_mutex, with debug-compilations class std::shared_timed_mutex is wrapped.
  • Field DbgWarningMaximumShared enables the raise of ALib warnings in the case that the number of parallel shared acquirements reaches the limit given with this member.
See also

Definition at line 66 of file sharedlock.hpp.

#include <sharedlock.hpp>

Inheritance diagram for SharedLock:
[legend]
Collaboration diagram for SharedLock:
[legend]

Public Field Index:

DbgSharedLockAsserter Dbg
 The debug tool instance.
 
std::atomic< int > DbgWarningMaximumShared =10
 

Public Method Index:

 ~SharedLock () override
 Destructor. With debug-compilations, asserts that this lock is not acquired.
 
ALIB_API void Acquire (ALIB_DBG_TAKE_CI)
 
ALIB_API void AcquireShared (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 void ReleaseShared (ALIB_DBG_TAKE_CI)
 
ALIB_API bool TryAcquire (ALIB_DBG_TAKE_CI)
 
ALIB_API bool TryAcquireShared (ALIB_DBG_TAKE_CI)
 
- Public Method Index: inherited from DbgCriticalSections::AssociatedLock
virtual ~AssociatedLock ()
 Virtual Destructor.
 

Protected Field Index:

std::shared_mutex mutex
 

Field Details:

◆ Dbg

The debug tool instance.

Definition at line 86 of file sharedlock.hpp.

◆ DbgWarningMaximumShared

std::atomic<int> DbgWarningMaximumShared =10

Warning-threshold of maximum number of parallel shared acquisitions.
Defaults to 10.

Definition at line 90 of file sharedlock.hpp.

◆ mutex

std::shared_mutex mutex
protected

The internal object to lock on.

Note
With debug-compilations, this is of type std::timed_mutex.

Definition at line 79 of file sharedlock.hpp.

Constructor(s) / Destructor Details:

◆ ~SharedLock()

~SharedLock ( )
inlineoverride

Destructor. With debug-compilations, asserts that this lock is not acquired.

Definition at line 96 of file sharedlock.hpp.

Here is the call graph for this function:

Method Details:

◆ Acquire()

void Acquire ( ALIB_DBG_TAKE_CI )

Acquires this lock. 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.

Debug Parameter:
Pass macro ALIB_CALLER_PRUNED with invocations.

Definition at line 653 of file locks.cpp.

Here is the call graph for this function:

◆ AcquireShared()

void AcquireShared ( ALIB_DBG_TAKE_CI )

Acquires this lock in shared mode. In the case that this object is already owned (not shared) 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.

Debug Parameter:
Pass macro ALIB_CALLER_PRUNED with invocations.

Definition at line 713 of file locks.cpp.

Here is the call graph for this function:

◆ DCSIsAcquired()

bool DCSIsAcquired ( ) const
overridevirtual
Returns
true if the lock is acquired (in non-shared mode), false otherwise.

Implements DbgCriticalSections::AssociatedLock.

Definition at line 282 of file locks.cpp.

◆ DCSIsSharedAcquired()

bool DCSIsSharedAcquired ( ) const
overridevirtual
Returns
true if the lock is shared-acquired (by at least any thread). Otherwise, returns false.

Implements DbgCriticalSections::AssociatedLock.

Definition at line 284 of file locks.cpp.

◆ Release()

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.

Debug Parameter:
Pass macro ALIB_COMMA_CALLER_PRUNED with invocations.

Definition at line 700 of file locks.cpp.

Here is the call graph for this function:

◆ ReleaseShared()

void ReleaseShared ( ALIB_DBG_TAKE_CI )

Releases shared ownership of this object. Invoking this method on an object that is not "shared acquired" by this thread constitutes undefined behavior.
In debug-compilations the overall sum (of any thread) of shared acquirements and releases is counted, and an assertion is raised if more releases than acquisitions are performed.

Debug Parameter:
Pass macro ALIB_CALLER_PRUNED with invocations.

Definition at line 769 of file locks.cpp.

Here is the call graph for this function:

◆ TryAcquire()

bool TryAcquire ( ALIB_DBG_TAKE_CI )
nodiscard

Tries to acquire this lock. Multiple (nested) successful calls to this method or method Acquire are not supported and lead to undefined behavior.

Debug Parameter:
Pass macro ALIB_CALLER_PRUNED with invocations.
Returns
true if the lock was not acquired by a different thread and thus, this call was successful. false otherwise.

Definition at line 687 of file locks.cpp.

Here is the call graph for this function:

◆ TryAcquireShared()

bool TryAcquireShared ( ALIB_DBG_TAKE_CI )
nodiscard

Tries to acquire this lock. Multiple (nested) calls to this method or method AcquireShared from within the same thread are not supported and lead to undefined behavior.

Debug Parameter:
Pass macro ALIB_CALLER_PRUNED with invocations.
Returns
true if the lock was not acquired by a different thread and thus, this call was successful. false otherwise.

Definition at line 751 of file locks.cpp.

Here is the call graph for this function:

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