1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_LightAlarm.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:$
14 *---------------------------------------------------------------------------*/
15
16 /* Please see man pages for details
17
18
19
20
21 */
22
23 #ifndef NN_OS_OS_LIGHTALARM_H_
24 #define NN_OS_OS_LIGHTALARM_H_
25
26 #include <nn/os.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 #include <nn/fnd/fnd_LinkedList.h>
32
33 typedef void (*nnosLightAlarmHandler)(void* param, bool cancelled);
34
35 #ifdef __cplusplus
36
37 namespace nn { namespace os {
38
39 /* Please see man pages for details
40
41
42
43 */
44 typedef void (*LightAlarmHandler)(void* param, bool canceled);
45 void InitializeLightAlarmSystemImpl(uptr stackBottom, s32 priority = 0);
46
47 /* Please see man pages for details
48
49
50
51
52
53 */
54 template <typename Stack>
55 inline void InitializeLightAlarmSystem(Stack& stack, s32 priority = 0)
56 {
57 InitializeLightAlarmSystemImpl(stack.GetStackBottom(), priority);
58 }
59
60 /* Please see man pages for details
61
62 */
63 void FinalizeLightAlarmSystem();
64
65 /* Please see man pages for details
66
67
68
69 */
70
71 namespace detail {
72 // Class for saving data for LightAlarm registered to wait queue
73 class LightAlarmNode : public nn::fnd::IntrusiveLinkedList<LightAlarmNode>::Item
74 {
75 public:
LightAlarmNode()76 LightAlarmNode() : event(false), waiting(false) {}
77 volatile LightAlarmHandler handler; //Alarm handler
78 LightEvent event; //Expression notification event
79 void* volatile parameter; //Alarm handler parameters
80 bool canceled; //Cancel flag
81 bool waiting; //Flag for alarm waiting to be set
82 NN_PADDING2;
83 NN_PADDING4;
84 Tick fire; //Expression tick count
85 Tick period; //Expression interval tick count
86 };
87 //Class for saving data for LightAlarms related to system
88 class LightAlarmSystem
89 {
90 public:
91 Timer timer;
92 Thread thread;
93 nn::fnd::IntrusiveLinkedList<LightAlarmNode> list;
94 bool initialized;
95 NN_PADDING3;
96 void SetTimer( Tick nowTick );
97 void InsertAlarm( LightAlarmNode *pInsertNode, Tick nowTick );
98 };
99 } //namespace detail
100
101 /* Please see man pages for details
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 */
127 class LightAlarm : private nn::util::NonCopyable<LightAlarm>
128 {
129 public:
130
131 /* Please see man pages for details
132
133
134 */
LightAlarm()135 LightAlarm(){};
136
137 /* Please see man pages for details
138
139
140 */
141 virtual ~LightAlarm();
142
143 /* Please see man pages for details
144
145 */
146 void Initialize();
147
148 /* Please see man pages for details
149
150
151
152 */
153 void Finalize();
154
155 /* Please see man pages for details
156
157
158
159
160
161
162
163
164
165 */
166 void SetOneShot(nn::fnd::TimeSpan time, LightAlarmHandler handler, void* param);
167
168 template <typename T>
169 void SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param);
170
171 /* Please see man pages for details
172
173
174
175
176
177
178
179
180
181
182
183 */
184 void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, LightAlarmHandler handler, void* param);
185
186 template <typename T>
187 void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
188
189 /* Please see man pages for details
190
191
192
193
194
195
196
197
198
199 */
SetPeriodic(nn::fnd::TimeSpan interval,LightAlarmHandler handler,void * param)200 void SetPeriodic(nn::fnd::TimeSpan interval, LightAlarmHandler handler, void* param) { SetPeriodic(0, interval, handler, param); }
201
202 template <typename T>
203 void SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
204
205 /* Please see man pages for details
206
207
208
209
210
211
212
213
214 */
215 void Cancel();
216
217 /* Please see man pages for details
218
219
220
221
222
223
224
225 */
226 bool CanSet();
227
228 static nn::os::detail::LightAlarmSystem* s_System;
229
230 private:
231 NN_PADDING4;
232 nn::os::detail::LightAlarmNode m_LightAlarm;
233 };
234
235 template <typename T>
SetOneShot(nn::fnd::TimeSpan time,void (* handler)(T * param,bool cancelled),T * param)236 inline void LightAlarm::SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param)
237 {
238 SetOneShot(time, reinterpret_cast<LightAlarmHandler&>(handler), reinterpret_cast<void*&>(param));
239 }
240
241 template <typename T>
SetPeriodic(nn::fnd::TimeSpan initial,nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)242 inline void LightAlarm::SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
243 {
244 SetPeriodic(initial, interval, reinterpret_cast<LightAlarmHandler&>(handler), reinterpret_cast<void*&>(param));
245 }
246
247 template <typename T>
SetPeriodic(nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)248 inline void LightAlarm::SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
249 {
250 SetPeriodic(interval, reinterpret_cast<LightAlarmHandler&>(handler), reinterpret_cast<void*&>(param));
251 }
252
253 namespace detail {
254 //Debug Functions
255 void InitializeLightAlarmSystemImplCore( uptr stackBottom, s32 priority, nn::os::detail::LightAlarmSystem* lightAlarmSystem );
256 void FinalizeLightAlarmSystemImpl( nn::os::detail::LightAlarmSystem* lightAlarmSystem );
257 } //namespace detail
258
259 }} // namespace nn::os
260
261 #endif // __cplusplus
262
263
264 // Below is the C declaration
265
266 #include <nn/util/detail/util_CLibImpl.h>
267
268 /* Please see man pages for details
269
270
271 */
272
273 /* Please see man pages for details
274
275 */
276 NN_EXTERN_C void nnosInitializeLightAlarmSystem( uptr stackBottom, s32 priority );
277
278 /* Please see man pages for details
279
280 */
281
282
283 /* Please see man pages for details
284
285
286
287
288
289
290
291
292
293
294
295 */
296
297 /* Please see man pages for details
298
299
300
301
302 */
303 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosLightAlarm, nn::os::LightAlarm, 56, u64);
304
305 /* Please see man pages for details
306
307 */
308 NN_EXTERN_C void nnosLightAlarmInitialize(nnosLightAlarm* this_);
309
310 /* Please see man pages for details
311
312 */
313 NN_EXTERN_C void nnosLightAlarmFinalize(nnosLightAlarm* this_);
314
315 /* Please see man pages for details
316
317 */
318 NN_EXTERN_C void nnosLightAlarmSetOneShot(nnosLightAlarm* this_, s64 time, nnosLightAlarmHandler handler, void* param);
319
320 /* Please see man pages for details
321
322 */
323 NN_EXTERN_C void nnosLightAlarmSetPeriodic(nnosLightAlarm* this_, s64 initial, s64 interval, nnosLightAlarmHandler handler, void* param);
324
325 /* Please see man pages for details
326
327 */
328 NN_EXTERN_C void nnosLightAlarmCancel(nnosLightAlarm* p);
329
330 /* Please see man pages for details
331
332 */
333 NN_EXTERN_C bool nnosLightAlarmCanSet(
334 nnosLightAlarm* p);
335
336 /* Please see man pages for details
337
338
339
340 */
341
342 #endif // NN_OS_OS_LIGHTALARM_H_
343