tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
lock_guard.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_MUTEX_LOCK_GUARD_HPP
5#define TETL_MUTEX_LOCK_GUARD_HPP
6
7#include <etl/_mutex/tags.hpp>
8
9namespace etl {
10
11/// \brief The struct lock_guard is a mutex wrapper that provides a convenient
12/// RAII-style mechanism for owning a mutex for the duration of a scoped block.
13/// When a lock_guard object is created, it attempts to take ownership of the
14/// mutex it is given. When control leaves the scope in which the lock_guard
15/// object was created, the lock_guard is destructed and the mutex is released.
16/// The lock_guard struct is non-copyable.
17/// \ingroup mutex
18template <typename MutexT>
19struct lock_guard {
20 using mutex_type = MutexT;
21
22 explicit lock_guard(mutex_type& m)
23 : _mutex{m}
24 {
25 _mutex.lock();
26 }
27
28 lock_guard(mutex_type& m, adopt_lock_t /*tag*/)
29 : _mutex{m}
30 {
31 }
32
34 {
35 _mutex.unlock();
36 }
37
38 lock_guard(lock_guard const&) = delete;
39 auto operator=(lock_guard const&) -> lock_guard& = delete;
40
41private:
42 mutex_type& _mutex;
43};
44
45} // namespace etl
46
47#endif // TETL_MUTEX_LOCK_GUARD_HPP
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
The struct lock_guard is a mutex wrapper that provides a convenient RAII-style mechanism for owning a...
Definition lock_guard.hpp:19
lock_guard(mutex_type &m)
Definition lock_guard.hpp:22
auto operator=(lock_guard const &) -> lock_guard &=delete
lock_guard(lock_guard const &)=delete
~lock_guard()
Definition lock_guard.hpp:33
lock_guard(mutex_type &m, adopt_lock_t)
Definition lock_guard.hpp:28