1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_Alarm.h
4
5 Copyright (C)2009 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: 34337 $
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 void InitializeAlarmSystem(s32 priority = 0);
49
50 /* Please see man pages for details
51
52
53
54 */
55 void InitializeAlarmSystem(s32 workerPriority, s32 waitPriority);
56
57 size_t GetRequiredMemorySizeForAlarmSystem(void);
58
59 /* Please see man pages for details
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 */
87 class Alarm : private nn::os::QueueableWaitTask, private nn::util::NonCopyable<Alarm>
88 {
89 public:
90
91 /* Please see man pages for details
92
93
94 */
Alarm()95 Alarm() : m_Handler(0), m_Flags(Flags::Create(false, false, false)), m_Invoker(0) {}
96
97 /* Please see man pages for details
98
99
100
101 */
102 virtual ~Alarm();
103
104 /* Please see man pages for details
105
106 */
107 void Initialize();
108
109 /* Please see man pages for details
110
111
112 */
113 void Finalize();
114
115 /* Please see man pages for details
116
117
118
119
120
121
122
123
124
125 */
126 void SetOneShot(nn::fnd::TimeSpan time, AlarmHandler handler, void* param);
127
128 template <typename T>
129 void SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param);
130
131 /* Please see man pages for details
132
133
134
135
136
137
138
139
140
141
142
143 */
144 void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, AlarmHandler handler, void* param);
145
146 template <typename T>
147 void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
148
149 /* Please see man pages for details
150
151
152
153
154
155
156
157
158
159 */
SetPeriodic(nn::fnd::TimeSpan interval,AlarmHandler handler,void * param)160 void SetPeriodic(nn::fnd::TimeSpan interval, AlarmHandler handler, void* param) { SetPeriodic(0, interval, handler, param); }
161
162 template <typename T>
163 void SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
164
165 /* Please see man pages for details
166
167
168
169
170 */
171 void Cancel();
172
173 /* Please see man pages for details
174
175
176
177
178
179
180 */
CanSet()181 bool CanSet() const { return !m_Flags.Read().isSet; }
182
183 private:
184
185 struct Flags
186 {
187 bool cancelled;
188 bool isOneShot;
189 bool isSet;
190 NN_PADDING1;
191
CreateFlags192 static Flags Create(bool cancelled, bool isOneShot, bool isSet)
193 {
194 Flags ret = { cancelled, isOneShot, isSet };
195 return ret;
196 }
197 };
198
199 struct CancelFunc;
200 struct InvokeFunc;
201 friend struct CancelFunc;
202 friend struct InvokeFunc;
203
204 volatile AlarmHandler m_Handler;
205 void* volatile m_Parameter;
206 nn::fnd::InterlockedVariable<Flags> m_Flags;
207 IWaitTaskInvoker* m_Invoker;
208 Timer m_Timer;
209
210 virtual void Invoke();
211 virtual nn::os::WaitObject* GetWaitObject();
212 };
213
214 template <typename T>
SetOneShot(nn::fnd::TimeSpan time,void (* handler)(T * param,bool cancelled),T * param)215 inline void Alarm::SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param)
216 {
217 SetOneShot(time, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
218 }
219
220 template <typename T>
SetPeriodic(nn::fnd::TimeSpan initial,nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)221 inline void Alarm::SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
222 {
223 SetPeriodic(initial, interval, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
224 }
225
226 template <typename T>
SetPeriodic(nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)227 inline void Alarm::SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
228 {
229 SetPeriodic(interval, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
230 }
231
232 }} // namespace nn::os
233
234 #endif // __cplusplus
235
236
237 // C declarations follow
238
239 #include <nn/util/detail/util_CLibImpl.h>
240
241 /* Please see man pages for details
242
243
244 */
245
246 /* Please see man pages for details
247
248 */
249 NN_EXTERN_C void nnosInitializeAlarmSystem(void);
250
251 /*
252
253 */
254
255
256 /* Please see man pages for details
257
258
259
260
261
262
263
264
265
266
267 */
268
269 /* Please see man pages for details
270
271
272
273
274 */
275 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosAlarm, nn::os::Alarm, 28, u32);
276
277 /* Please see man pages for details
278
279 */
280 NN_EXTERN_C void nnosAlarmInitialize(nnosAlarm* this_);
281
282 /* Please see man pages for details
283
284 */
285 NN_EXTERN_C void nnosAlarmFinalize(nnosAlarm* this_);
286
287 /* Please see man pages for details
288
289 */
290 NN_EXTERN_C void nnosAlarmSetOneShot(nnosAlarm* this_, s64 time, nnosAlarmHandler handler, void* param);
291
292 /* Please see man pages for details
293
294 */
295 NN_EXTERN_C void nnosAlarmSetPeriodic(nnosAlarm* this_, s64 initial, s64 interval, nnosAlarmHandler handler, void* param);
296
297 /* Please see man pages for details
298
299 */
300 NN_EXTERN_C void nnosAlarmCancel(nnosAlarm* p);
301
302 /* Please see man pages for details
303
304 */
305 NN_EXTERN_C bool nnosAlarmCanSet(const nnosAlarm* p);
306
307 /*
308
309
310
311 */
312
313 #endif
314