nlib
CondVar.h
Go to the documentation of this file.
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
These can be used for nlib_time and nlib_duration.
Definition: DateTime.h:70
TimeValue ToTimeValue() const noexcept
Converts to a TimeValue object.
Definition: DateTime.h:116
void NotifyAll() noexcept
Signals all waiting threads.
Definition: CondVar.h:68
errno_t nlib_cond_signal(nlib_cond *cond)
Resumes the execution of one thread that is waiting for condition variable cond.
void Notify() noexcept
Signals to at least one waiting thread.
Definition: CondVar.h:62
#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:179
Implements mutex, reentrant timeout mutex, and reentrant mutex.
Simplest critical section. Not reentrant.
errno_t nlib_cond_broadcast(nlib_cond *cond)
Resumes the execution of all threads that are waiting for the conditional variable cond...
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
errno_t nlib_cond_init(nlib_cond *cond)
Initializes a condition variable.
Conditional variable for synchronization.
Definition: CondVar.h:46
The class for representing the date and time.
Definition: DateTime.h:246
errno_t nlib_cond_wait(nlib_cond *cond, nlib_mutex *mutex) NLIB_REQUIRES(*mutex)
Unlocks mutex and waits for a condition variable. It then relocks mutex after execution resumes...
errno_t nlib_cond_wait_for(nlib_cond *cond, nlib_mutex *mutex, nlib_duration duration) NLIB_REQUIRES(*mutex)
Unlocks mutex and waits for just the duration amount of time for a condition variable. It then relocks mutex after execution resumes.
errno_t WaitFor(lock_type &lock, const nlib_ns::TimeSpan &timeout) noexcept NLIB_REQUIRES(lock)
Waits with a specified timeout.
Definition: CondVar.h:84
errno_t nlib_cond_destroy(nlib_cond *cond)
Destroys a condition variable object.
Defines the class for handling times and durations.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
pthread_cond_t nlib_cond
The type for a condition variable object.
A file that contains the configuration information for each development environment.
errno_t Wait(lock_type &lock) noexcept NLIB_REQUIRES(lock)
Waits.
Definition: CondVar.h:76
TimeValue ToTimeValue() const noexcept
Returns a TimeValue object.
Definition: DateTime.h:295
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
#define NLIB_COND_INITIALIZER
Constant for statically initializing nlib_cond.
The class for representing the time.
Definition: DateTime.h:97
errno_t WaitUntil(lock_type &lock, const nlib_ns::DateTime &datetime) noexcept NLIB_REQUIRES(lock)
Waits with a timeout specified in date/time.
Definition: CondVar.h:89
errno_t nlib_cond_wait_until(nlib_cond *cond, nlib_mutex *mutex, nlib_time abstime) NLIB_REQUIRES(*mutex)
Unlocks mutex and waits until abstime for a condition variable. It then relocks mutex after execution...
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37