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>
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
52  errno_t e = nlib_cond_init(&cond_);
53  NLIB_ASSERT_NOERR(e);
54  NLIB_UNUSED(e);
55  }
56 #endif
58  errno_t e = nlib_cond_destroy(&cond_);
59  NLIB_ASSERT_NOERR(e);
60  NLIB_UNUSED(e);
61  }
63  errno_t e;
64  e = nlib_cond_signal(&cond_);
65  NLIB_UNUSED(e);
66  NLIB_ASSERT_NOERR(e);
67  }
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) {
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>
85  const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT
86  NLIB_REQUIRES(lock) {
87  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
88  }
89  template<class lock_type>
91  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT
92  NLIB_REQUIRES(lock) {
93  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock), datetime.ToTimeValue().tick);
94  }
95 
96  private:
97 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
99 #else
100  nlib_cond cond_;
101 #endif
103 };
104 
105 #ifndef NLIB_DOXYGEN
106 template<>
108 CondVar::WaitFor(SimpleCriticalSection& lock, const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT
109  NLIB_REQUIRES(lock) {
110  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
111 }
112 
113 template<>
115 CondVar::WaitUntil(SimpleCriticalSection& lock, const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT
116  NLIB_REQUIRES(lock) {
117  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock), datetime.ToTimeValue().tick);
118 }
119 #endif
120 
121 #endif
122 
123 } // namespace threading
124 NLIB_NAMESPACE_END
125 
126 #endif // INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
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:183
ミューテックス, 再入可能ミューテックス, 再入とタイムアウトが可能なミューテックスを実装しています。 ...
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:231
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:109
pthread_cond_t nlib_cond
条件変数オブジェクトの型です。
開発環境別の設定が書かれるファイルです。
errno_t Wait(lock_type &lock) noexcept NLIB_REQUIRES(lock)
ウェイトします。
Definition: CondVar.h:76
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:250
#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:90
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