tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
unique_lock.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MUTEX_UNIQUE_LOCK_HPP
4#define TETL_MUTEX_UNIQUE_LOCK_HPP
5
8#include <etl/_mutex/tags.hpp>
10#include <etl/_utility/swap.hpp>
11
12namespace etl {
13
27template <typename Mutex>
29private:
30 Mutex* _mutex{nullptr};
31 bool _owns{false};
32
33public:
34 using mutex_type = Mutex;
35
37 constexpr unique_lock() noexcept = default;
38
43 explicit constexpr unique_lock(mutex_type& m)
44 : _mutex{&m}
45 {
46 lock();
47 }
48
51 constexpr unique_lock(mutex_type& m, defer_lock_t /*tag*/) noexcept
52 : _mutex{&m}
53 {
54 }
55
60 constexpr unique_lock(mutex_type& m, try_to_lock_t /*tag*/) noexcept
61 : _mutex{&m}
62 {
63 try_lock();
64 }
65
68 constexpr unique_lock(mutex_type& m, adopt_lock_t /*tag*/)
69 : _mutex{&m}
70 , _owns{true}
71 {
72 }
73
79 template <typename Clock, typename Duration>
80 constexpr unique_lock(mutex_type& m, chrono::time_point<Clock, Duration> const& absTime) noexcept
81 : _mutex{&m}
82 {
83 try_lock_until(absTime);
84 }
85
91 template <typename Rep, typename Period>
92 constexpr unique_lock(mutex_type& m, chrono::duration<Rep, Period> const& relTime) noexcept
93 : _mutex{&m}
94 {
95 try_lock_for(relTime);
96 }
97
99 constexpr unique_lock(unique_lock const&) = delete;
100
102 constexpr auto operator=(unique_lock const&) -> unique_lock& = delete;
103
106 constexpr unique_lock(unique_lock&& u) noexcept
107 : _mutex{exchange(u._mutex, nullptr)}
108 , _owns{exchange(u._owns, false)}
109 {
110 }
111
115 constexpr auto operator=(unique_lock&& u) noexcept -> unique_lock&
116 {
117 if (_mutex != nullptr && _owns) {
118 unlock();
119 }
120 _mutex = exchange(u._mutex, nullptr);
121 _owns = exchange(u._owns, false);
122 return *this;
123 }
124
125 constexpr ~unique_lock() noexcept { unlock(); }
126
128 constexpr auto lock() noexcept(noexcept(_mutex->lock())) -> void
129 {
130 if ((_mutex != nullptr) and !_owns) {
131 _mutex->lock();
132 _owns = true;
133 }
134 }
135
140 constexpr auto try_lock() noexcept(noexcept(_mutex->try_lock())) -> bool
141 {
142 if ((_mutex != nullptr) && !_owns) {
143 if (auto success = _mutex->try_lock(); success) {
144 _owns = true;
145 return true;
146 }
147 }
148
149 return false;
150 }
151
159 template <typename Rep, typename Period>
160 constexpr auto try_lock_for(chrono::duration<Rep, Period> const& dur) noexcept(noexcept(_mutex->try_lock_for(dur)))
161 -> bool
162 {
163 if ((_mutex != nullptr) && !_owns) {
164 if (auto success = _mutex->try_lock_for(dur); success) {
165 _owns = true;
166 return true;
167 }
168 }
169 return false;
170 }
171
174 template <typename Clock, typename Duration>
176 noexcept(noexcept(_mutex->try_lock_until(tp))) -> bool
177 {
178 if ((_mutex != nullptr) && !_owns) {
179 if (auto success = _mutex->try_lock_until(tp); success) {
180 _owns = true;
181 return true;
182 }
183 }
184 return false;
185 }
186
190 constexpr auto unlock() -> void
191 {
192 if ((_mutex != nullptr) and _owns) {
193 _mutex->unlock();
194 _owns = false;
195 }
196 }
197
199 constexpr auto swap(unique_lock& other) noexcept -> void
200 {
201 using etl::swap;
202 swap(_mutex, other._mutex);
203 swap(_owns, other._owns);
204 }
205
213 [[nodiscard]] constexpr auto release() noexcept -> mutex_type*
214 {
215 _owns = false;
216 return _mutex;
217 }
218
220 [[nodiscard]] constexpr auto owns_lock() const noexcept -> bool { return _owns; }
221
223 [[nodiscard]] explicit constexpr operator bool() const noexcept { return owns_lock(); }
224
227 [[nodiscard]] constexpr auto mutex() const noexcept -> mutex_type* { return _mutex; }
228
230 friend constexpr auto swap(unique_lock& lhs, unique_lock& rhs) noexcept(noexcept(lhs.swap(rhs))) -> void
231 {
232 lhs.swap(rhs);
233 }
234};
235
236} // namespace etl
237
238#endif // TETL_MUTEX_UNIQUE_LOCK_HPP
Definition adjacent_find.hpp:8
constexpr auto exchange(T &obj, U &&newValue) noexcept(etl::is_nothrow_move_constructible_v< T > and etl::is_nothrow_assignable_v< T &, U >) -> T
Replaces the value of obj with new_value and returns the old value of obj.
Definition exchange.hpp:16
auto swap(inplace_function< R(Args...), Capacity, Alignment > &lhs, inplace_function< R(Args...), Capacity, Alignment > &rhs) noexcept -> void
Overloads the etl::swap algorithm for etl::inplace_function. Exchanges the state of lhs with that of ...
Definition inplace_function.hpp:249
Empty struct tag types used to specify locking strategy for etl::lock_guard, etl::scoped_lock,...
Definition tags.hpp:41
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:31
Class template time_point represents a point in time. It is implemented as if it stores a value of ty...
Definition time_point.hpp:21
Empty struct tag types used to specify locking strategy for etl::lock_guard, etl::scoped_lock,...
Definition tags.hpp:13
Empty struct tag types used to specify locking strategy for etl::lock_guard, etl::scoped_lock,...
Definition tags.hpp:27
constexpr unique_lock(unique_lock const &)=delete
Deleted copy constructor. unique_lock is move only.
constexpr unique_lock(mutex_type &m, adopt_lock_t)
Constructs a unique_lock with m as the associated mutex. Additionally: Assumes the calling thread alr...
Definition unique_lock.hpp:68
constexpr auto mutex() const noexcept -> mutex_type *
Returns a pointer to the associated mutex, or a null pointer if there is no associated mutex.
Definition unique_lock.hpp:227
constexpr auto swap(unique_lock &other) noexcept -> void
Exchanges the internal states of the lock objects.
Definition unique_lock.hpp:199
constexpr auto owns_lock() const noexcept -> bool
Checks whether *this owns a locked mutex or not.
Definition unique_lock.hpp:220
constexpr auto operator=(unique_lock &&u) noexcept -> unique_lock &
Move assignment operator. Replaces the contents with those of other using move semantics....
Definition unique_lock.hpp:115
constexpr auto try_lock_until(chrono::time_point< Clock, Duration > const &tp) noexcept(noexcept(_mutex->try_lock_until(tp))) -> bool
Tries to lock (i.e., takes ownership of) the associated mutex without blocking.
Definition unique_lock.hpp:175
constexpr auto operator=(unique_lock const &) -> unique_lock &=delete
Deleted copy assignment. unique_lock is move only.
constexpr auto release() noexcept -> mutex_type *
Breaks the association of the associated mutex, if any, and *this. No locks are unlocked....
Definition unique_lock.hpp:213
constexpr auto lock() noexcept(noexcept(_mutex->lock())) -> void
Locks (i.e., takes ownership of) the associated mutex.
Definition unique_lock.hpp:128
constexpr auto unlock() -> void
Unlocks (i.e., releases ownership of) the associated mutex and releases ownership....
Definition unique_lock.hpp:190
constexpr unique_lock(mutex_type &m, chrono::time_point< Clock, Duration > const &absTime) noexcept
Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated m...
Definition unique_lock.hpp:80
constexpr unique_lock(mutex_type &m, defer_lock_t) noexcept
Constructs a unique_lock with m as the associated mutex. Additionally: Does not lock the associated m...
Definition unique_lock.hpp:51
constexpr auto try_lock_for(chrono::duration< Rep, Period > const &dur) noexcept(noexcept(_mutex->try_lock_for(dur))) -> bool
Tries to lock (i.e., takes ownership of) the associated mutex. Blocks until specified timeout_duratio...
Definition unique_lock.hpp:160
constexpr ~unique_lock() noexcept
Definition unique_lock.hpp:125
friend constexpr auto swap(unique_lock &lhs, unique_lock &rhs) noexcept(noexcept(lhs.swap(rhs))) -> void
Specializes the swap algorithm for unique_lock. Exchanges the state of lhs with that of rhs.
Definition unique_lock.hpp:230
constexpr auto try_lock() noexcept(noexcept(_mutex->try_lock())) -> bool
Tries to lock (i.e., takes ownership of) the associated mutex without blocking.
Definition unique_lock.hpp:140
constexpr unique_lock(mutex_type &m, try_to_lock_t) noexcept
Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated m...
Definition unique_lock.hpp:60
constexpr unique_lock() noexcept=default
Constructs a unique_lock with no associated mutex.
constexpr unique_lock(mutex_type &m, chrono::duration< Rep, Period > const &relTime) noexcept
Constructs a unique_lock with m as the associated mutex. Additionally: Tries to lock the associated m...
Definition unique_lock.hpp:92
constexpr unique_lock(unique_lock &&u) noexcept
Move constructor. Initializes the unique_lock with the contents of other. Leaves other with no associ...
Definition unique_lock.hpp:106
Mutex mutex_type
Definition unique_lock.hpp:34