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>
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
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:183
Implements mutex, reentrant timeout mutex, and reentrant mutex.
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:231
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:109
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
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
#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:90
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