nlib
CondVar.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
17 #define INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/DateTime.h"
22 
23 #include <new> // NOLINT
24 
25 NLIB_NAMESPACE_BEGIN
26 namespace threading {
27 
28 #ifndef NLIB_CONDVAR_USE_FALLBACK
29 // code snippets:
30 // Initialization:
31 // CondVar cond;
32 // SimpleCriticalSection m;
33 // bool flag = false;
34 // Wating thread:
35 // m.lock();
36 // while (!flag)
37 // if (cond.Wait(m) != 0) { error; .... }
38 // # note that cond.Wait() may return without signal notified
39 // flag = false;
40 // m.unlock();
41 // Notifying thread:
42 // m.lock();
43 // flag = true;
44 // cond.NotifyAll();
45 // m.unlock();
47  public:
48 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
49  constexpr CondVar() NLIB_NOEXCEPT = default;
50 #else
51  CondVar() NLIB_NOEXCEPT {
52  errno_t e = nlib_cond_init(&cond_);
53  NLIB_ASSERT_NOERR(e);
54  NLIB_UNUSED(e);
55  }
56 #endif
57  ~CondVar() NLIB_NOEXCEPT {
58  errno_t e = nlib_cond_destroy(&cond_);
59  NLIB_ASSERT_NOERR(e);
60  NLIB_UNUSED(e);
61  }
62  void Notify() NLIB_NOEXCEPT {
63  errno_t e;
64  e = nlib_cond_signal(&cond_);
65  NLIB_UNUSED(e);
66  NLIB_ASSERT_NOERR(e);
67  }
68  void NotifyAll() NLIB_NOEXCEPT {
69  errno_t e;
70  e = nlib_cond_broadcast(&cond_);
71  NLIB_UNUSED(e);
72  NLIB_ASSERT_NOERR(e);
73  }
74 
75  template <class lock_type>
76  errno_t Wait(lock_type& lock) NLIB_NOEXCEPT NLIB_REQUIRES(lock) { // NOLINT
77  errno_t e;
78  e = nlib_cond_wait(&cond_, detail::GetRawMutex(lock));
79  NLIB_UNUSED(e);
80  NLIB_ASSERT_NOERR(e);
81  return 0;
82  }
83  template <class lock_type>
84  NLIB_CHECK_RESULT errno_t WaitFor(lock_type& lock, const nlib_ns::TimeSpan& timeout) // NOLINT
85  NLIB_NOEXCEPT NLIB_REQUIRES(lock) { // NOLINT
86  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
87  }
88  template <class lock_type>
89  NLIB_CHECK_RESULT errno_t WaitUntil(lock_type& lock, // NOLINT
90  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT NLIB_REQUIRES(lock) {
91  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock),
92  datetime.ToTimeValue().tick);
93  }
94 
95  private:
96 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
98 #else
99  nlib_cond cond_;
100 #endif
102 };
103 
104 #ifndef NLIB_DOXYGEN
105 template <>
106 NLIB_CHECK_RESULT inline errno_t CondVar::WaitFor(SimpleCriticalSection& lock, // NOLINT
107  const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT
108  NLIB_REQUIRES(lock) {
109  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
110 }
111 
112 template <>
113 NLIB_CHECK_RESULT inline errno_t CondVar::WaitUntil(SimpleCriticalSection& lock, // NOLINT
114  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT
115  NLIB_REQUIRES(lock) {
116  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock), datetime.ToTimeValue().tick);
117 }
118 #endif
119 
120 #endif
121 
122 } // namespace threading
123 NLIB_NAMESPACE_END
124 
125 #endif // INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
int64_t tick
nlib_time, nlib_durationとして利用することができます。
Definition: DateTime.h:70
TimeValue ToTimeValue() const noexcept
TimeValueオブジェクトに変換します。
Definition: DateTime.h:116
void NotifyAll() noexcept
ウェイトしているスレッド全てにシグナルします。
Definition: CondVar.h:68
errno_t nlib_cond_signal(nlib_cond *cond)
条件変数cond を待っているスレッドの1つの実行を再開させます。
void Notify() noexcept
ウェイトしているスレッドの1つ以上にシグナルします。
Definition: CondVar.h:62
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
ミューテックス, 再入可能ミューテックス, 再入とタイムアウトが可能なミューテックスを実装しています。 ...
最も単純なクリティカルセクションです。リエントラントではありません。
errno_t nlib_cond_broadcast(nlib_cond *cond)
条件変数cond を待っているスレッド全ての実行を再開させます。
#define NLIB_CHECK_RESULT
関数の呼び出し元が戻り値をチェックする必要があることを示します。
errno_t nlib_cond_init(nlib_cond *cond)
条件変数を初期化します。
同期処理を行うための条件変数です。
Definition: CondVar.h:46
日時を表すクラスです。
Definition: DateTime.h:246
errno_t nlib_cond_wait(nlib_cond *cond, nlib_mutex *mutex) NLIB_REQUIRES(*mutex)
mutexをアンロックし、条件変数を待機します。実行が再開したらmutexを再ロックします。 ...
errno_t nlib_cond_wait_for(nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration) NLIB_REQUIRES(*mutex)
mutexをアンロックし、条件変数をduration の間だけ待機します。実行が再開したらmutexを再ロックします。 ...
errno_t WaitFor(lock_type &lock, const nlib_ns::TimeSpan &timeout) noexcept NLIB_REQUIRES(lock)
期間を指定したタイムアウトつきのウェイトをします。
Definition: CondVar.h:84
errno_t nlib_cond_destroy(nlib_cond *cond)
条件変数オブジェクトを破壊します。
時刻や時間を扱うためのクラスが定義されています。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
pthread_cond_t nlib_cond
条件変数オブジェクトの型です。
開発環境別の設定が書かれるファイルです。
errno_t Wait(lock_type &lock) noexcept NLIB_REQUIRES(lock)
ウェイトします。
Definition: CondVar.h:76
TimeValue ToTimeValue() const noexcept
TimeValueオブジェクトを返します。
Definition: DateTime.h:295
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
#define NLIB_COND_INITIALIZER
nlib_cond を静的に初期化するための定数です。
時間を表すクラスです。
Definition: DateTime.h:97
errno_t WaitUntil(lock_type &lock, const nlib_ns::DateTime &datetime) noexcept NLIB_REQUIRES(lock)
日時を指定したタイムアウトつきのウェイトをします。
Definition: CondVar.h:89
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:37