ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
lock.hpp
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-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_THREADS_LOCK
9#define HPP_ALIB_THREADS_LOCK 1
10#pragma once
11#if !defined(DOXYGEN)
12# include "alib/alib.hpp"
13#endif
14ALIB_ASSERT_MODULE(THREADS)
15#if ALIB_DEBUG
17#endif
18#if ALIB_DEBUG_CRITICAL_SECTIONS
20#endif
21
22#include <mutex>
23
24
25namespace alib { namespace threads {
26// =================================================================================================
27/// This class is a simple wrapper around C++ standard library type \c std::mutex.
28/// Thus, it is used to implement <em>mutual exclusive access</em> to resources by protecting
29/// critical code sections from being executed in parallel in concurrent threads.
30///
31/// When a pair of #Acquire and #Release invocations is performed within the same code block, then
32/// it is recommended to use a stack instantiation of class \alib{lang;Owner} to acquire and release
33/// objects of this class. Such a use is highly simplified with macros \ref ALIB_LOCK and
34/// \ref ALIB_LOCK_WITH.
35///
36/// This class does not allow nested calls to the method #Acquire - method #Release has to be
37/// invoked (from within the same thread that acquired this mutex), before any other thread can
38/// again gain access. Nested acquisitions constitute undefined behavior.
39///
40/// \par Debug-Features
41/// Public member #Dbg is available with debug-compilations. It offers the following features:
42/// - An assertion is raised when nested use is performed.
43/// - The object stores the actual owning thread and the source code position of the last
44/// acquirement.
45/// - Releasing non-acquired instances, as well as destructing acquired one, raise an assertion.
46/// - Field \alib{threads::DbgLockAsserter;WaitTimeLimit} enables the raise of \alib warnings in
47/// case a certain wait time is exceeded.
48/// Note that instead of wrapping \c std::mutex, with debug-compilations class
49/// \c std::timed_mutex is wrapped.
50///
51///
52/// @see
53/// - Chapter \ref alib_threads_locks of the Programmer's Manual of the module \alib_threads_nl.
54/// - Chapter \ref alib_manual_appendix_callerinfo of the General Programmer's Manual.
55/// - For this class, as well as for all other five lock types of this module, a
56/// \ref alibtools_debug_helpers_gdb "pretty printer" for the GNU debugger is provided.
57// =================================================================================================
58class Lock
61#endif
62{
63 protected:
64 #if !ALIB_DEBUG && !DOXYGEN
65 std::mutex mutex; // the only member in release compilations
66
67 #else
68 #if DOXYGEN
69 /// The internal object to lock on.
70 /// \note With debug-compilations, this is of type <c>std::timed_mutex</c>.
71 std::mutex mutex;
72 #else
73 std::timed_mutex mutex;
74 #endif
75
76 public:
77 /// The debug tool instance.
79 #endif
80
81 public:
82
83 #if ALIB_DEBUG_CRITICAL_SECTIONS
84 /// Destructor. With debug-compilations, asserts that this lock is not acquired.
85 ~Lock() override
86 { Dbg.AssertNotOwned( ALIB_CALLER, ALIB_CALLER, "Destructing acquired lock" ); }
87
88 /// @return \c true if the lock is acquired (in non-shared mode), \c false otherwise.
89 ALIB_API virtual bool DCSIsAcquired() const override;
90
91 /// @return \c true if the lock is shared-acquired (by at least any thread).
92 /// Otherwise, returns \c false.
93 ALIB_API virtual bool DCSIsSharedAcquired() const override;
94
95 #elif ALIB_DEBUG
96 ~Lock()
97 { Dbg.AssertNotOwned( ALIB_CALLER, ALIB_CALLER, "Destructing acquired lock" ); }
98 #endif
99
100 #if ALIB_DEBUG || DOXYGEN
101 /// Acquires this lock.
102 /// In the case that this object is already owned by another thread, the invoking thread is
103 /// suspended until ownership can be gained.
104 /// Multiple (nested) calls to this method are not supported and lead to undefined behavior.
105 ///
106 /// \par Debug Parameter:
107 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
110
111 /// Tries to acquire this lock.
112 /// Multiple (nested) successful calls to this method or method #Acquire are not supported and
113 /// lead to undefined behavior.
114 ///
115 /// \par Debug Parameter:
116 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
117 ///
118 /// @return \c true if the lock was not acquired by a different thread and thus, this
119 /// call was successful. \c false otherwise.
121 [[nodiscard]]
123
124 /// Releases ownership of this object.
125 /// If this method is invoked on an object that is not acquired, in debug-compilations an
126 /// assertion is raised.
127 /// In release compilations, this leads to undefined behavior.
128 ///
129 /// \par Debug Parameter:
130 /// Pass macro \ref ALIB_CALLER_PRUNED with invocations.
133 #else
134 void Acquire() { mutex.lock(); }
135 [[nodiscard]] bool TryAcquire() { return mutex.try_lock(); }
136 void Release() { mutex.unlock(); }
137 #endif
138};
139
141
142} // namespace alib[::threads]
143
144/// Type alias in namespace \b alib.
146
147} // namespace [alib]
148
149#endif // HPP_ALIB_THREADS_LOCK
150
virtual ALIB_API bool DCSIsAcquired() const override
Definition locks.cpp:274
std::mutex mutex
Definition lock.hpp:71
DbgLockAsserter Dbg
The debug tool instance.
Definition lock.hpp:78
virtual ALIB_API bool DCSIsSharedAcquired() const override
Definition locks.cpp:275
~Lock() override
Destructor. With debug-compilations, asserts that this lock is not acquired.
Definition lock.hpp:85
ALIB_API bool TryAcquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:346
ALIB_API void Acquire(ALIB_DBG_TAKE_CI)
Definition locks.cpp:312
ALIB_API void Release(ALIB_DBG_TAKE_CI)
Definition locks.cpp:360
#define ALIB_DBG_TAKE_CI
Definition alib.hpp:1176
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_CALLER
Definition alib.hpp:1164
#define ALIB_API
Definition alib.hpp:639
#define ALIB_DEBUG_CRITICAL_SECTIONS
Definition prepro.md:44
ALIB_API Lock STD_IOSTREAMS_LOCK
Definition locks.cpp:301
Definition alib.cpp:69
threads::Lock Lock
Type alias in namespace alib.
Definition lock.hpp:145
void AssertNotOwned(const CallerInfo &assertCI, const CallerInfo &ci, const NString &headline)