NDEVR
API Documentation
RWLock.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: RWLock
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/BaseValues.h>
35#include <NDEVR/Dictionary.h>
36#include <NDEVR/Buffer.h>
37#include <mutex>
38namespace NDEVR
39{
40 class UUID;
41 class TimeSpan;
42
48 class NDEVR_BASE_API RWLock
49 {
50 public:
55 RWLock(const RWLock& lock) = delete;
60
64 void readLock();
68 void writeLock();
69
80
86 bool tryReadLock(uint08 time);
92 bool tryWriteLock(uint08 time);
93
97 void readUnlock();
106 [[nodiscard]] uint04 numOfWriteLocksHeld() const;
111 [[nodiscard]] uint04 numOfWriteLocks() const;
116 [[nodiscard]] uint04 numOfReadLocksHeld() const;
121 [[nodiscard]] uint04 numOfReadLocks() const;
122 private:
127 bool _TryReadLock();
132 bool _TryWriteLock();
133
134 private:
135 std::mutex m_critical_section;
136 volatile uint04 m_write_owner;
137 volatile uint04 m_readers;
138 volatile uint04 m_writers;
139 volatile uint04 m_write_waiters;
140 public:
146 static RWLock* getEntry(const void* entry);
147 };
148
156 class NDEVR_BASE_API RLock
157 {
158 public:
163 explicit RLock(const void* lock);
168 explicit RLock(const UUID& lock);
173 explicit RLock(RWLock& lock);
179 RLock(const void* lock, uint08 timeout);
185 RLock(const void* lock, bool& didAquire);
190 RLock(RLock&& lock) noexcept;
191 RLock(const RLock&) = delete;
201 static bool HasLock(const void* lock);
207 static bool HasLock(const RWLock& lock);
212 bool isLocked() const;
213 void operator=(const RLock&) = delete;
214 private:
215 RWLock* m_lock;
216 };
217
222 class NDEVR_BASE_API MultiRLock
223 {
224 public:
229 MultiRLock(std::initializer_list<const void*> l);
234 MultiRLock(std::initializer_list<RWLock*> l);
240 MultiRLock(std::initializer_list<const void*> l, uint08 timeout);
245 MultiRLock(MultiRLock&& lock) noexcept;
246 MultiRLock(const MultiRLock&) = delete;
247 void operator=(const MultiRLock&) = delete;
252 bool isLocked() const;
257 private:
261 void sortAndAcquire();
262 Buffer<RWLock*> m_lock;
263 };
264
271 class NDEVR_BASE_API WLock
272 {
273 public:
278 explicit WLock(const void* lock);
283 explicit WLock(const UUID& lock);
288 explicit WLock(RWLock& lock);
293 WLock(WLock&& lock) noexcept;
294 WLock(const WLock&) = delete;
295 void operator=(const WLock&) = delete;
301 WLock(const void* lock, uint08 timeout);
307 WLock(const void* lock, const TimeSpan& timeout);
312 bool isLocked() const;
318 static bool HasLock(const void* lock);
324 static bool HasLock(const RWLock& lock);
329 private:
330 RWLock* m_lock;
331 };
332
337 class NDEVR_BASE_API MultiWLock
338 {
339 public:
344 MultiWLock(std::initializer_list<const void*> l);
350 MultiWLock(std::initializer_list<const void*> l, uint08 timeout);
355 MultiWLock(std::initializer_list<RWLock*> l);
360 MultiWLock(MultiWLock&& lock) noexcept;
361 MultiWLock(const MultiWLock&) = delete;
362 void operator=(const MultiWLock&) = delete;
367 bool isLocked() const;
372 private:
376 void sortAndAcquire();
377 Buffer<RWLock*> m_lock;
378 };
379}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
~MultiRLock()
Destructor.
bool isLocked() const
Returns whether all locks in this MultiRLock are currently held.
MultiRLock(std::initializer_list< const void * > l, uint08 timeout)
Constructs read locks on multiple entries with a timeout.
MultiRLock(MultiRLock &&lock) noexcept
Move constructor.
MultiRLock(std::initializer_list< RWLock * > l)
Constructs read locks on multiple RWLocks directly.
MultiRLock(std::initializer_list< const void * > l)
Constructs read locks on multiple entries identified by pointers.
MultiWLock(std::initializer_list< const void * > l, uint08 timeout)
Constructs write locks on multiple entries with a timeout.
MultiWLock(std::initializer_list< RWLock * > l)
Constructs write locks on multiple RWLocks directly.
bool isLocked() const
Returns whether all locks in this MultiWLock are currently held.
MultiWLock(std::initializer_list< const void * > l)
Constructs write locks on multiple entries identified by pointers.
MultiWLock(MultiWLock &&lock) noexcept
Move constructor.
~MultiWLock()
Destructor.
RLock(RLock &&lock) noexcept
Move constructor.
RLock(const void *lock, uint08 timeout)
Constructs a read lock with a timeout, looking up the RWLock by pointer.
bool isLocked() const
Returns whether this RLock currently holds a read lock.
RLock(const void *lock)
Constructs a read lock by looking up the RWLock associated with the given pointer.
RLock(const void *lock, bool &didAquire)
Attempts to construct a read lock, reporting whether acquisition succeeded.
RLock(const UUID &lock)
Constructs a read lock by looking up the RWLock associated with the given UUID.
static bool HasLock(const void *lock)
Checks whether a read lock is currently held for the given pointer.
static bool HasLock(const RWLock &lock)
Checks whether a read lock is currently held on the given RWLock.
RLock(RWLock &lock)
Constructs a read lock on the given RWLock.
~RLock()
Destructor.
A readers-writer lock allowing concurrent reads or exclusive writes.
Definition RWLock.h:49
void readLock()
Acquires a read lock.
~RWLock()
Destroys the RWLock.
bool tryReadLock(uint08 time)
Attempts to acquire a read lock, waiting up to the specified time.
bool tryReadLock()
Attempts to acquire a read lock without blocking.
uint04 numOfWriteLocks() const
Returns the total number of write locks currently held.
bool tryWriteLock(uint08 time)
Attempts to acquire a write lock, waiting up to the specified time.
bool tryWriteLock()
Attempts to acquire a write lock without blocking.
void readUnlock()
Releases a previously acquired read lock.
uint04 numOfReadLocksHeld() const
Returns the number of read locks held by the calling thread.
RWLock()
Constructs a new RWLock with no locks held.
static RWLock * getEntry(const void *entry)
Retrieves the RWLock associated with the given pointer.
void writeUnlock()
Releases a previously acquired write lock.
uint04 numOfWriteLocksHeld() const
Returns the number of write locks held by the calling thread.
void writeLock()
Acquires a write lock.
uint04 numOfReadLocks() const
Returns the total number of read locks currently held.
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer s...
Definition UUID.h:61
~WLock()
Destructor.
WLock(const UUID &lock)
Constructs a write lock by looking up the RWLock associated with the given UUID.
WLock(const void *lock, uint08 timeout)
Constructs a write lock with a timeout, looking up the RWLock by pointer.
static bool HasLock(const RWLock &lock)
Checks whether a write lock is currently held on the given RWLock.
static bool HasLock(const void *lock)
Checks whether a write lock is currently held for the given pointer.
WLock(WLock &&lock) noexcept
Move constructor.
WLock(const void *lock)
Constructs a write lock by looking up the RWLock associated with the given pointer.
WLock(const void *lock, const TimeSpan &timeout)
Constructs a write lock with a TimeSpan timeout, looking up the RWLock by pointer.
bool isLocked() const
Returns whether this WLock currently holds a write lock.
WLock(RWLock &lock)
Constructs a write lock on the given RWLock.
The primary namespace for the NDEVR SDK.
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...