StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
shared_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 {
63 public:
67 SharedLock() noexcept
68 : m_mutex{nullptr}, m_owns{false} {}
69
76 : m_mutex{&mutex}, m_owns{false} {
77 mutex.lock_shared();
78 m_owns = true;
79 }
80
87 : m_mutex{&mutex}, m_owns{false} {
88 (void)tag;
89 }
90
97 : m_mutex{&mutex}, m_owns{mutex.try_lock_shared()} {
98 (void)tag;
99 }
100
107 : m_mutex{&mutex}, m_owns{true} {
108 (void)tag;
109 }
110
111 SharedLock(const SharedLock&) = delete;
112
117 SharedLock(SharedLock&& other) noexcept
118 : m_mutex{other.m_mutex}, m_owns{other.m_owns} {
119 other.m_mutex = nullptr;
120 other.m_owns = false;
121 }
122
126 ~SharedLock() noexcept {
127 if (m_owns)
128 m_mutex->unlock_shared();
129 }
130
131 SharedLock& operator=(const SharedLock&) = delete;
132
138 SharedLock& operator=(SharedLock&& other) noexcept {
139 if (this != &other) {
140 if (m_owns)
141 m_mutex->unlock_shared();
142 m_mutex = other.m_mutex;
143 m_owns = other.m_owns;
144 other.m_mutex = nullptr;
145 other.m_owns = false;
146 }
147 return *this;
148 }
149
155 void lock() {
156 m_mutex->lock_shared();
157 m_owns = true;
158 }
159
164 bool try_lock() noexcept {
165 if (m_mutex == nullptr)
166 return false;
167 m_owns = m_mutex->try_lock_shared();
168 return m_owns;
169 }
170
175 void unlock() noexcept {
176 m_mutex->unlock_shared();
177 m_owns = false;
178 }
179
184 SharedMutex* release() noexcept {
185 SharedMutex* released = m_mutex;
186 m_mutex = nullptr;
187 m_owns = false;
188 return released;
189 }
190
195 SharedMutex* mutex() const noexcept {
196 return m_mutex;
197 }
198
203 bool owns_lock() const noexcept {
204 return m_owns;
205 }
206
211 explicit operator bool() const noexcept {
212 return m_owns;
213 }
214
219 void swap(SharedLock& other) noexcept {
220 SharedMutex* held = m_mutex;
221 const bool owns = m_owns;
222 m_mutex = other.m_mutex;
223 m_owns = other.m_owns;
224 other.m_mutex = held;
225 other.m_owns = owns;
226 }
227
228 private:
229 SharedMutex* m_mutex;
230
231 bool m_owns;
232 };
233
239 inline void swap(SharedLock& left, SharedLock& right) noexcept {
240 left.swap(right);
241 }
242 }
243}
Owns at most one shared hold of a SharedMutex.
Definition shared_lock.hxx:62
void unlock() noexcept
Drop the shared hold.
Definition shared_lock.hxx:175
bool try_lock() noexcept
Try to take a shared hold of the associated mutex.
Definition shared_lock.hxx:164
SharedLock(SharedMutex &mutex, DeferLock tag) noexcept
Associate mutex without locking it.
Definition shared_lock.hxx:86
SharedLock() noexcept
Empty lock.
Definition shared_lock.hxx:67
SharedLock(SharedMutex &mutex, TryToLock tag) noexcept
Try to take a shared hold of mutex.
Definition shared_lock.hxx:96
bool owns_lock() const noexcept
Whether this lock owns the shared hold.
Definition shared_lock.hxx:203
SharedLock(SharedMutex &mutex)
Take a shared hold of mutex.
Definition shared_lock.hxx:75
SharedLock(SharedMutex &mutex, AdoptLock tag) noexcept
Assume the calling thread already holds mutex shared.
Definition shared_lock.hxx:106
void lock()
Take a shared hold of the associated mutex.
Definition shared_lock.hxx:155
SharedMutex * mutex() const noexcept
Associated mutex.
Definition shared_lock.hxx:195
~SharedLock() noexcept
Drop the shared hold if this lock owns it.
Definition shared_lock.hxx:126
SharedMutex * release() noexcept
Drop the association without unlocking.
Definition shared_lock.hxx:184
SharedLock(SharedLock &&other) noexcept
Take a lock.
Definition shared_lock.hxx:117
SharedLock & operator=(const SharedLock &)=delete
SharedLock & operator=(SharedLock &&other) noexcept
Take a lock.
Definition shared_lock.hxx:138
SharedLock(const SharedLock &)=delete
void swap(SharedLock &other) noexcept
Exchange two locks.
Definition shared_lock.hxx:219
Non-recursive shared mutex.
Definition shared_mutex.hxx:60
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