API Documentation
Loading...
Searching...
No Matches
RWLock.h
Go to the documentation of this file.
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 <mutex>
37namespace NDEVR
38{
39 class UUID;
40 class TimeSpan;
41 /**----------------------------------------------------------------------------
42 \brief Maintains a pair of associated locks, one for read-only operations and
43 one for writing. The read lock may be held simultaneously by multiple reader
44 threads, so long as there are no writers. The write lock is exclusive.
45 ----------------------------------------------------------------------------*/
47 {
48 public:
50 RWLock(const RWLock& lock) = delete;
52
53 bool ReadLock();
54 bool WriteLock();
55
58
59
60 bool TryReadLock(uint08 time);
61 bool TryWriteLock(uint08 time);
62
63 void ReadUnlock();
65 [[nodiscard]] uint04 numOfWriteLocksHeld() const;
66 [[nodiscard]] uint04 numOfWriteLocks() const;
67 [[nodiscard]] uint04 numOfReadLocksHeld() const;
68 [[nodiscard]] uint04 numOfReadLocks() const;
69 private:
70 bool _TryReadLock();
71 bool _TryWriteLock();
72
73 private:
74 Dictionary<uint04, uint04> m_read_owners;//Stores every thread's read counters
75 std::mutex m_critical_section;//critical section for this object only
76 volatile uint04 m_write_owner;//The thread that owns the write lock atm
77 volatile uint04 m_readers;//The total number of reader locks held by all threads.
78 volatile uint04 m_writers;//The total number of write locks held by the writer thread.
79 volatile uint04 m_write_waiters;//When this is non-zero, read threads will wait. Used to prevent starvation.
80 public:
81
82 static RWLock* getEntry(const void* entry);
83 };
84 /**----------------------------------------------------------------------------
85 \brief Used to lock a particular variable for reading. Any number of readers
86 can be created when no write locks are on a variable, otherwise will wait.
87
88 Note: Automatically unlocks on destruction.
89 ----------------------------------------------------------------------------*/
91 {
92 public:
93 explicit RLock(const void* lock);
94 explicit RLock(const UUID& lock);
95 explicit RLock(RWLock& lock);
96 RLock(RLock&& lock) noexcept;
97 RLock(const RLock&) = delete;
98 void operator=(const RLock&) = delete;
99 RLock(const void* lock, uint08 timeout);
100 RLock(const void* lock, bool& didAquire);
101 static bool HasLock(const void* lock);
102 static bool HasLock(const RWLock& lock);
103 bool isLocked() const;
105 private:
106 RWLock* m_lock;//pointer to the lock that will be unlocked on destructor
107 };
108
109 /**----------------------------------------------------------------------------
110 \brief Used to lock a particular variable for writing. Only one write lock
111 can be created when no read locks are on a variable, otherwise will wait.
112 Note: Automatically unlocks on destruction.
113 ----------------------------------------------------------------------------*/
115 {
116 public:
117 explicit WLock(const void* lock);
118 explicit WLock(const UUID& lock);
119 explicit WLock(RWLock& lock);
120 WLock(WLock&& lock) noexcept;
121 WLock(const WLock&) = delete;
122 void operator=(const WLock&) = delete;
123 WLock(const void* lock, uint08 timeout);
124 WLock(const void* lock, const TimeSpan& timeout);
125 bool isLocked() const;
126 static bool HasLock(const void* lock);
127 static bool HasLock(const RWLock& lock);
129 private:
130 RWLock* m_lock;//pointer to the lock that will be unlocked on destructor
131 };
132}
#define NDEVR_BASE_API
Definition DLLInfo.h:57
A hash-based key-value store, useful for quick associative lookups. Key features include:
Definition Dictionary.h:61
Used to lock a particular variable for reading. Any number of readers can be created when no write lo...
Definition RWLock.h:91
RLock(const void *lock)
bool isLocked() const
RLock(const UUID &lock)
static bool HasLock(const RWLock &lock)
RLock(RLock &&lock) noexcept
RLock(const void *lock, bool &didAquire)
RLock(const RLock &)=delete
RLock(RWLock &lock)
static bool HasLock(const void *lock)
RLock(const void *lock, uint08 timeout)
void operator=(const RLock &)=delete
Maintains a pair of associated locks, one for read-only operations and one for writing....
Definition RWLock.h:47
bool WriteLock()
static RWLock * getEntry(const void *entry)
void ReadUnlock()
bool TryReadLock()
RWLock(const RWLock &lock)=delete
bool TryWriteLock()
uint04 numOfReadLocksHeld() const
uint04 numOfWriteLocks() const
uint04 numOfWriteLocksHeld() const
void WriteUnlock()
uint04 numOfReadLocks() const
bool TryWriteLock(uint08 time)
bool TryReadLock(uint08 time)
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:60
Used to lock a particular variable for writing. Only one write lock can be created when no read locks...
Definition RWLock.h:115
WLock(const UUID &lock)
WLock(const void *lock)
WLock(const WLock &)=delete
bool isLocked() const
void operator=(const WLock &)=delete
WLock(const void *lock, const TimeSpan &timeout)
static bool HasLock(const RWLock &lock)
WLock(const void *lock, uint08 timeout)
WLock(WLock &&lock) noexcept
static bool HasLock(const void *lock)
WLock(RWLock &lock)
Definition ACIColor.h:37
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96