4#ifndef TETL_MUTEX_UNIQUE_LOCK_HPP
5#define TETL_MUTEX_UNIQUE_LOCK_HPP
7#include <etl/_chrono/duration.hpp>
8#include <etl/_chrono/time_point.hpp>
9#include <etl/_mutex/tags.hpp>
10#include <etl/_utility/exchange.hpp>
11#include <etl/_utility/swap.hpp>
28template <
typename Mutex>
31 Mutex* _mutex{
nullptr};
35 using mutex_type = Mutex;
80 template <
typename Clock,
typename Duration>
84 try_lock_until(absTime);
92 template <
typename Rep,
typename Period>
96 try_lock_for(relTime);
108 : _mutex{exchange(u._mutex,
nullptr)}
109 , _owns{exchange(u._owns,
false)}
118 if (_mutex !=
nullptr && _owns) {
121 _mutex = exchange(u._mutex,
nullptr);
122 _owns = exchange(u._owns,
false);
134 if ((_mutex !=
nullptr)
and !_owns) {
146 if ((_mutex !=
nullptr) && !_owns) {
147 if (
auto success = _mutex->try_lock(); success) {
163 template <
typename Rep,
typename Period>
167 if ((_mutex !=
nullptr) && !_owns) {
168 if (
auto success = _mutex->try_lock_for(dur); success) {
178 template <
typename Clock,
typename Duration>
182 if ((_mutex !=
nullptr) && !_owns) {
183 if (
auto success = _mutex->try_lock_until(tp); success) {
196 if ((_mutex !=
nullptr)
and _owns) {
206 swap(_mutex, other._mutex);
207 swap(_owns, other._owns);
Definition adjacent_find.hpp:9
Empty struct tag types used to specify locking strategy for etl::lock_guard, etl::scoped_lock,...
Definition tags.hpp:42
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:32
Class template time_point represents a point in time. It is implemented as if it stores a value of ty...
Definition time_point.hpp:22
Empty struct tag types used to specify locking strategy for etl::lock_guard, etl::scoped_lock,...
Definition tags.hpp:14
Empty struct tag types used to specify locking strategy for etl::lock_guard, etl::scoped_lock,...
Definition tags.hpp:28
The struct unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking,...
Definition unique_lock.hpp:29
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:69
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:237
constexpr auto swap(unique_lock &other) noexcept -> void
Exchanges the internal states of the lock objects.
Definition unique_lock.hpp:203
constexpr auto owns_lock() const noexcept -> bool
Checks whether *this owns a locked mutex or not.
Definition unique_lock.hpp:224
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:116
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:180
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:217
constexpr operator bool() const noexcept
Checks whether *this owns a locked mutex or not.
Definition unique_lock.hpp:230
constexpr auto lock() noexcept(noexcept(_mutex->lock())) -> void
Locks (i.e., takes ownership of) the associated mutex.
Definition unique_lock.hpp:132
constexpr auto unlock() -> void
Unlocks (i.e., releases ownership of) the associated mutex and releases ownership....
Definition unique_lock.hpp:194
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:81
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:52
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:164
constexpr ~unique_lock() noexcept
Definition unique_lock.hpp:126
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:243
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:144
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:61
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:93
constexpr unique_lock(mutex_type &m)
Constructs a unique_lock with m as the associated mutex. Additionally: Locks the associated mutex by ...
Definition unique_lock.hpp:44
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:107