nlib
SharedCriticalSection.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_SHAREDCRITICALSECTION_H_
4 #define INCLUDE_NN_NLIB_THREADING_SHAREDCRITICALSECTION_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/DateTime.h"
8 
9 NLIB_NAMESPACE_BEGIN
10 namespace threading {
11 
12 class NLIB_CAPABILITY("mutex") SharedCriticalSection NLIB_FINAL {
13  public:
14 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
15  constexpr SharedCriticalSection() NLIB_NOEXCEPT = default;
16 #else
17  SharedCriticalSection() NLIB_NOEXCEPT {
18  errno_t e = nlib_rwlock_init(&rwock_);
19  NLIB_ASSERT_NOERR(e);
20  NLIB_UNUSED(e);
21  }
22 #endif
23  ~SharedCriticalSection() NLIB_NOEXCEPT {
24  errno_t e = nlib_rwlock_destroy(&rwock_);
25  NLIB_ASSERT_NOERR(e);
26  NLIB_UNUSED(e);
27  }
28 
29  void lock_shared() NLIB_NOEXCEPT NLIB_ACQUIRE_SHARED() {
30  errno_t e = nlib_rwlock_rdlock(&rwock_);
31  NLIB_UNUSED(e);
32  NLIB_ASSERT_NOERR(e);
33  }
34  NLIB_CHECK_RESULT bool try_lock_shared() NLIB_NOEXCEPT NLIB_TRY_ACQUIRE_SHARED(true) {
35  errno_t e = nlib_rwlock_tryrdlock(&rwock_);
36  NLIB_ASSERT(e == 0 || e == EBUSY);
37  return e == 0;
38  }
39  NLIB_CHECK_RESULT bool try_lock_shared_for(const TimeSpan& timeout) NLIB_NOEXCEPT
40  NLIB_TRY_ACQUIRE_SHARED(true) {
41  errno_t e = nlib_rwlock_tryrdlock_for(&rwock_, timeout.ToTimeValue().tick);
42  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
43  return e == 0;
44  }
45  NLIB_CHECK_RESULT bool try_lock_shared_until(const DateTime& abstime) NLIB_NOEXCEPT
46  NLIB_TRY_ACQUIRE_SHARED(true) {
47  errno_t e = nlib_rwlock_tryrdlock_until(&rwock_, abstime.ToTimeValue().tick);
48  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
49  return e == 0;
50  }
51  void unlock_shared() NLIB_NOEXCEPT NLIB_RELEASE_SHARED() {
52  errno_t e = nlib_rwlock_rdunlock(&rwock_);
53  NLIB_UNUSED(e);
54  NLIB_ASSERT_NOERR(e);
55  }
56 
57  void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
58  errno_t e = nlib_rwlock_wrlock(&rwock_);
59  NLIB_UNUSED(e);
60  NLIB_ASSERT_NOERR(e);
61  }
62  NLIB_CHECK_RESULT bool try_lock() NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
63  errno_t e = nlib_rwlock_trywrlock(&rwock_);
64  NLIB_ASSERT(e == 0 || e == EBUSY);
65  return e == 0;
66  }
68  NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
69  errno_t e = nlib_rwlock_trywrlock_for(&rwock_, timeout.ToTimeValue().tick);
70  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
71  return e == 0;
72  }
74  NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
75  errno_t e = nlib_rwlock_trywrlock_until(&rwock_, abstime.ToTimeValue().tick);
76  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
77  return e == 0;
78  }
79  void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
80  errno_t e = nlib_rwlock_wrunlock(&rwock_);
81  NLIB_UNUSED(e);
82  NLIB_ASSERT_NOERR(e);
83  }
84 
85  private:
86 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
88 #else
89  nlib_rwlock rwock_;
90 #endif
92 };
93 
95  public:
96 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
97  constexpr CondVarForSharedCriticalSection() NLIB_NOEXCEPT = default;
98 #else
99  CondVarForSharedCriticalSection() NLIB_NOEXCEPT {
100  errno_t e = nlib_condrwlock_init(&condrwlock_);
101  NLIB_ASSERT_NOERR(e);
102  NLIB_UNUSED(e);
103  }
104 #endif
106  errno_t e = nlib_condrwlock_destroy(&condrwlock_);
107  NLIB_ASSERT_NOERR(e);
108  NLIB_UNUSED(e);
109  }
110  void Notify() NLIB_NOEXCEPT {
111  errno_t e = nlib_condrwlock_signal(&condrwlock_);
112  NLIB_UNUSED(e);
113  NLIB_ASSERT_NOERR(e);
114  }
115  void NotifyAll() NLIB_NOEXCEPT {
116  errno_t e = nlib_condrwlock_broadcast(&condrwlock_);
117  NLIB_UNUSED(e);
118  NLIB_ASSERT_NOERR(e);
119  }
120 
121  errno_t WaitReader(SharedCriticalSection& lock) NLIB_NOEXCEPT { // NOLINT
122  return nlib_condrwlock_wait(&condrwlock_, &lock.rwock_, 1);
123  }
124  errno_t WaitReader(nlib_rwlock& lock) NLIB_NOEXCEPT { // NOLINT
125  return nlib_condrwlock_wait(&condrwlock_, &lock, 1);
126  }
127  errno_t Wait(SharedCriticalSection& lock) NLIB_NOEXCEPT { // NOLINT
128  return nlib_condrwlock_wait(&condrwlock_, &lock.rwock_, 0);
129  }
130  errno_t Wait(nlib_rwlock& lock) NLIB_NOEXCEPT { // NOLINT
131  return nlib_condrwlock_wait(&condrwlock_, &lock, 0);
132  }
133 
135  const TimeSpan& timeout) NLIB_NOEXCEPT {
136  return nlib_condrwlock_wait_for(&condrwlock_, &lock.rwock_,
137  timeout.ToTimeValue().tick, 1);
138  }
140  const TimeSpan& timeout) NLIB_NOEXCEPT {
141  return nlib_condrwlock_wait_for(&condrwlock_, &lock,
142  timeout.ToTimeValue().tick, 1);
143  }
145  const TimeSpan& timeout) NLIB_NOEXCEPT {
146  return nlib_condrwlock_wait_for(&condrwlock_, &lock.rwock_,
147  timeout.ToTimeValue().tick, 0);
148  }
150  const TimeSpan& timeout) NLIB_NOEXCEPT {
151  return nlib_condrwlock_wait_for(&condrwlock_, &lock,
152  timeout.ToTimeValue().tick, 0);
153  }
154 
156  const DateTime& datetime) NLIB_NOEXCEPT {
157  return nlib_condrwlock_wait_until(&condrwlock_, &lock.rwock_,
158  datetime.ToTimeValue().tick, 1);
159  }
161  const DateTime& datetime) NLIB_NOEXCEPT {
162  return nlib_condrwlock_wait_until(&condrwlock_, &lock,
163  datetime.ToTimeValue().tick, 1);
164  }
166  const DateTime& datetime) NLIB_NOEXCEPT {
167  return nlib_condrwlock_wait_until(&condrwlock_, &lock.rwock_,
168  datetime.ToTimeValue().tick, 0);
169  }
171  const DateTime& datetime) NLIB_NOEXCEPT {
172  return nlib_condrwlock_wait_until(&condrwlock_, &lock,
173  datetime.ToTimeValue().tick, 0);
174  }
175 
176  private:
177 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
179 #else
180  nlib_condrwlock condrwlock_;
181 #endif
182 };
183 
184 
185 } // namespace threading
186 NLIB_NAMESPACE_END
187 
188 #endif // INCLUDE_NN_NLIB_THREADING_SHAREDCRITICALSECTION_H_
A conditional variable for SharedCriticalSection and nlib_rwlock. This variable can be used in the sa...
errno_t nlib_rwlock_rdunlock(nlib_rwlock *rwlock) NLIB_RELEASE_SHARED(*rwlock)
Releases the read lock.
NLIB_CHECK_RESULT errno_t WaitFor(nlib_rwlock &lock, const TimeSpan &timeout) noexcept
Unlocks a write lock and waits for a conditional variable signal for a specified period. Also see nlib_condrwlock_wait_for().
void lock_shared() noexcept NLIB_ACQUIRE_SHARED()
Gets the read lock, and enters the critical section. Blocks until it can get a lock.
errno_t nlib_condrwlock_wait(nlib_condrwlock *cond, nlib_rwlock *rwlock, int rdlock)
Unlocks rwlock and waits for a conditional variable. It then locks rwlock again after the execution r...
NLIB_CHECK_RESULT bool try_lock_shared() noexcept NLIB_TRY_ACQUIRE_SHARED(true)
Gets the read lock, and attempts to enter the critical section.
errno_t nlib_rwlock_tryrdlock(nlib_rwlock *rwlock) NLIB_TRY_ACQUIRE_SHARED(0
Gets the read lock, and attempts to enter the critical section.
errno_t nlib_condrwlock_wait_until(nlib_condrwlock *cond, nlib_rwlock *rwlock, nlib_time abstime, int rdlock)
Unlocks rwlock and waits for a conditional variable. It then locks rwlock again after the execution r...
struct nlib_rwlock_ nlib_rwlock
The type for a read-write lock object.
Definition: Platform.h:1113
NLIB_CHECK_RESULT bool try_lock_until(const DateTime &abstime) noexcept NLIB_TRY_ACQUIRE(true)
Gets a write lock, and attempts to enter the critical section. Times out.
void Notify() noexcept
Signals to at least one waiting thread. Also see nlib_condrwlock_signal().
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
Definition: Platform_unix.h:74
errno_t WaitReader(SharedCriticalSection &lock) noexcept
Unlocks the read lock and waits for a signal from the condition variable. Also see nlib_condrwlock_wa...
errno_t nlib_rwlock_wrlock(nlib_rwlock *rwlock) NLIB_ACQUIRE(*rwlock)
Gets a write lock, and enters the critical section. Blocks until it can get a lock.
NLIB_CHECK_RESULT errno_t WaitUntil(SharedCriticalSection &lock, const DateTime &datetime) noexcept
Unlocks a write lock and waits for a conditional variable signal until specified date and time...
errno_t nlib_rwlock_destroy(nlib_rwlock *rwlock) NLIB_EXCLUDES(*rwlock)
Destroys a read-write lock object.
errno_t nlib_condrwlock_wait_for(nlib_condrwlock *cond, nlib_rwlock *rwlock, nlib_duration duration, int rdlock)
Unlocks rwlock and waits for a conditional variable. It then locks rwlock again after the execution r...
errno_t nlib_condrwlock_signal(nlib_condrwlock *cond)
Resumes the execution of one thread that is waiting for the read-write lock conditional variable cond...
NLIB_CHECK_RESULT errno_t WaitReaderFor(nlib_rwlock &lock, const TimeSpan &timeout) noexcept
Unlocks a read lock and waits for a conditional variable signal for a specified period. Also see nlib_condrwlock_wait_for().
The class for representing the date and time.
Definition: DateTime.h:248
NLIB_CHECK_RESULT errno_t WaitReaderFor(SharedCriticalSection &lock, const TimeSpan &timeout) noexcept
Unlocks a read lock and waits for a conditional variable signal for a specified period. Also see nlib_condrwlock_wait_for().
Implements a read/write lock. Used when multiple threads simultaneously read data, and a single thread writes data.
errno_t nlib_condrwlock_init(nlib_condrwlock *cond)
Initializes a read-write lock conditional variable.
NLIB_CHECK_RESULT bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Gets a write lock, and attempts to enter the critical section.
errno_t WaitReader(nlib_rwlock &lock) noexcept
Unlocks the read lock and waits for a signal from the condition variable. Also see nlib_condrwlock_wa...
struct nlib_condrwlock_ nlib_condrwlock
Type of the conditional variable for read-write locks.
errno_t Wait(SharedCriticalSection &lock) noexcept
Unlocks a write lock and waits for a conditional variable signal. Also see nlib_condrwlock_wait().
#define NLIB_CONDRWLOCK_INITIALIZER
Constant for statically initializing nlib_condrwlock.
Definition: Platform.h:1283
errno_t nlib_rwlock_rdlock(nlib_rwlock *rwlock) NLIB_ACQUIRE_SHARED(*rwlock)
Gets the read lock, and enters the critical section. Blocks until it can get a lock.
#define NLIB_RWLOCK_INITIALIZER
Constant for statically initializing nlib_rwlock.
Definition: Platform.h:1115
errno_t nlib_rwlock_wrunlock(nlib_rwlock *rwlock) NLIB_RELEASE(*rwlock)
Releases a write lock.
NLIB_CHECK_RESULT errno_t WaitReaderUntil(nlib_rwlock &lock, const DateTime &datetime) noexcept
Unlocks a read lock and waits for a conditional variable signal until specified date and time...
Defines the class for handling times and durations.
errno_t Wait(nlib_rwlock &lock) noexcept
Unlocks a write lock and waits for a conditional variable signal. Also see nlib_condrwlock_wait().
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
errno_t nlib_rwlock_tryrdlock_for(nlib_rwlock *rwlock, nlib_duration duration) NLIB_TRY_ACQUIRE_SHARED(0
Gets the read lock, and attempts to enter the critical section. Times out.
A file that contains the configuration information for each development environment.
errno_t nlib_condrwlock_destroy(nlib_condrwlock *cond)
Destroys a read-write lock conditional variable.
void unlock_shared() noexcept NLIB_RELEASE_SHARED()
Releases the read lock.
errno_t nlib_rwlock_trywrlock_for(nlib_rwlock *rwlock, nlib_duration duration) NLIB_TRY_ACQUIRE(0
Gets a write lock, and attempts to enter the critical section. Times out.
NLIB_CHECK_RESULT errno_t WaitUntil(nlib_rwlock &lock, const DateTime &datetime) noexcept
Unlocks a write lock and waits for a conditional variable signal until specified date and time...
void unlock() noexcept NLIB_RELEASE()
Releases a write lock.
errno_t nlib_rwlock_trywrlock(nlib_rwlock *rwlock) NLIB_TRY_ACQUIRE(0
Gets a write lock, and attempts to enter the critical section.
NLIB_CHECK_RESULT bool try_lock_shared_for(const TimeSpan &timeout) noexcept NLIB_TRY_ACQUIRE_SHARED(true)
Gets the read lock, and attempts to enter the critical section. Times out.
errno_t nlib_rwlock_tryrdlock_until(nlib_rwlock *rwlock, nlib_time abstime) NLIB_TRY_ACQUIRE_SHARED(0
Gets the read lock, and attempts to enter the critical section. Times out.
errno_t nlib_condrwlock_broadcast(nlib_condrwlock *cond)
Resumes the execution of all threads that are waiting for the read-write lock conditional variable co...
void lock() noexcept NLIB_ACQUIRE()
Gets a write lock, and enters the critical section. Blocks until it can get a lock.
NLIB_CHECK_RESULT bool try_lock_shared_until(const DateTime &abstime) noexcept NLIB_TRY_ACQUIRE_SHARED(true)
Gets the read lock, and attempts to enter the critical section. Times out.
errno_t nlib_rwlock_init(nlib_rwlock *rwlock) NLIB_EXCLUDES(*rwlock)
Initializes a read-write lock object.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
The class for representing the time.
Definition: DateTime.h:93
NLIB_CHECK_RESULT errno_t WaitFor(SharedCriticalSection &lock, const TimeSpan &timeout) noexcept
Unlocks a write lock and waits for a conditional variable signal for a specified period. Also see nlib_condrwlock_wait_for().
NLIB_CHECK_RESULT errno_t WaitReaderUntil(SharedCriticalSection &lock, const DateTime &datetime) noexcept
Unlocks a read lock and waits for a conditional variable signal until specified date and time...
NLIB_CHECK_RESULT bool try_lock_for(const TimeSpan &timeout) noexcept NLIB_TRY_ACQUIRE(true)
Gets a write lock, and attempts to enter the critical section. Times out.
void NotifyAll() noexcept
Signals all waiting threads. Also see nlib_condrwlock_broadcast().
errno_t nlib_rwlock_trywrlock_until(nlib_rwlock *rwlock, nlib_time abstime) NLIB_TRY_ACQUIRE(0
Gets a write lock, and attempts to enter the critical section. Times out.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24