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_LOCKABLE SharedCriticalSection NLIB_FINAL {
13  public:
14 #ifdef NLIB_CXX11_CONSTEXPR
16  : rwock_(NLIB_RWLOCK_INITIALIZER) {}
17 #else
19  errno_t e = nlib_rwlock_init(&rwock_);
20  NLIB_ASSERT_NOERR(e);
21  NLIB_UNUSED(e);
22  }
23 #endif
24  ~SharedCriticalSection() NLIB_NOEXCEPT {
25  errno_t e = nlib_rwlock_destroy(&rwock_);
26  NLIB_ASSERT_NOERR(e);
27  NLIB_UNUSED(e);
28  }
29 
30  void lock_shared() NLIB_NOEXCEPT NLIB_SHARED_LOCK_FUNC() {
31  errno_t e = nlib_rwlock_rdlock(&rwock_);
32  NLIB_UNUSED(e);
33  NLIB_ASSERT_NOERR(e);
34  }
35  bool try_lock_shared() NLIB_NOEXCEPT NLIB_SHARED_TRYLOCK_FUNC(true) {
36  errno_t e = nlib_rwlock_tryrdlock(&rwock_);
37  NLIB_ASSERT(e == 0 || e == EBUSY);
38  return e == 0;
39  }
41  NLIB_SHARED_TRYLOCK_FUNC(true) {
42  errno_t e = nlib_rwlock_tryrdlock_for(&rwock_, timeout.ToTimeValue().tick);
43  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
44  return e == 0;
45  }
47  NLIB_SHARED_TRYLOCK_FUNC(true) {
48  errno_t e = nlib_rwlock_tryrdlock_until(&rwock_, abstime.ToTimeValue().tick);
49  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
50  return e == 0;
51  }
52  void unlock_shared() NLIB_NOEXCEPT NLIB_SHARED_UNLOCK_FUNC() {
53  errno_t e = nlib_rwlock_rdunlock(&rwock_);
54  NLIB_UNUSED(e);
55  NLIB_ASSERT_NOERR(e);
56  }
57 
58  void lock() NLIB_NOEXCEPT NLIB_LOCK_FUNC() {
59  errno_t e = nlib_rwlock_wrlock(&rwock_);
60  NLIB_UNUSED(e);
61  NLIB_ASSERT_NOERR(e);
62  }
63  bool try_lock() NLIB_NOEXCEPT NLIB_TRYLOCK_FUNC(true) {
64  errno_t e = nlib_rwlock_trywrlock(&rwock_);
65  NLIB_ASSERT(e == 0 || e == EBUSY);
66  return e == 0;
67  }
68  bool try_lock_for(const TimeSpan& timeout) NLIB_NOEXCEPT NLIB_TRYLOCK_FUNC(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  }
73  bool try_lock_until(const DateTime& abstime) NLIB_NOEXCEPT NLIB_TRYLOCK_FUNC(true) {
74  errno_t e = nlib_rwlock_trywrlock_until(&rwock_, abstime.ToTimeValue().tick);
75  NLIB_ASSERT(e == 0 || e == ETIMEDOUT);
76  return e == 0;
77  }
78  void unlock() NLIB_NOEXCEPT NLIB_UNLOCK_FUNC() {
79  errno_t e = nlib_rwlock_wrunlock(&rwock_);
80  NLIB_UNUSED(e);
81  NLIB_ASSERT_NOERR(e);
82  }
83 
84  private:
85  nlib_rwlock rwock_;
87 };
88 
90  public:
91 #ifdef NLIB_CXX11_CONSTEXPR
93  : condrwlock_(NLIB_CONDRWLOCK_INITIALIZER) {}
94 #else
96  errno_t e = nlib_condrwlock_init(&condrwlock_);
97  NLIB_ASSERT_NOERR(e);
98  NLIB_UNUSED(e);
99  }
100 #endif
102  errno_t e = nlib_condrwlock_destroy(&condrwlock_);
103  NLIB_ASSERT_NOERR(e);
104  NLIB_UNUSED(e);
105  }
107  errno_t e = nlib_condrwlock_signal(&condrwlock_);
108  NLIB_UNUSED(e);
109  NLIB_ASSERT_NOERR(e);
110  }
112  errno_t e = nlib_condrwlock_broadcast(&condrwlock_);
113  NLIB_UNUSED(e);
114  NLIB_ASSERT_NOERR(e);
115  }
116 
118  return nlib_condrwlock_wait(&condrwlock_, &lock.rwock_, 1);
119  }
121  return nlib_condrwlock_wait(&condrwlock_, &lock, 1);
122  }
124  return nlib_condrwlock_wait(&condrwlock_, &lock.rwock_, 0);
125  }
127  return nlib_condrwlock_wait(&condrwlock_, &lock, 0);
128  }
129 
131  return nlib_condrwlock_wait_for(&condrwlock_, &lock.rwock_,
132  timeout.ToTimeValue().tick, 1);
133  }
134  errno_t WaitReaderFor(nlib_rwlock& lock, const TimeSpan& timeout) NLIB_NOEXCEPT { // NOLINT
135  return nlib_condrwlock_wait_for(&condrwlock_, &lock,
136  timeout.ToTimeValue().tick, 1);
137  }
138  errno_t WaitFor(SharedCriticalSection& lock, const TimeSpan& timeout) NLIB_NOEXCEPT { // NOLINT
139  return nlib_condrwlock_wait_for(&condrwlock_, &lock.rwock_,
140  timeout.ToTimeValue().tick, 0);
141  }
142  errno_t WaitFor(nlib_rwlock& lock, const TimeSpan& timeout) NLIB_NOEXCEPT { // NOLINT
143  return nlib_condrwlock_wait_for(&condrwlock_, &lock,
144  timeout.ToTimeValue().tick, 0);
145  }
146 
148  return nlib_condrwlock_wait_until(&condrwlock_, &lock.rwock_,
149  datetime.ToTimeValue().tick, 1);
150  }
151  errno_t WaitReaderUntil(nlib_rwlock& lock, const DateTime& datetime) NLIB_NOEXCEPT { // NOLINT
152  return nlib_condrwlock_wait_until(&condrwlock_, &lock,
153  datetime.ToTimeValue().tick, 1);
154  }
155  errno_t WaitUntil(SharedCriticalSection& lock, const DateTime& datetime) NLIB_NOEXCEPT { // NOLINT
156  return nlib_condrwlock_wait_until(&condrwlock_, &lock.rwock_,
157  datetime.ToTimeValue().tick, 0);
158  }
159  errno_t WaitUntil(nlib_rwlock& lock, const DateTime& datetime) NLIB_NOEXCEPT { // NOLINT
160  return nlib_condrwlock_wait_until(&condrwlock_, &lock,
161  datetime.ToTimeValue().tick, 0);
162  }
163 
164  private:
165  nlib_condrwlock condrwlock_;
166 };
167 
168 
169 } // namespace threading
170 NLIB_NAMESPACE_END
171 
172 #endif // INCLUDE_NN_NLIB_THREADING_SHAREDCRITICALSECTION_H_
A conditional variable for SharedCriticalSection and nlib_rwlock. This variable can be used in the sa...
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
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().
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...
errno_t nlib_rwlock_destroy(nlib_rwlock *rwlock)
Destroys a read-write lock object.
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:689
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
constexpr CondVarForSharedCriticalSection() noexcept
Instantiates the object with default parameters (default constructor).
errno_t nlib_rwlock_rdlock(nlib_rwlock *rwlock)
Gets the read lock, and enters the critical section. Blocks until it can get a lock.
void Notify() noexcept
Signals to at least one waiting thread. Also see nlib_condrwlock_signal().
errno_t nlib_rwlock_trywrlock_until(nlib_rwlock *rwlock, nlib_time abstime)
Gets a write lock, and attempts to enter the critical section. Times out.
void unlock_shared() noexcept NLIB_SHARED_UNLOCK_FUNC()
Releases the read lock.
errno_t WaitReader(SharedCriticalSection &lock) noexcept
Unlocks a read lock and waits for a conditional variable signal. Also see nlib_condrwlock_wait().
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_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...
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:249
errno_t nlib_rwlock_tryrdlock(nlib_rwlock *rwlock)
Gets the read lock, and attempts to enter the critical section.
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_rwlock_trywrlock(nlib_rwlock *rwlock)
Gets a write lock, and attempts to enter the critical section.
errno_t nlib_condrwlock_init(nlib_condrwlock *cond)
Initializes a read-write lock conditional variable.
errno_t WaitReader(nlib_rwlock &lock) noexcept
Unlocks a read lock and waits for a conditional variable signal. Also see nlib_condrwlock_wait().
bool try_lock_shared_until(const DateTime &abstime) noexcept NLIB_SHARED_TRYLOCK_FUNC(true)
Gets the read lock, and attempts to enter the critical section. Times out.
void lock() noexcept NLIB_LOCK_FUNC()
Gets a write lock, and enters the critical section. Blocks until it can get a lock.
errno_t nlib_rwlock_wrunlock(nlib_rwlock *rwlock)
Releases a write lock.
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_RWLOCK_INITIALIZER
Constant for statically initializing nlib_rwlock.
Definition: Platform.h:691
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().
errno_t nlib_rwlock_tryrdlock_until(nlib_rwlock *rwlock, nlib_time abstime)
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 lock_shared() noexcept NLIB_SHARED_LOCK_FUNC()
Gets the read lock, and enters the critical section. Blocks until it can get a lock.
bool try_lock_until(const DateTime &abstime) noexcept NLIB_TRYLOCK_FUNC(true)
Gets a write lock, and attempts to enter the critical section. Times out.
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...
bool try_lock_for(const TimeSpan &timeout) noexcept NLIB_TRYLOCK_FUNC(true)
Gets a write lock, and attempts to enter the critical section. Times out.
errno_t nlib_rwlock_rdunlock(nlib_rwlock *rwlock)
Releases the read lock.
void unlock() noexcept NLIB_UNLOCK_FUNC()
Releases a write lock.
bool try_lock_shared() noexcept NLIB_SHARED_TRYLOCK_FUNC(true)
Gets the read lock, and attempts to enter the critical section.
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...
The class for representing the time.
Definition: DateTime.h:93
constexpr SharedCriticalSection() noexcept
Instantiates the object with default parameters (default constructor).
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().
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...
errno_t nlib_rwlock_init(nlib_rwlock *rwlock)
Initializes a read-write lock object.
errno_t nlib_rwlock_trywrlock_for(nlib_rwlock *rwlock, nlib_duration duration)
Gets a write lock, and attempts to enter the critical section. Times out.
bool try_lock_shared_for(const TimeSpan &timeout) noexcept NLIB_SHARED_TRYLOCK_FUNC(true)
Gets the read lock, and attempts to enter the critical section. Times out.
errno_t nlib_rwlock_tryrdlock_for(nlib_rwlock *rwlock, nlib_duration duration)
Gets the read lock, and attempts to enter the critical section. Times out.
errno_t nlib_rwlock_wrlock(nlib_rwlock *rwlock)
Gets a write lock, and enters the critical section. Blocks until it can get a lock.
void NotifyAll() noexcept
Signals all waiting threads. Also see nlib_condrwlock_broadcast().
bool try_lock() noexcept NLIB_TRYLOCK_FUNC(true)
Gets a write lock, and attempts to enter the critical section.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24