ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
lock.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::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.
15///
16/// When a pair of #Acquire and #Release invocations is performed within the same code block, then
17/// it is recommended to use a stack instantiation of class \alib{lang;Owner} to acquire and release
18/// objects of this class. Such a use is highly simplified with macros \ref ALIB_LOCK and
19/// \ref ALIB_LOCK_WITH.
20///
21/// This class does not allow nested calls to the method #Acquire - method #Release has to be
22/// invoked (from within the same thread that acquired this mutex), before any other thread can
23/// again gain access. Nested acquisitions constitute undefined behavior.
24///
25/// \par Debug-Features
26/// Public member #Dbg is available with debug-compilations. It offers the following features:
27/// - An assertion is raised when nested use is performed.
28/// - The object stores the actual owning thread and the source code position of the last
29/// acquirement.
30/// - Releasing non-acquired instances, as well as destructing acquired one, raise an assertion.
31/// - Field \alib{threads::DbgLockAsserter;WaitTimeLimit} enables the raise of \alib warnings in
32/// case a certain wait time is exceeded.
33/// Note that instead of wrapping \c std::mutex, with debug-compilations class
34/// \c std::timed_mutex is wrapped.
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/// - For this class, as well as for all other five lock types of this module, a
43/// \ref alibtools_debug_helpers_gdb "pretty printer" for the GNU debugger is provided.
44//==================================================================================================
45class Lock
48#endif
49{
50 protected:
51 #if !ALIB_DEBUG && !DOXYGEN
52 std::mutex mutex; // the only member in release compilations
53
54 #else
55 #if DOXYGEN
56 /// The internal object to lock on.
57 /// \note With debug-compilations, this is of type <c>std::timed_mutex</c>.
58 std::mutex mutex;
59 #else
60 std::timed_mutex mutex;
61 #endif
62
63 public:
64 /// The debug tool instance.
66 #endif
67
68 public:
69
70 #if ALIB_DEBUG_CRITICAL_SECTIONS
71 /// @return \c true if the lock is acquired (in non-shared mode), \c false otherwise.
72 ALIB_DLL virtual bool DCSIsAcquired() const override;
73
74 /// @return \c true if the lock is shared-acquired (by at least any thread).
75 /// Otherwise, returns \c false.
76 ALIB_DLL virtual bool DCSIsSharedAcquired() const override;
77 #endif
78
79 #if ALIB_DEBUG || DOXYGEN
80 /// Acquires this lock.
81 /// In the case that this object is already owned by another thread, the invoking thread is
82 /// suspended until ownership can be gained.
83 /// Multiple (nested) calls to this method are not supported and lead to undefined behavior.
84 ///
85 /// \par Debug Parameter:
86 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
89
90 /// Tries to acquire this lock.
91 /// Multiple (nested) successful calls to this method or method #Acquire are not supported and
92 /// lead to undefined behavior.
93 ///
94 /// \par Debug Parameter:
95 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
96 ///
97 /// @return \c true if the lock was not acquired by a different thread and thus, this
98 /// call was successful. \c false otherwise.
100 [[nodiscard]]
102
103 /// Releases ownership of this object.
104 /// If this method is invoked on an object that is not acquired, in debug-compilations an
105 /// assertion is raised.
106 /// In release compilations, this leads to undefined behavior.
107 ///
108 /// \par Debug Parameter:
109 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
112 #else
113 void Acquire() { mutex.lock(); }
114 [[nodiscard]] bool TryAcquire() { return mutex.try_lock(); }
115 void Release() { mutex.unlock(); }
116 #endif
117};
118
120
121} // namespace alib[threads]
122
123/// Type alias in namespace \b alib.
125
126} // namespace [alib]
127#endif // !ALIB_SINGLE_THREADED
virtual ALIB_DLL bool DCSIsAcquired() const override
Definition locks.cpp:550
virtual ALIB_DLL bool DCSIsSharedAcquired() const override
Definition locks.cpp:551
DbgLockAsserter Dbg
The debug tool instance.
Definition lock.inl:65
std::mutex mutex
Definition lock.inl:58
ALIB_DLL void Release(ALIB_DBG_TAKE_CI)
Definition locks.cpp:93
ALIB_DLL bool TryAcquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:83
ALIB_DLL void Acquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:54
#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
ALIB_DLL Lock STD_IOSTREAMS_LOCK
Definition locks.cpp:48
threads::Lock Lock
Type alias in namespace alib.
Definition lock.inl:124