ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
recursivetimedlock.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_threads of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#if !ALIB_SINGLE_THREADED
9ALIB_EXPORT namespace alib { namespace threads {
10
11//==================================================================================================
12/// This class is a simple wrapper around C++ standard library type \c std::recursive_timed_mutex.
13/// Thus, it is used to implement <em>mutual exclusive access</em> to resources by protecting
14/// critical code sections from being executed in parallel in concurrent threads.<br>
15/// With release-compilations, the only difference to using \c std::recursive_timed_mutex directly
16/// is that "spurious wake-ups" are detected and mitigated by this implementation.
17///
18/// code block, then it is recommended to use a stack instantiation of class
19/// \alib{lang;OwnerRecursive} to acquire and release objects of this class.
20/// Such a use is highly simplified with macros \ref ALIB_LOCK_RECURSIVE and
21/// \ref ALIB_LOCK_RECURSIVE_WITH.
22///
23/// Nested acquisitions are supported with this type.
24/// An instance of this class is released when an equal number of invocations to #AcquireRecursive
25/// and #ReleaseRecursive have been performed.<br>
26/// This class has slightly reduced performance in comparison to non-recursive type
27/// \alib{threads;Lock}. Not only for this reason, non-nested locking is the preferred technique.
28/// But there are situations where nested locks are just unavoidable.
29///
30/// \par Debug-Features
31/// Public member #Dbg is available with debug-compilations. It offers the following features:
32/// - The object stores the actual owning thread and the source code position of the last
33/// acquirement.
34/// - Releasing non-acquired instances, as well as destructing acquired one, raise an assertion.
35/// - A warning threshold for the number of nested acquisitions can be defined with public member
36/// \alib{threads;DbgLockAsserter::RecursionLimit}.
37/// - Field \alib{threads;DbgLockAsserter;WaitTimeLimit} enables the raise of \alib warnings in case a
38/// certain wait time is exceeded.
39///
40/// \par Availability
41/// This type is not available if the compiler-symbol \ref ALIB_SINGLE_THREADED is set.
42///
43/// @see
44/// - Chapter \ref alib_threads_locks of the Programmer's Manual of the module \alib_threads_nl.
45/// - Chapter \ref alib_manual_appendix_callerinfo of the General Programmer's Manual.
46//==================================================================================================
50#endif
51{
52 protected:
53 /// The internal object to lock on.
54 std::recursive_timed_mutex mutex; // the only member in release compilations
55
56 public:
57 #if ALIB_DEBUG
58 /// The debug tool instance.
60 #endif
61
62 #if ALIB_DEBUG_CRITICAL_SECTIONS
63 /// @return \c true if the lock is acquired (in non-shared mode), \c false otherwise.
64 ALIB_DLL virtual bool DCSIsAcquired() const override;
65
66 /// @return \c true if the lock is shared-acquired (by at least any thread).
67 /// Otherwise, returns \c false.
68 ALIB_DLL virtual bool DCSIsSharedAcquired() const override;
69 #endif
70
71 //################################################################################################
72 // Interface
73 //################################################################################################
74 public:
75
76 #if ALIB_DEBUG || DOXYGEN
77 /// Thread which invokes this method gets registered as the current owner of this object,
78 /// until the same thread releases the ownership invoking #ReleaseRecursive.
79 /// In the case that this object is already owned by another thread, the invoking thread is
80 /// suspended until ownership can be gained.
81 /// Multiple (nested) calls to this method are counted and the object is only released when
82 /// the same number of Release() calls have been made.
83 ///
84 /// \par Debug Parameter:
85 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
88
89 /// Tries to acquire this lock.
90 /// Successful calls to this method are counted, as if #AcquireRecursive was called and an
91 /// according invocation of #ReleaseRecursive has to be performed.
92 ///
93 /// \par Debug Parameter:
94 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
95 ///
96 /// @return \c true if the lock was not acquired by a different thread and thus, this
97 /// call was successful. \c false otherwise.
99 [[nodiscard]]
101
102 /// A thread which invokes this method gets registered as the current owner of this object,
103 /// until the same thread releases the ownership invoking #ReleaseRecursive.
104 /// In the case that this object is already owned by another thread, the invoking thread is
105 /// suspended until ownership can be gained.<br>
106 /// Successful calls to this method are counted, as if #AcquireRecursive was called and an
107 /// according invocation of #ReleaseRecursive has to be performed.
108 ///
109 /// @param waitDuration The maximum time-span to wait.
110 /// @param ci Caller information.
111 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
112 /// @return \c true if the lock was acquired, \c false if the \p{waitDuration} expired
113 /// without successful acquisition
115 [[nodiscard]]
116 bool TryAcquireTimed( const Ticks::Duration& waitDuration, const CallerInfo& ci );
117
118 /// Same as overloaded sibling, but expects a C++ standard library duration type
119 /// rather than an \b Ticks::Duration.
120 ///
121 /// @param waitDuration The point in time, when this method stops waiting.
122 /// @param ci Caller information.
123 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
124 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
125 /// without successful acquisition.
126 [[nodiscard]]
127 bool TryAcquireTimed( const Ticks::Duration::TDuration& waitDuration, const CallerInfo& ci )
128 { return TryAcquireTimed( Ticks::Duration(waitDuration), ci); }
129
130 /// Same as overloaded sibling, but expects a point in time rather than an \b Ticks::Duration.
131 /// @param pointInTime The point in time, when this method stops waiting.
132 /// @param ci Caller information.
133 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
134 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
135 /// without successful acquisition.
136 [[nodiscard]]
137 bool TryAcquireTimed( const Ticks& pointInTime, const CallerInfo& ci )
138 { return TryAcquireTimed( pointInTime - Ticks::Now(), ci); }
139
140 /// Same as overloaded sibling, but expects a C++ standard library point in time type
141 /// rather than an \b Ticks::Duration.
142 ///
143 /// @param pointInTime The point in time, when this method stops waiting.
144 /// @param ci Caller information.
145 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
146 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
147 /// without successful acquisition.
148 [[nodiscard]]
149 bool TryAcquireTimed( const Ticks::TTimePoint& pointInTime, const CallerInfo& ci )
150 { return TryAcquireTimed( Ticks(pointInTime), ci); }
151
152 #else
153 void AcquireRecursive() { mutex.lock(); }
154 [[nodiscard]] bool TryAcquire() { return mutex.try_lock(); }
155 ALIB_DLL [[nodiscard]] bool TryAcquireTimed( const Ticks::Duration& waitDuration );
156 [[nodiscard]] bool TryAcquireTimed( const Ticks::Duration::TDuration& waitDuration ) { return TryAcquireTimed( Ticks::Duration(waitDuration) ); }
157 [[nodiscard]] bool TryAcquireTimed( const Ticks& pointInTime ) { return TryAcquireTimed( pointInTime - Ticks::Now() ); }
158 [[nodiscard]] bool TryAcquireTimed( const Ticks::TTimePoint& pointInTime ) { return TryAcquireTimed( Ticks(pointInTime) ); }
159 #endif
160
161 #if ALIB_DEBUG || DOXYGEN
162 /// Releases ownership of this object. If #AcquireRecursive was called multiple times before,
163 /// the same number of calls to this method has to be performed to release ownership.
164 /// \par Debug Parameter:
165 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
168 #else
169 void ReleaseRecursive() { mutex.unlock(); }
170 #endif
171};
172
173
174} // namespace alib[threads]
175
176/// Type alias in namespace \b alib.
178
179} // namespace [alib]
180#endif // !ALIB_SINGLE_THREADED
ALIB_DLL void ReleaseRecursive(ALIB_DBG_TAKE_CI)
Definition locks.cpp:287
DbgLockAsserter Dbg
The debug tool instance.
virtual ALIB_DLL bool DCSIsAcquired() const override
Definition locks.cpp:556
std::recursive_timed_mutex mutex
The internal object to lock on.
virtual ALIB_DLL bool DCSIsSharedAcquired() const override
Definition locks.cpp:557
ALIB_DLL bool TryAcquireTimed(const Ticks::Duration &waitDuration, const CallerInfo &ci)
Definition locks.cpp:272
ALIB_DLL void AcquireRecursive(ALIB_DBG_TAKE_CI)
Definition locks.cpp:237
ALIB_DLL bool TryAcquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:264
bool TryAcquireTimed(const Ticks::TTimePoint &pointInTime, const CallerInfo &ci)
bool TryAcquireTimed(const Ticks &pointInTime, const CallerInfo &ci)
bool TryAcquireTimed(const Ticks::Duration::TDuration &waitDuration, const CallerInfo &ci)
typename std::chrono::steady_clock::time_point TTimePoint
#define ALIB_DBG_TAKE_CI
Definition alib.inl:1030
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_DEBUG_CRITICAL_SECTIONS
Definition prepro.dox.md:43
lang::CallerInfo CallerInfo
Type alias in namespace alib.
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:79
threads::RecursiveTimedLock RecursiveTimedLock
Type alias in namespace alib.