ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
timedlock.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::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::timed_mutex directly
16/// is that "spurious wake-ups" are detected and mitigated by this implementation.
17///
18/// When a pair of #Acquire and #Release invocations is performed within the same code block, then
19/// it is recommended to use a stack instantiation of class \alib{lang;Owner} to acquire and release
20/// objects of this class. Such a use is highly simplified with macros \ref ALIB_LOCK and
21/// \ref ALIB_LOCK_WITH.
22///
23/// This class does not allow nested calls to the method #Acquire - method #Release has to be
24/// invoked (from within the same thread that acquired this mutex), before any other thread can
25/// again gain access. Nested acquisitions constitute undefined behavior.
26///
27/// \par Debug-Features
28/// Public member #Dbg is available with debug-compilations. It offers the following features:
29/// - An assertion is raised when nested use is performed.
30/// - The object stores the actual owning thread and the source code position of the last
31/// acquirement.
32/// - Releasing non-acquired instances, as well as destructing acquired one, raise an assertion.
33/// - Field \alib{threads;DbgLockAsserter;WaitTimeLimit} enables the raise of \alib warnings
34/// in case a certain wait time is exceeded.
35///
36/// \par Availability
37/// This type is not available if the compiler-symbol \ref ALIB_SINGLE_THREADED is set.
38///
39/// @see
40/// - Chapter \ref alib_threads_locks of the Programmer's Manual of the module \alib_threads_nl.
41/// - Chapter \ref alib_manual_appendix_callerinfo of the General Programmer's Manual.
42//==================================================================================================
46#endif
47{
48 protected:
49 /// The internal object to lock on.
50 std::timed_mutex mutex;
51
52 public:
53 #if ALIB_DEBUG
54 /// The debug tool instance.
56 #endif
57
58 #if ALIB_DEBUG_CRITICAL_SECTIONS
59 /// @return \c true if the lock is acquired (in non-shared mode), \c false otherwise.
60 ALIB_DLL virtual bool DCSIsAcquired() const override;
61
62 /// @return \c true if the lock is shared-acquired (by at least any thread).
63 /// Otherwise, returns \c false.
64 ALIB_DLL virtual bool DCSIsSharedAcquired() const override;
65 #endif
66
67 #if ALIB_DEBUG || DOXYGEN
68 /// Same as #TryAcquireTimed(const Ticks::Duration&, const CallerInfo&)
69 /// but misses the parameter \p{waitTime}. Using this method, the behavior is equivalent to that
70 /// of sibling type \alib{threads;Lock}.
71 ///
72 /// \par Debug Parameter:
73 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
76
77 /// Tries to acquire this lock.
78 /// Multiple (nested) successful calls to this method or method #Acquire are not supported and
79 /// lead to undefined behavior.
80 ///
81 /// \par Debug Parameter:
82 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
83 /// @return \c true if the lock was not acquired by a different thread and thus, this
84 /// call was successful. \c false otherwise.
86 [[nodiscard]]
88
89 /// A thread which invokes this method gets registered as the current owner of this object,
90 /// until the same thread releases the ownership invoking #Release.
91 /// In the case that this object is already owned by another thread, the invoking thread is
92 /// suspended until ownership can be gained.
93 /// Multiple (nested) calls to this method are not supported and lead to undefined behavior.
94 ///
95 /// @param waitDuration The maximum time-span to wait.
96 /// @param ci Caller information.
97 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
98 /// @return \c true if the lock was acquired, \c false if the \p{waitDuration} expired
99 /// without successful acquisition
101 [[nodiscard]]
102 bool TryAcquireTimed( const Ticks::Duration& waitDuration, const CallerInfo& ci );
103
104 /// Same as overloaded sibling, but expects a C++ standard library duration type
105 /// rather than an \b Ticks::Duration.
106 /// @param waitDuration The point in time, when this method stops waiting.
107 /// @param ci Caller information.
108 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
109 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
110 /// without successful acquisition.
111 [[nodiscard]]
112 bool TryAcquireTimed( const Ticks::Duration::TDuration& waitDuration, const CallerInfo& ci )
113 { return TryAcquireTimed( Ticks::Duration(waitDuration), ci); }
114
115 /// Same as overloaded sibling, but expects a point in time rather than an \b Ticks::Duration.
116 /// @param pointInTime The point in time, when this method stops waiting.
117 /// @param ci Caller information.
118 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
119 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
120 /// without successful acquisition.
121 [[nodiscard]]
122 bool TryAcquireTimed( const Ticks& pointInTime, const CallerInfo& ci )
123 { return TryAcquireTimed( pointInTime - Ticks::Now(), ci); }
124
125 /// Same as overloaded sibling, but expects a C++ standard library point in time type
126 /// rather than an \b Ticks::Duration.
127 /// @param pointInTime The point in time, when this method stops waiting.
128 /// @param ci Caller information.
129 /// Use macro \ref ALIB_COMMA_CALLER_PRUNED with invocations.
130 /// @return \c true if the lock was acquired, \c false if the \p{pointInTime} was reached
131 /// without successful acquisition.
132 [[nodiscard]]
133 bool TryAcquireTimed( const Ticks::TTimePoint& pointInTime, const CallerInfo& ci )
134 { return TryAcquireTimed( Ticks(pointInTime), ci); }
135
136 /// Releases ownership of this object.
137 /// If this method is invoked on an object that is not acquired, in debug-compilations an
138 /// assertion is raised. In release compilations, this leads to undefined behavior.
139 /// \par Debug Parameter:
140 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
143
144 #else // release
145 void Acquire() { mutex.lock(); }
146 [[nodiscard]] bool TryAcquire() { return mutex.try_lock(); }
147 ALIB_DLL [[nodiscard]] bool TryAcquireTimed( const Ticks::Duration& waitDuration );
148 [[nodiscard]] bool TryAcquireTimed( const Ticks::Duration::TDuration& waitDuration ) { return TryAcquireTimed( Ticks::Duration(waitDuration) ); }
149 [[nodiscard]] bool TryAcquireTimed( const Ticks& pointInTime ) { return TryAcquireTimed( pointInTime - Ticks::Now() ); }
150 [[nodiscard]] bool TryAcquireTimed( const Ticks::TTimePoint& pointInTime ) { return TryAcquireTimed( Ticks(pointInTime) ); }
151 void Release() { mutex.unlock(); }
152
153 #endif
154};
155
156} // namespace alib[threads]
157
158/// Type alias in namespace \b alib.
160
161} // namespace [alib]
162#endif // !ALIB_SINGLE_THREADED
DbgLockAsserter Dbg
The debug tool instance.
Definition timedlock.inl:55
ALIB_DLL bool TryAcquireTimed(const Ticks::Duration &waitDuration, const CallerInfo &ci)
Definition locks.cpp:158
bool TryAcquireTimed(const Ticks::Duration::TDuration &waitDuration, const CallerInfo &ci)
ALIB_DLL void Release(ALIB_DBG_TAKE_CI)
Definition locks.cpp:174
bool TryAcquireTimed(const Ticks &pointInTime, const CallerInfo &ci)
virtual ALIB_DLL bool DCSIsSharedAcquired() const override
Definition locks.cpp:553
bool TryAcquireTimed(const Ticks::TTimePoint &pointInTime, const CallerInfo &ci)
virtual ALIB_DLL bool DCSIsAcquired() const override
Definition locks.cpp:552
std::timed_mutex mutex
The internal object to lock on.
Definition timedlock.inl:50
ALIB_DLL bool TryAcquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:148
ALIB_DLL void Acquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:119
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::TimedLock TimedLock
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