nlib
CondVar.h
[詳解]
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
nlib_time, nlib_durationとして利用することができます。
Definition: DateTime.h:56
TimeValue ToTimeValue() const noexcept
TimeValueオブジェクトに変換します。
Definition: DateTime.h:112
void NotifyAll() noexcept
ウェイトしているスレッド全てにシグナルします。
Definition: CondVar.h:55
errno_t nlib_cond_signal(nlib_cond *cond)
条件変数cond を待っているスレッドの1つの実行を再開させます。
void Notify() noexcept
ウェイトしているスレッドの1つ以上にシグナルします。
Definition: CondVar.h:49
ミューテックス, 再入可能ミューテックス, 再入とタイムアウトが可能なミューテックスを実装しています。 ...
最も単純なクリティカルセクションです。リエントラントではありません。
errno_t nlib_cond_broadcast(nlib_cond *cond)
条件変数cond を待っているスレッド全ての実行を再開させます。
#define NLIB_CHECK_RESULT
関数の呼び出し元が戻り値をチェックする必要があることを示します。
Definition: Platform_unix.h:74
errno_t nlib_cond_init(nlib_cond *cond)
条件変数を初期化します。
同期処理を行うための条件変数です。
Definition: CondVar.h:33
日時を表すクラスです。
Definition: DateTime.h:248
errno_t nlib_cond_wait(nlib_cond *cond, nlib_mutex *mutex) NLIB_REQUIRES(*mutex)
mutexをアンロックし、条件変数を待機します。実行が再開したらmutexを再ロックします。 ...
NLIB_CHECK_RESULT errno_t nlib_cond_wait_for(nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration) NLIB_REQUIRES(*mutex)
mutexをアンロックし、条件変数をduration の間だけ待機します。実行が再開したらmutexを再ロックします。 ...
NLIB_CHECK_RESULT errno_t WaitFor(lock_type &lock, const nlib_ns::TimeSpan &timeout) noexcept NLIB_REQUIRES(lock)
期間を指定したタイムアウトつきのウェイトをします。
Definition: CondVar.h:71
errno_t nlib_cond_destroy(nlib_cond *cond)
条件変数オブジェクトを破壊します。
時刻や時間を扱うためのクラスが定義されています。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:86
pthread_cond_t nlib_cond
条件変数オブジェクトの型です。
開発環境別の設定が書かれるファイルです。
errno_t Wait(lock_type &lock) noexcept NLIB_REQUIRES(lock)
ウェイトします。
Definition: CondVar.h:63
TimeValue ToTimeValue() const noexcept
TimeValueオブジェクトを返します。
Definition: DateTime.h:289
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:211
#define NLIB_COND_INITIALIZER
nlib_cond を静的に初期化するための定数です。
時間を表すクラスです。
Definition: DateTime.h:93
NLIB_CHECK_RESULT errno_t WaitUntil(lock_type &lock, const nlib_ns::DateTime &datetime) noexcept NLIB_REQUIRES(lock)
日時を指定したタイムアウトつきのウェイトをします。
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)
mutexをアンロックし、条件変数をabstimeまで待機します。実行が再開したらmutexを再ロックします。 ...
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24