ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
sharedtimedlock.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::shared_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, while
15/// allowing typical read-operations to continue to be executed in parallel. <br>
16/// With release-compilations, the only difference to using \c std::shared_timed_mutex directly
17/// is that "spurious wake-ups" are detected and mitigated by this implementation.
18///
19/// When a pair of #Acquire and #Release invocations is performed within the same code block, then
20/// it is recommended to use a stack instantiation of class \alib{lang;Owner} to acquire and release
21/// objects of this class.
22/// Such a use is highly simplified with macros \ref ALIB_LOCK and \ref ALIB_LOCK_WITH. <br>
23/// The same is recommended for paired invocations of #AcquireShared and #ReleaseShared.
24/// Here, class \alib{lang;OwnerShared} is to be used, best using macros \ref ALIB_LOCK_SHARED and
25/// \ref ALIB_LOCK_SHARED_WITH.
26///
27/// This class does not allow nested calls to the method #Acquire - method #Release has to be
28/// invoked (from within the same thread that acquired this mutex), before any other thread can
29/// again gain access. Nested acquisitions constitute undefined behavior.
30///
31/// \par Debug-Features
32/// Public member #Dbg is available with debug-compilations. It offers the following features:
33/// - An assertion is raised when nested use is performed.
34/// - The object stores the actual owning thread and the source code position of the last
35/// acquirement.
36/// Likewise the last shared acquirement's caller information is stored.
37/// - Releasing non-acquired instances, as well as destructing acquired one,
38/// \ref alib_mod_assert "raises an ALib error".
39/// - Field \alib{threads;DbgLockAsserter;WaitTimeLimit} the raise of \alib warnings in case a
40/// certain wait time is exceeded.
41/// - Field #DbgWarningMaximumShared enables the raise of \alib warnings in the case that
42/// the number of parallel shared acquirements reaches the limit given with this member.
43///
44/// \par Availability
45/// This type is not available if the compiler-symbol \ref ALIB_SINGLE_THREADED is set.
46///
47/// @see
48/// - Chapter \ref alib_threads_locks of the Programmer's Manual of the module \alib_threads_nl.
49/// - Chapter \ref alib_manual_appendix_callerinfo of the General Programmer's Manual.
50//==================================================================================================
54#endif
55{
56 protected:
57 /// The internal object to lock on.
58 std::shared_timed_mutex mutex;
59
60 public:
61 #if ALIB_DEBUG
62 /// The debug tool instance.
64
65 /// Warning-threshold of maximum number of parallel shared acquisitions.<br>
66 /// Defaults to 10.
67 std::atomic<int> DbgWarningMaximumShared =10;
68
69 #if ALIB_DEBUG_CRITICAL_SECTIONS
70 /// @return \c true if the lock is acquired (in non-shared mode), \c false otherwise.
71 ALIB_DLL virtual bool DCSIsAcquired() const override;
72
73 /// @return \c true if the lock is shared-acquired (by at least any thread).
74 /// Otherwise, returns \c false.
75 ALIB_DLL virtual bool DCSIsSharedAcquired() const override;
76 #endif
77 #endif
78
79 //================================================================================================
80 // ==== Standard Acquire/Release (Writer)
81 //================================================================================================
82
83 #if ALIB_DEBUG || DOXYGEN
84 /// Same as #TryAcquireTimed(const Ticks::Duration&, const CallerInfo&)
85 /// but misses the parameter \p{waitTime}. Using this method, the behavior is equivalent to that
86 /// of sibling type \alib{threads;Lock}.
87 ///
88 /// \par Debug Parameter:
89 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
92
93 /// Tries to acquire this lock.
94 /// Multiple (nested) successful calls to this method or method #Acquire are not supported and
95 /// lead to undefined behavior.
96 ///
97 /// \par Debug Parameter:
98 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
99 /// @return \c true if the lock was not acquired by a different thread and thus, this
100 /// call was successful. \c false otherwise.
102 [[nodiscard]]
104
105 /// A thread which invokes this method gets registered as the current owner of this object,
106 /// until the same thread releases the ownership invoking #Release.
107 /// In the case that this object is already owned by another thread, the invoking thread is
108 /// suspended until ownership can be gained.
109 /// Multiple (nested) calls to this method are not supported and lead to undefined behavior.
110 ///
111 /// @param waitDuration The maximum time-span to wait.
112 /// @param ci Caller information.
113 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
114 /// @return \c true if the lock was acquired, \c false if the \p{waitDuration} expired
115 /// without successful acquisition
117 [[nodiscard]]
118 bool TryAcquireTimed( const Ticks::Duration& waitDuration, const CallerInfo& ci );
119
120 /// Same as overloaded sibling, but expects a C++ standard library duration type
121 /// rather than an \b Ticks::Duration.
122 ///
123 /// @param waitDuration The point in time, when this method stops waiting.
124 /// @param ci Caller information.
125 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
126 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
127 /// without successful acquisition.
128 [[nodiscard]]
129 bool TryAcquireTimed( const Ticks::Duration::TDuration& waitDuration, const CallerInfo& ci )
130 { return TryAcquireTimed( Ticks::Duration(waitDuration), ci); }
131
132 /// Same as overloaded sibling, but expects a point in time rather than an \b Ticks::Duration.
133 ///
134 /// @param pointInTime The point in time, when this method stops waiting.
135 /// @param ci Caller information.
136 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
137 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
138 /// without successful acquisition.
139 [[nodiscard]]
140 bool TryAcquireTimed( const Ticks& pointInTime, const CallerInfo& ci )
141 { return TryAcquireTimed( pointInTime - Ticks::Now(), ci); }
142
143 /// Same as overloaded sibling, but expects a C++ standard library point in time type
144 /// rather than an \b Ticks::Duration.
145 /// \par Debug Parameter:
146 /// Pass macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
147 ///
148 /// @param pointInTime The point in time, when this method stops waiting.
149 /// @param ci Caller information.
150 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
151 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
152 /// without successful acquisition.
153 [[nodiscard]]
154 bool TryAcquireTimed( const Ticks::TTimePoint& pointInTime, const CallerInfo& ci )
155 { return TryAcquireTimed( Ticks(pointInTime), ci); }
156
157 /// Releases ownership of this object.
158 /// If this method is invoked on an object that is not acquired, in debug-compilations an
159 /// assertion is raised. In release compilations, this leads to undefined behavior.
160 /// \par Debug Parameter:
161 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
164
165 #else // release
166 void Acquire() { mutex.lock(); }
167 [[nodiscard]] bool TryAcquire() { return mutex.try_lock(); }
168 ALIB_DLL [[nodiscard]] bool TryAcquireTimed( const Ticks::Duration& waitDuration );
169 [[nodiscard]] bool TryAcquireTimed( const Ticks::Duration::TDuration& waitDuration ) { return TryAcquireTimed( Ticks::Duration(waitDuration) ); }
170 [[nodiscard]] bool TryAcquireTimed( const Ticks& pointInTime ) { return TryAcquireTimed( pointInTime - Ticks::Now() ); }
171 [[nodiscard]] bool TryAcquireTimed( const Ticks::TTimePoint& pointInTime ) { return TryAcquireTimed( Ticks(pointInTime) ); }
172 void Release() { mutex.unlock(); }
173
174 #endif
175
176 //================================================================================================
177 // ==== Shared Acquire/Release (Reader)
178 //================================================================================================
179
180 #if ALIB_DEBUG || DOXYGEN
181 /// Same as #TryAcquireSharedTimed(const Ticks::Duration&, const CallerInfo&)
182 /// but misses the parameter \p{waitTime}.
183 /// Using this method, the behavior is equivalent to that of sibling type
184 /// \alib{threads;SharedLock}.
185 ///
186 /// \par Debug Parameter:
187 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
190
191 /// Tries to acquire this lock in shared mode.
192 /// Multiple (nested) calls to this method or method #AcquireShared from within the same thread
193 /// are not supported and lead to undefined behavior.
194 ///
195 /// \par Debug Parameter:
196 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
197 /// @return \c true if the lock was not acquired by a different thread and thus, this
198 /// call was successful. \c false otherwise.
200 [[nodiscard]]
202
203 /// Same as method #TryAcquireShared(ALIB_DBG_TAKE_CI), but
204 /// accepts a maximum wait time as first parameter.
205 ///
206 /// @param waitDuration The maximum time-span to wait.
207 /// @param ci Caller information.
208 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
209 /// @return \c true if the lock was acquired, \c false if the \p{waitDuration} expired
210 /// without successful acquisition
212 [[nodiscard]]
213 bool TryAcquireSharedTimed( const Ticks::Duration& waitDuration, const CallerInfo& ci );
214
215 /// Same as overloaded sibling, but expects a C++ standard library duration type
216 /// rather than an \b Ticks::Duration.
217 ///
218 /// @param waitDuration The point in time, when this method stops waiting.
219 /// @param ci Caller information. Available only with debug-builds.
220 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
221 /// without successful acquisition.
222 [[nodiscard]]
223 bool TryAcquireSharedTimed(const Ticks::Duration::TDuration& waitDuration, const CallerInfo& ci)
224 { return TryAcquireSharedTimed( Ticks::Duration(waitDuration), ci); }
225
226 /// Same as overloaded sibling, but expects a point in time rather than an \b Ticks::Duration.
227 ///
228 /// @param pointInTime The point in time, when this method stops waiting.
229 /// @param ci Caller information.
230 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
231 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
232 /// without successful acquisition.
233 [[nodiscard]]
234 bool TryAcquireSharedTimed( const Ticks& pointInTime, const CallerInfo& ci )
235 { return TryAcquireSharedTimed( pointInTime - Ticks::Now(), ci); }
236
237 /// Same as overloaded sibling, but expects a C++ standard library point in time type
238 /// rather than a \b Ticks::Duration value.
239 ///
240 /// @param pointInTime The point in time, when this method stops waiting.
241 /// @param ci Caller information.
242 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
243 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
244 /// without successful acquisition.
245 [[nodiscard]]
246 bool TryAcquireSharedTimed( const Ticks::TTimePoint& pointInTime, const CallerInfo& ci )
247 { return TryAcquireSharedTimed( Ticks(pointInTime), ci); }
248
249 #else // release
250 void AcquireShared() { mutex.lock_shared(); }
251 [[nodiscard]] bool TryAcquireShared() { return mutex.try_lock_shared(); }
252 ALIB_DLL [[nodiscard]] bool TryAcquireSharedTimed( const Ticks::Duration& waitDuration );
253 [[nodiscard]] bool TryAcquireSharedTimed( const Ticks::Duration::TDuration& waitDuration ) { return TryAcquireSharedTimed( Ticks::Duration(waitDuration) ); }
254 [[nodiscard]] bool TryAcquireSharedTimed( const Ticks& pointInTime ) { return TryAcquireSharedTimed( pointInTime - Ticks::Now() ); }
255 [[nodiscard]] bool TryAcquireSharedTimed( const Ticks::TTimePoint& pointInTime ) { return TryAcquireSharedTimed( Ticks(pointInTime) ); }
256 #endif
257
258 #if ALIB_DEBUG || DOXYGEN
259 /// Releases shared ownership of this object.
260 /// Invoking this method on an object that is not "shared acquired" by this thread constitutes
261 /// undefined behavior.<br>
262 /// In debug-compilations the overall sum (of any thread) of shared acquirements and releases
263 /// is counted, and an \ref alib_mod_assert "error is raised" if more releases than
264 /// acquisitions are performed.
265 /// \par Debug Parameter:
266 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
269 #else
270 void ReleaseShared() { mutex.unlock_shared(); }
271 #endif
272};
273
274} // namespace alib[threads]
275
276/// Type alias in namespace \b alib.
278
279} // namespace [alib]
280#endif // !ALIB_SINGLE_THREADED
virtual ALIB_DLL bool DCSIsSharedAcquired() const override
Definition locks.cpp:562
DbgSharedLockAsserter Dbg
The debug tool instance.
std::shared_timed_mutex mutex
The internal object to lock on.
bool TryAcquireSharedTimed(const Ticks::Duration::TDuration &waitDuration, const CallerInfo &ci)
virtual ALIB_DLL bool DCSIsAcquired() const override
Definition locks.cpp:561
bool TryAcquireSharedTimed(const Ticks::TTimePoint &pointInTime, const CallerInfo &ci)
ALIB_DLL void AcquireShared(ALIB_DBG_TAKE_CI)
Definition locks.cpp:482
bool TryAcquireTimed(const Ticks &pointInTime, const CallerInfo &ci)
ALIB_DLL void Acquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:403
ALIB_DLL bool TryAcquireShared(ALIB_DBG_TAKE_CI)
Definition locks.cpp:512
bool TryAcquireTimed(const Ticks::Duration::TDuration &waitDuration, const CallerInfo &ci)
std::atomic< int > DbgWarningMaximumShared
bool TryAcquireSharedTimed(const Ticks &pointInTime, const CallerInfo &ci)
bool TryAcquireTimed(const Ticks::TTimePoint &pointInTime, const CallerInfo &ci)
ALIB_DLL void ReleaseShared(ALIB_DBG_TAKE_CI)
Definition locks.cpp:541
ALIB_DLL bool TryAcquireTimed(const Ticks::Duration &waitDuration, const CallerInfo &ci)
Definition locks.cpp:441
ALIB_DLL bool TryAcquireSharedTimed(const Ticks::Duration &waitDuration, const CallerInfo &ci)
Definition locks.cpp:523
ALIB_DLL void Release(ALIB_DBG_TAKE_CI)
Definition locks.cpp:458
ALIB_DLL bool TryAcquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:431
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
threads::SharedTimedLock SharedTimedLock
Type alias in namespace alib.
lang::CallerInfo CallerInfo
Type alias in namespace alib.
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:79