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(NLIB_CXX11_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(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
98 #else
99  nlib_cond cond_;
100 #endif
101 };
102 
103 #ifndef NLIB_DOXYGEN
104 template <>
105 inline NLIB_CHECK_RESULT errno_t CondVar::WaitFor(SimpleCriticalSection& lock, // NOLINT
106  const nlib_ns::TimeSpan& timeout) NLIB_NOEXCEPT
107  NLIB_REQUIRES(lock) {
108  return nlib_cond_wait_for(&cond_, detail::GetRawMutex(lock), timeout.ToTimeValue().tick);
109 }
110 
111 template <>
112 inline NLIB_CHECK_RESULT errno_t CondVar::WaitUntil(SimpleCriticalSection& lock, // NOLINT
113  const nlib_ns::DateTime& datetime) NLIB_NOEXCEPT
114  NLIB_REQUIRES(lock) {
115  return nlib_cond_wait_until(&cond_, detail::GetRawMutex(lock), datetime.ToTimeValue().tick);
116 }
117 #endif
118 
119 #endif
120 
121 } // namespace threading
122 NLIB_NAMESPACE_END
123 
124 #endif // INCLUDE_NN_NLIB_THREADING_CONDVAR_H_
int64_t tick
These can be used for nlib_time and nlib_duration.
Definition: DateTime.h:69
TimeValue ToTimeValue() const noexcept
Converts to a TimeValue object.
Definition: DateTime.h:125
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
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:261
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...
NLIB_CHECK_RESULT 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.
NLIB_CHECK_RESULT 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:99
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:310
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
#define NLIB_COND_INITIALIZER
Constant for statically initializing nlib_cond.
The class for representing the time.
Definition: DateTime.h:106
NLIB_CHECK_RESULT 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
NLIB_CHECK_RESULT 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