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"
8 #include "nn/nlib/threading/CondVarFallback.h"
10 
11 #include <new> // NOLINT
12 
13 NLIB_NAMESPACE_BEGIN
14 namespace threading {
15 
16 #ifndef NLIB_CONDVAR_USE_FALLBACK
17 // code snippets:
18 // Initialization:
19 // CondVar cond;
20 // SimpleCriticalSection m;
21 // bool flag = false;
22 // Wating thread:
23 // m.lock();
24 // while (!flag)
25 // if (cond.Wait(m) != 0) { error; .... }
26 // # note that cond.Wait() may return without signal notified
27 // flag = false;
28 // m.unlock();
29 // Notifying thread:
30 // m.lock();
31 // flag = true;
32 // cond.NotifyAll();
33 // m.unlock();
35  public:
36 #ifdef NLIB_CXX11_CONSTEXPR
37  constexpr CondVar() NLIB_NOEXCEPT : m_Cond(NLIB_COND_INITIALIZER) {}
38 #else
40  errno_t e = nlib_cond_init(&m_Cond);
41  NLIB_ASSERT_NOERR(e);
42  NLIB_UNUSED(e);
43  }
44 #endif
45  ~CondVar() NLIB_NOEXCEPT {
46  errno_t e = nlib_cond_destroy(&m_Cond);
47  NLIB_ASSERT_NOERR(e);
48  NLIB_UNUSED(e);
49  }
51  errno_t e;
52  e = nlib_cond_signal(&m_Cond);
53  NLIB_UNUSED(e);
54  NLIB_ASSERT_NOERR(e);
55  }
57  errno_t e;
58  e = nlib_cond_broadcast(&m_Cond);
59  NLIB_UNUSED(e);
60  NLIB_ASSERT_NOERR(e);
61  }
62 
63  template <class lock_type>
64  errno_t Wait(lock_type& lock) NLIB_NOEXCEPT { // NOLINT
65  errno_t e;
66  e = nlib_cond_wait(&m_Cond, detail::GetRawMutex(lock));
67  NLIB_UNUSED(e);
68  NLIB_ASSERT_NOERR(e);
69  return 0;
70  }
71  template <class lock_type>
72  errno_t
73  WaitFor(lock_type& lock, const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT { // NOLINT
74  return nlib_cond_wait_for(&m_Cond, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
75  }
76  template <class lock_type>
77  errno_t WaitUntil(lock_type& lock, // NOLINT
78  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT {
79  return nlib_cond_wait_until(&m_Cond, detail::GetRawMutex(lock),
80  datetime.ToTimeValue().tick);
81  }
82 
83  private:
84  nlib_cond m_Cond;
85 };
86 
87 #ifndef NLIB_DOXYGEN
88 template <>
89 inline errno_t CondVar::WaitFor(SimpleCriticalSection& lock, // NOLINT
90  const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT
91  NLIB_LOCK_REQUIRED(lock) {
92  return nlib_cond_wait_for(&m_Cond, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
93 }
94 
95 template <>
96 inline errno_t CondVar::WaitUntil(SimpleCriticalSection& lock, // NOLINT
97  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT
98  NLIB_LOCK_REQUIRED(lock) {
99  return nlib_cond_wait_until(&m_Cond, detail::GetRawMutex(lock), datetime.ToTimeValue().tick);
100 }
101 #endif
102 
103 #endif
104 
105 } // namespace threading
106 NLIB_NAMESPACE_END
107 
108 #endif // INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
void NotifyAll() noexcept
Signals all waiting threads.
Definition: CondVar.h:56
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:50
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Implements mutex, reentrant timeout mutex, and reentrant mutex.
errno_t nlib_cond_broadcast(nlib_cond *cond)
Resumes the execution of all threads that are waiting for the conditional variable cond...
errno_t nlib_cond_init(nlib_cond *cond)
Initializes a condition variable.
NLIB_CHECK_RESULT errno_t nlib_cond_wait_for(nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration)
Unlocks mutex and waits for just the duration amount of time for a condition variable. It then relocks mutex after execution resumes.
Conditional variable for synchronization.
Definition: CondVar.h:34
The class for representing the date and time.
Definition: DateTime.h:249
errno_t nlib_cond_wait(nlib_cond *cond, nlib_mutex *mutex)
Unlocks mutex and waits for a condition variable. It then relocks mutex after execution resumes...
errno_t Wait(lock_type &lock) noexcept
Waits.
Definition: CondVar.h:64
errno_t nlib_cond_destroy(nlib_cond *cond)
Destroys a condition variable object.
constexpr CondVar() noexcept
Instantiates the object with default parameters (default constructor).
Definition: CondVar.h:37
Defines the class for handling times and durations.
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 WaitUntil(lock_type &lock, const nlib_ns::DateTime &datetime) noexcept
Waits with a timeout specified in date/time.
Definition: CondVar.h:77
errno_t WaitFor(lock_type &lock, const nlib_ns::TimeSpan &timeout) noexcept
Waits with a specified timeout.
Definition: CondVar.h:73
#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 nlib_cond_wait_until(nlib_cond *cond, nlib_mutex *mutex, nlib_time abstime)
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