1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Alarm.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 47236 $
14  *---------------------------------------------------------------------------*/
15 
16 /* Please see man pages for details
17 
18 
19 
20 */
21 
22 #ifndef NN_OS_OS_ALARM_H_
23 #define NN_OS_OS_ALARM_H_
24 
25 #include <nn/os/os_ThreadPool.h>
26 #include <nn/os/os_Thread.h>
27 #include <nn/fnd/fnd_TimeSpan.h>
28 #include <nn/os/os_Timer.h>
29 #include <nn/util/util_NonCopyable.h>
30 #include <nn/fnd/fnd_Interlocked.h>
31 
32 typedef void (*nnosAlarmHandler)(void* param, bool cancelled);
33 
34 #ifdef __cplusplus
35 
36 namespace nn { namespace os {
37 
38 /* Please see man pages for details
39 
40 */
41 typedef void (*AlarmHandler)(void* param, bool cancelled);
42 
43 /* Please see man pages for details
44 
45 
46 
47 
48 
49 */
50 void InitializeAlarmSystem(s32 priority = 0);
51 
52 /* Please see man pages for details
53 
54 
55 
56 
57 
58 
59 */
60 void InitializeAlarmSystem(s32 workerPriority, s32 waitPriority);
61 
62 size_t GetRequiredMemorySizeForAlarmSystem(void);
63 
64 /* Please see man pages for details
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 */
93 class Alarm : private nn::os::QueueableWaitTask, private nn::util::NonCopyable<Alarm>
94 {
95 public:
96 
97     /* Please see man pages for details
98 
99 
100     */
Alarm()101     Alarm() : m_Handler(0), m_Flags(Flags::Create(false, false, false)), m_Invoker(0) {}
102 
103     /* Please see man pages for details
104 
105 
106 
107     */
108     virtual ~Alarm();
109 
110     /* Please see man pages for details
111 
112 
113     */
114     Result TryInitialize();
115 
116     /* Please see man pages for details
117 
118 
119     */
120     void Initialize();
121 
122     /* Please see man pages for details
123 
124 
125     */
126     void Finalize();
127 
128     /* Please see man pages for details
129 
130 
131 
132 
133 
134 
135 
136 
137 
138     */
139     void SetOneShot(nn::fnd::TimeSpan time, AlarmHandler handler, void* param);
140 
141     template <typename T>
142     void SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param);
143 
144     /* Please see man pages for details
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156     */
157     void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, AlarmHandler handler, void* param);
158 
159     template <typename T>
160     void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
161 
162     /* Please see man pages for details
163 
164 
165 
166 
167 
168 
169 
170 
171 
172     */
SetPeriodic(nn::fnd::TimeSpan interval,AlarmHandler handler,void * param)173     void SetPeriodic(nn::fnd::TimeSpan interval, AlarmHandler handler, void* param) { SetPeriodic(0, interval, handler, param); }
174 
175     template <typename T>
176     void SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
177 
178     /* Please see man pages for details
179 
180 
181 
182 
183     */
184     void Cancel();
185 
186     /* Please see man pages for details
187 
188 
189 
190 
191 
192 
193     */
CanSet()194     bool CanSet() const { return !m_Flags.Read().isSet; }
195 
196 private:
197 
198     struct Flags
199     {
200         bool cancelled;
201         bool isOneShot;
202         bool isSet;
203         NN_PADDING1;
204 
CreateFlags205         static Flags Create(bool cancelled, bool isOneShot, bool isSet)
206         {
207             Flags ret = { cancelled, isOneShot, isSet };
208             return ret;
209         }
210     };
211 
212     struct CancelFunc;
213     struct InvokeFunc;
214     friend struct CancelFunc;
215     friend struct InvokeFunc;
216 
217     volatile AlarmHandler m_Handler;
218     void* volatile m_Parameter;
219     nn::fnd::InterlockedVariable<Flags> m_Flags;
220     IWaitTaskInvoker* m_Invoker;
221     Timer m_Timer;
222 
223     virtual void Invoke();
224     virtual nn::os::WaitObject* GetWaitObject();
225 };
226 
227 template <typename T>
SetOneShot(nn::fnd::TimeSpan time,void (* handler)(T * param,bool cancelled),T * param)228 inline void Alarm::SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param)
229 {
230     SetOneShot(time, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
231 }
232 
233 template <typename T>
SetPeriodic(nn::fnd::TimeSpan initial,nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)234 inline void Alarm::SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
235 {
236     SetPeriodic(initial, interval, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
237 }
238 
239 template <typename T>
SetPeriodic(nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)240 inline void Alarm::SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
241 {
242     SetPeriodic(interval, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
243 }
244 
245 }} // namespace nn::os
246 
247 #endif // __cplusplus
248 
249 
250 // Below is the C declaration
251 
252 #include <nn/util/detail/util_CLibImpl.h>
253 
254 /* Please see man pages for details
255 
256 
257 */
258 
259 /* Please see man pages for details
260 
261 */
262 NN_EXTERN_C void nnosInitializeAlarmSystem(void);
263 
264 /*
265 
266 */
267 
268 
269 /* Please see man pages for details
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 */
281 
282 /* Please see man pages for details
283 
284 
285 
286 
287 */
288 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosAlarm, nn::os::Alarm, 28, u32);
289 
290 /* Please see man pages for details
291 
292 */
293 NN_EXTERN_C void nnosAlarmInitialize(nnosAlarm* this_);
294 
295 /* Please see man pages for details
296 
297 */
298 NN_EXTERN_C void nnosAlarmFinalize(nnosAlarm* this_);
299 
300 /* Please see man pages for details
301 
302 */
303 NN_EXTERN_C void nnosAlarmSetOneShot(nnosAlarm* this_, s64 time, nnosAlarmHandler handler, void* param);
304 
305 /* Please see man pages for details
306 
307 */
308 NN_EXTERN_C void nnosAlarmSetPeriodic(nnosAlarm* this_, s64 initial, s64 interval, nnosAlarmHandler handler, void* param);
309 
310 /* Please see man pages for details
311 
312 */
313 NN_EXTERN_C void nnosAlarmCancel(nnosAlarm* p);
314 
315 /* Please see man pages for details
316 
317 */
318 NN_EXTERN_C bool nnosAlarmCanSet(const nnosAlarm* p);
319 
320 /*
321 
322 
323 
324 */
325 
326 #endif
327