roo_threads
API Documentation for roo_threads
Loading...
Searching...
No Matches
mutex_api.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5#include "roo_time.h"
6
7namespace roo_threads {
8namespace doc {
9
10/// @ingroup roo_threads_api_mutex
11/// @brief Mutual exclusion primitive.
12class mutex {
13 public:
14 /// @brief Constructs a mutex.
15 mutex() noexcept;
16
17 mutex(const mutex&) = delete;
18 mutex& operator=(const mutex&) = delete;
19
20 /// @brief Blocks until the mutex is acquired.
21 void lock();
22
23 /// @brief Attempts to acquire the mutex without blocking.
24 /// @return true if lock acquisition succeeded.
25 bool try_lock();
26
27 /// @brief Releases the mutex.
28 void unlock();
29};
30
31template <typename Mutex>
32/// @ingroup roo_threads_api_mutex
34 public:
35 /// @brief Type of wrapped mutex.
36 using mutex_type = Mutex;
37
38 /// @brief Acquires mutex on construction.
39 /// @param mutex mutex to lock.
41
42 /// @brief Releases mutex on destruction.
44};
45
46/// @ingroup roo_threads_api_mutex
47/// @brief Tag type selecting deferred locking constructor overloads.
48struct defer_lock_t;
49
50/// @ingroup roo_threads_api_mutex
51/// @brief Tag type selecting try-lock constructor overloads.
52struct try_to_lock_t;
53
54/// @ingroup roo_threads_api_mutex
55/// @brief Tag type selecting adopt-lock constructor overloads.
56struct adopt_lock_t;
57
58template <typename Mutex>
59/// @ingroup roo_threads_api_mutex
61 public:
62 /// @brief Type of wrapped mutex.
63 using mutex_type = Mutex;
64
65 /// @brief Constructs an empty lock not associated with any mutex.
66 unique_lock() noexcept;
67
68 /// @brief Acquires the given mutex.
69 /// @param mutex mutex to lock.
71
72 /// @brief Associates with mutex but does not lock it.
73 /// @param mutex mutex to associate with.
74 unique_lock(mutex_type& mutex, defer_lock_t) noexcept;
75
76 /// @brief Attempts to lock mutex without blocking.
77 /// @param mutex mutex to lock.
78 unique_lock(mutex_type& mutex, try_to_lock_t);
79
80 /// @brief Adopts an already-owned mutex.
81 /// @param mutex mutex already locked by caller.
82 unique_lock(mutex_type& mutex, adopt_lock_t) noexcept;
83
84 /// @brief Attempts to lock until the specified time point.
85 /// @param mutex mutex to lock.
86 /// @param tp absolute timeout point.
87 unique_lock(mutex_type& mutex, roo_time::Uptime tp);
88
89 /// @brief Attempts to lock for the specified duration.
90 /// @param mutex mutex to lock.
91 /// @param duration relative timeout duration.
92 unique_lock(mutex_type& mutex, roo_time::Duration duration);
93
94 /// @brief Releases owned mutex if any.
96
97 unique_lock(const unique_lock&) = delete;
98 unique_lock& operator=(const unique_lock&) = delete;
99
100 /// @brief Move-constructs the lock.
101 unique_lock(unique_lock&& lock) noexcept;
102
103 /// @brief Move-assigns the lock.
104 /// @return reference to this object.
105 unique_lock& operator=(unique_lock&& lock) noexcept;
106
107 /// @brief Acquires associated mutex.
108 void lock();
109
110 /// @brief Attempts to acquire associated mutex without blocking.
111 /// @return true if lock acquired.
112 bool try_lock();
113
114 /// @brief Attempts to acquire associated mutex before the given deadline.
115 /// @param tp absolute timeout point.
116 /// @return true if lock acquired.
117 bool try_lock_until(roo_time::Uptime tp);
118
119 /// @brief Attempts to acquire associated mutex for the given duration.
120 /// @param duration relative timeout duration.
121 /// @return true if lock acquired.
122 bool try_lock_for(roo_time::Duration duration);
123
124 /// @brief Releases associated mutex if owned.
125 void unlock();
126
127 /// @brief Swaps two locks.
128 void swap(unique_lock& other) noexcept;
129
130 /// @brief Disassociates lock from mutex without unlocking.
131 /// @return pointer to previously associated mutex.
132 mutex_type* release() noexcept;
133
134 /// @brief Returns whether this lock currently owns the mutex.
135 bool owns_lock() const noexcept;
136
137 /// @brief Boolean conversion equivalent to `owns_lock()`.
138 explicit operator bool() const noexcept;
139
140 /// @brief Returns associated mutex pointer, or `nullptr`.
141 mutex_type* mutex() const noexcept;
142};
143
144} // namespace doc
145} // namespace roo_threads
Mutex mutex_type
Type of wrapped mutex.
Definition mutex_api.h:36
~lock_guard()
Releases mutex on destruction.
lock_guard(mutex_type &mutex)
Acquires mutex on construction.
Mutual exclusion primitive.
Definition mutex_api.h:12
bool try_lock()
Attempts to acquire the mutex without blocking.
void lock()
Blocks until the mutex is acquired.
mutex() noexcept
Constructs a mutex.
void unlock()
Releases the mutex.
unique_lock() noexcept
Constructs an empty lock not associated with any mutex.
Mutex mutex_type
Type of wrapped mutex.
Definition mutex_api.h:63