nlib
CondVar.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
4 #define INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/DateTime.h"
9 
10 #include <new> // NOLINT
11 
12 NLIB_NAMESPACE_BEGIN
13 namespace threading {
14 
15 #ifndef NLIB_CONDVAR_USE_FALLBACK
16 // code snippets:
17 // Initialization:
18 // CondVar cond;
19 // SimpleCriticalSection m;
20 // bool flag = false;
21 // Wating thread:
22 // m.lock();
23 // while (!flag)
24 // if (cond.Wait(m) != 0) { error; .... }
25 // # note that cond.Wait() may return without signal notified
26 // flag = false;
27 // m.unlock();
28 // Notifying thread:
29 // m.lock();
30 // flag = true;
31 // cond.NotifyAll();
32 // m.unlock();
34  public:
35 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
36  constexpr CondVar() NLIB_NOEXCEPT = default;
37 #else
38  CondVar() NLIB_NOEXCEPT {
39  errno_t e = nlib_cond_init(&cond_);
40  NLIB_ASSERT_NOERR(e);
41  NLIB_UNUSED(e);
42  }
43 #endif
44  ~CondVar() NLIB_NOEXCEPT {
45  errno_t e = nlib_cond_destroy(&cond_);
46  NLIB_ASSERT_NOERR(e);
47  NLIB_UNUSED(e);
48  }
49  void Notify() NLIB_NOEXCEPT {
50  errno_t e;
51  e = nlib_cond_signal(&cond_);
52  NLIB_UNUSED(e);
53  NLIB_ASSERT_NOERR(e);
54  }
55  void NotifyAll() NLIB_NOEXCEPT {
56  errno_t e;
57  e = nlib_cond_broadcast(&cond_);
58  NLIB_UNUSED(e);
59  NLIB_ASSERT_NOERR(e);
60  }
61 
62  template <class lock_type>
63  errno_t Wait(lock_type& lock) NLIB_NOEXCEPT NLIB_REQUIRES(lock) { // NOLINT
64  errno_t e;
65  e = nlib_cond_wait(&cond_, detail::GetRawMutex(lock));
66  NLIB_UNUSED(e);
67  NLIB_ASSERT_NOERR(e);
68  return 0;
69  }
70  template <class lock_type>
71  NLIB_CHECK_RESULT errno_t WaitFor(lock_type& lock, const nlib_ns::TimeSpan& timeout) // NOLINT
72  NLIB_NOEXCEPT NLIB_REQUIRES(lock) { // NOLINT
73  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
74  }
75  template <class lock_type>
76  NLIB_CHECK_RESULT errno_t WaitUntil(lock_type& lock, // NOLINT
77  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT NLIB_REQUIRES(lock) {
78  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock),
79  datetime.ToTimeValue().tick);
80  }
81 
82  private:
83 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
85 #else
86  nlib_cond cond_;
87 #endif
88 };
89 
90 #ifndef NLIB_DOXYGEN
91 template <>
92 inline NLIB_CHECK_RESULT errno_t CondVar::WaitFor(SimpleCriticalSection& lock, // NOLINT
93  const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT
94  NLIB_REQUIRES(lock) {
95  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
96 }
97 
98 template <>
99 inline NLIB_CHECK_RESULT errno_t CondVar::WaitUntil(SimpleCriticalSection& lock, // NOLINT
100  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT
101  NLIB_REQUIRES(lock) {
102  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock), datetime.ToTimeValue().tick);
103 }
104 #endif
105 
106 #endif
107 
108 } // namespace threading
109 NLIB_NAMESPACE_END
110 
111 #endif // INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
int64_t tick
These can be used for nlib_time and nlib_duration.
Definition: DateTime.h:56
TimeValue ToTimeValue() const noexcept
Converts to a TimeValue object.
Definition: DateTime.h:112
void NotifyAll() noexcept
Signals all waiting threads.
Definition: CondVar.h:55
errno_t nlib_cond_signal(nlib_cond *cond)
Resumes the execution of one thread that is waiting for condition variable cond.
void Notify() noexcept
Signals to at least one waiting thread.
Definition: CondVar.h:49
Implements mutex, reentrant timeout mutex, and reentrant mutex.
Simplest critical section. Not reentrant.
errno_t nlib_cond_broadcast(nlib_cond *cond)
Resumes the execution of all threads that are waiting for the conditional variable cond...
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
Definition: Platform_unix.h:74
errno_t nlib_cond_init(nlib_cond *cond)
Initializes a condition variable.
Conditional variable for synchronization.
Definition: CondVar.h:33
The class for representing the date and time.
Definition: DateTime.h:248
errno_t nlib_cond_wait(nlib_cond *cond, nlib_mutex *mutex) NLIB_REQUIRES(*mutex)
Unlocks mutex and waits for a condition variable. It then relocks mutex after execution resumes...
NLIB_CHECK_RESULT errno_t nlib_cond_wait_for(nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration) NLIB_REQUIRES(*mutex)
Unlocks mutex and waits for just the duration amount of time for a condition variable. It then relocks mutex after execution resumes.
NLIB_CHECK_RESULT errno_t WaitFor(lock_type &lock, const nlib_ns::TimeSpan &timeout) noexcept NLIB_REQUIRES(lock)
Waits with a specified timeout.
Definition: CondVar.h:71
errno_t nlib_cond_destroy(nlib_cond *cond)
Destroys a condition variable object.
Defines the class for handling times and durations.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
pthread_cond_t nlib_cond
The type for a condition variable object.
A file that contains the configuration information for each development environment.
errno_t Wait(lock_type &lock) noexcept NLIB_REQUIRES(lock)
Waits.
Definition: CondVar.h:63
TimeValue ToTimeValue() const noexcept
Returns a TimeValue object.
Definition: DateTime.h:289
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
#define NLIB_COND_INITIALIZER
Constant for statically initializing nlib_cond.
The class for representing the time.
Definition: DateTime.h:93
NLIB_CHECK_RESULT errno_t WaitUntil(lock_type &lock, const nlib_ns::DateTime &datetime) noexcept NLIB_REQUIRES(lock)
Waits with a timeout specified in date/time.
Definition: CondVar.h:76
NLIB_CHECK_RESULT errno_t nlib_cond_wait_until(nlib_cond *cond, nlib_mutex *mutex, nlib_time abstime) NLIB_REQUIRES(*mutex)
Unlocks mutex and waits until abstime for a condition variable. It then relocks mutex after execution...
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24