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
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Defines that class that is corresponding to std::unique_ptr.
The class for representing the date and time.
Definition: DateTime.h:249
Defines the class for handling times and durations.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
The class for representing the time.
Definition: DateTime.h:93
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24