nlib
CondVarFallback.h
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_CONDVARFALLBACK_H_
4 #define INCLUDE_NN_NLIB_THREADING_CONDVARFALLBACK_H_
5 
6 #include "nn/nlib/UniquePtr.h"
7 #include "nn/nlib/DateTime.h"
8 
9 NLIB_NAMESPACE_BEGIN
10 namespace threading {
11 
12 class CondVarFallback NLIB_FINAL {
13  public:
15  NLIB_VIS_PUBLIC ~CondVarFallback() NLIB_NOEXCEPT;
16  NLIB_VIS_PUBLIC errno_t Init() NLIB_NOEXCEPT;
17  void Notify() NLIB_NOEXCEPT { this->signal_(false); }
18  void NotifyAll() NLIB_NOEXCEPT { this->signal_(true); }
19  template <class lock_type>
20  errno_t Wait(lock_type& lock) NLIB_NOEXCEPT { // NOLINT
21  return this->wait_(lock, NULL);
22  }
23  template <class lock_type>
24  errno_t WaitFor(lock_type& lock, // NOLINT
25  const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT {
26  return this->wait_(lock, &timeout);
27  }
28  template <class lock_type>
29  errno_t WaitUntil(lock_type& lock, // NOLINT
30  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT {
31  DateTime now;
32  errno_t e = DateTime::GetNow(&now);
33  if (e != 0) return e;
34  return this->WaitFor(lock, datetime - now);
35  }
36 
37  private:
38  template <class lock_type>
39  struct ScopedUnlock {
40  explicit ScopedUnlock(lock_type& lock) : m_Lock(lock) { // NOLINT
41  m_Lock.unlock();
42  } // NOLINT
43  ~ScopedUnlock() { m_Lock.lock(); }
44 
45  private:
46  lock_type& m_Lock;
47  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedUnlock);
48  };
49  errno_t signal_(bool bAll);
50  template <class lock_type>
51  errno_t wait_(lock_type& lock, const nlib_ns::TimeSpan* timeout); // NOLINT
52  errno_t wait_1();
53  errno_t wait_2(const nlib_ns::TimeSpan* timeout);
54 
55  private:
56  struct Data_;
57  UniquePtr<Data_> m_Obj;
58 };
59 
60 template <class lock_type>
61 inline errno_t CondVarFallback::wait_(lock_type& lock, // NOLINT
62  const nlib_ns::TimeSpan* timeout) {
63  errno_t e;
64  if (!m_Obj) return EINVAL;
65  e = this->wait_1();
66  if (e != 0) return e;
67  ScopedUnlock<lock_type> ulk(lock);
68  return this->wait_2(timeout);
69 }
70 
71 #ifdef NLIB_CONDVAR_USE_FALLBACK
72 typedef CondVarFallback CondVar;
73 #endif
74 
75 } // namespace threading
76 NLIB_NAMESPACE_END
77 
78 #endif // INCLUDE_NN_NLIB_THREADING_CONDVARFALLBACK_H_
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Platform.h:2151
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:126
#define NLIB_DEPRECATED
関数等がdeprecatedになったことを示します。
std::unique_ptrに相当するクラスが定義されています。
日時を表すクラスです。
Definition: DateTime.h:249
時刻や時間を扱うためのクラスが定義されています。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
時間を表すクラスです。
Definition: DateTime.h:93
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24