StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
unique_lock.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
45
50namespace StormByte {
55 namespace Safe {
57
65 public:
69 UniqueLock() noexcept
70 : m_mutex{nullptr}, m_owns{false} {}
71
78 : m_mutex{&mutex}, m_owns{false} {
79 mutex.lock();
80 m_owns = true;
81 }
82
89 : m_mutex{&mutex}, m_owns{false} {}
90
97 : m_mutex{&mutex}, m_owns{mutex.try_lock()} {}
98
105 : m_mutex{&mutex}, m_owns{true} {}
106
107 UniqueLock(const UniqueLock&) = delete;
108
113 UniqueLock(UniqueLock&& other) noexcept
114 : m_mutex{other.m_mutex}, m_owns{other.m_owns} {
115 other.m_mutex = nullptr;
116 other.m_owns = false;
117 }
118
122 ~UniqueLock() noexcept {
123 if (m_owns)
124 m_mutex->unlock();
125 }
126
127 UniqueLock& operator=(const UniqueLock&) = delete;
128
134 UniqueLock& operator=(UniqueLock&& other) noexcept {
135 if (this != &other) {
136 if (m_owns)
137 m_mutex->unlock();
138 m_mutex = other.m_mutex;
139 m_owns = other.m_owns;
140 other.m_mutex = nullptr;
141 other.m_owns = false;
142 }
143 return *this;
144 }
145
151 void lock() {
152 m_mutex->lock();
153 m_owns = true;
154 }
155
160 bool try_lock() noexcept {
161 if (m_mutex == nullptr)
162 return false;
163 m_owns = m_mutex->try_lock();
164 return m_owns;
165 }
166
171 void unlock() noexcept {
172 m_mutex->unlock();
173 m_owns = false;
174 }
175
180 Mutex* release() noexcept {
181 Mutex* released = m_mutex;
182 m_mutex = nullptr;
183 m_owns = false;
184 return released;
185 }
186
191 Mutex* mutex() const noexcept {
192 return m_mutex;
193 }
194
199 bool owns_lock() const noexcept {
200 return m_owns;
201 }
202
207 explicit operator bool() const noexcept {
208 return m_owns;
209 }
210
215 void swap(UniqueLock& other) noexcept {
216 Mutex* held = m_mutex;
217 const bool owns = m_owns;
218 m_mutex = other.m_mutex;
219 m_owns = other.m_owns;
220 other.m_mutex = held;
221 other.m_owns = owns;
222 }
223
224 private:
225 friend class ConditionVariable;
226
230 void Abandon() noexcept {
231 m_owns = false;
232 }
233
237 void Claim() noexcept {
238 m_owns = true;
239 }
240
241 Mutex* m_mutex;
242
243 bool m_owns;
244 };
245
251 inline void swap(UniqueLock& left, UniqueLock& right) noexcept {
252 left.swap(right);
253 }
254 }
255}
Wakes threads waiting on a Mutex.
Definition condition_variable.hxx:64
Non-recursive mutex.
Definition mutex.hxx:62
Owns at most one Mutex.
Definition unique_lock.hxx:64
UniqueLock() noexcept
Empty lock.
Definition unique_lock.hxx:69
UniqueLock & operator=(const UniqueLock &)=delete
UniqueLock(UniqueLock &&other) noexcept
Take a lock.
Definition unique_lock.hxx:113
UniqueLock & operator=(UniqueLock &&other) noexcept
Take a lock.
Definition unique_lock.hxx:134
Mutex * release() noexcept
Drop the association without unlocking.
Definition unique_lock.hxx:180
bool owns_lock() const noexcept
Whether this lock owns the mutex.
Definition unique_lock.hxx:199
UniqueLock(Mutex &mutex, TryToLock tag) noexcept
Try to lock mutex.
Definition unique_lock.hxx:96
UniqueLock(Mutex &mutex, AdoptLock tag) noexcept
Assume the calling thread already owns mutex.
Definition unique_lock.hxx:104
void lock()
Acquire the associated mutex.
Definition unique_lock.hxx:151
Mutex * mutex() const noexcept
Associated mutex.
Definition unique_lock.hxx:191
bool try_lock() noexcept
Try to acquire the associated mutex.
Definition unique_lock.hxx:160
friend class ConditionVariable
Definition unique_lock.hxx:225
UniqueLock(Mutex &mutex, DeferLock tag) noexcept
Associate mutex without locking it.
Definition unique_lock.hxx:88
void swap(UniqueLock &other) noexcept
Exchange two locks.
Definition unique_lock.hxx:215
void unlock() noexcept
Release ownership.
Definition unique_lock.hxx:171
~UniqueLock() noexcept
Unlock if this lock owns the mutex.
Definition unique_lock.hxx:122
UniqueLock(Mutex &mutex)
Lock mutex.
Definition unique_lock.hxx:77
UniqueLock(const UniqueLock &)=delete
Owned values that cross a DLL without the caller's CRT.
void swap(Binary &left, Binary &right) noexcept
Exchange two sequences.
Definition binary.hxx:760
Root namespace of the StormByte suite.
The calling thread already owns the mutex.
Definition memory_order.hxx:104
Do not lock the mutex in the UniqueLock constructor.
Definition memory_order.hxx:82
Try to lock the mutex in the UniqueLock constructor.
Definition memory_order.hxx:93
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57