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: 38846 $
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 class Alarm : private nn::os::QueueableWaitTask, private nn::util::NonCopyable<Alarm>
92 {
93 public:
94 
95     /* Please see man pages for details
96 
97 
98     */
Alarm()99     Alarm() : m_Handler(0), m_Flags(Flags::Create(false, false, false)), m_Invoker(0) {}
100 
101     /* Please see man pages for details
102 
103 
104 
105     */
106     virtual ~Alarm();
107 
108     /* Please see man pages for details
109 
110 
111     */
112     Result TryInitialize();
113 
114     /* Please see man pages for details
115 
116 
117     */
118     void Initialize();
119 
120     /* Please see man pages for details
121 
122 
123     */
124     void Finalize();
125 
126     /* Please see man pages for details
127 
128 
129 
130 
131 
132 
133 
134 
135 
136     */
137     void SetOneShot(nn::fnd::TimeSpan time, AlarmHandler handler, void* param);
138 
139     template <typename T>
140     void SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param);
141 
142     /* Please see man pages for details
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154     */
155     void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, AlarmHandler handler, void* param);
156 
157     template <typename T>
158     void SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
159 
160     /* Please see man pages for details
161 
162 
163 
164 
165 
166 
167 
168 
169 
170     */
SetPeriodic(nn::fnd::TimeSpan interval,AlarmHandler handler,void * param)171     void SetPeriodic(nn::fnd::TimeSpan interval, AlarmHandler handler, void* param) { SetPeriodic(0, interval, handler, param); }
172 
173     template <typename T>
174     void SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T* param, bool cancelled), T* param);
175 
176     /* Please see man pages for details
177 
178 
179 
180 
181     */
182     void Cancel();
183 
184     /* Please see man pages for details
185 
186 
187 
188 
189 
190 
191     */
CanSet()192     bool CanSet() const { return !m_Flags.Read().isSet; }
193 
194 private:
195 
196     struct Flags
197     {
198         bool cancelled;
199         bool isOneShot;
200         bool isSet;
201         NN_PADDING1;
202 
CreateFlags203         static Flags Create(bool cancelled, bool isOneShot, bool isSet)
204         {
205             Flags ret = { cancelled, isOneShot, isSet };
206             return ret;
207         }
208     };
209 
210     struct CancelFunc;
211     struct InvokeFunc;
212     friend struct CancelFunc;
213     friend struct InvokeFunc;
214 
215     volatile AlarmHandler m_Handler;
216     void* volatile m_Parameter;
217     nn::fnd::InterlockedVariable<Flags> m_Flags;
218     IWaitTaskInvoker* m_Invoker;
219     Timer m_Timer;
220 
221     virtual void Invoke();
222     virtual nn::os::WaitObject* GetWaitObject();
223 };
224 
225 template <typename T>
SetOneShot(nn::fnd::TimeSpan time,void (* handler)(T * param,bool cancelled),T * param)226 inline void Alarm::SetOneShot(nn::fnd::TimeSpan time, void (*handler)(T* param, bool cancelled), T* param)
227 {
228     SetOneShot(time, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
229 }
230 
231 template <typename T>
SetPeriodic(nn::fnd::TimeSpan initial,nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)232 inline void Alarm::SetPeriodic(nn::fnd::TimeSpan initial, nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
233 {
234     SetPeriodic(initial, interval, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
235 }
236 
237 template <typename T>
SetPeriodic(nn::fnd::TimeSpan interval,void (* handler)(T *,bool),T * param)238 inline void Alarm::SetPeriodic(nn::fnd::TimeSpan interval, void (*handler)(T*, bool), T* param)
239 {
240     SetPeriodic(interval, reinterpret_cast<AlarmHandler&>(handler), reinterpret_cast<void*&>(param));
241 }
242 
243 }} // namespace nn::os
244 
245 #endif // __cplusplus
246 
247 
248 // Below is the C declaration
249 
250 #include <nn/util/detail/util_CLibImpl.h>
251 
252 /* Please see man pages for details
253 
254 
255 */
256 
257 /* Please see man pages for details
258 
259 */
260 NN_EXTERN_C void nnosInitializeAlarmSystem(void);
261 
262 /*
263 
264 */
265 
266 
267 /* Please see man pages for details
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 */
279 
280 /* Please see man pages for details
281 
282 
283 
284 
285 */
286 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosAlarm, nn::os::Alarm, 28, u32);
287 
288 /* Please see man pages for details
289 
290 */
291 NN_EXTERN_C void nnosAlarmInitialize(nnosAlarm* this_);
292 
293 /* Please see man pages for details
294 
295 */
296 NN_EXTERN_C void nnosAlarmFinalize(nnosAlarm* this_);
297 
298 /* Please see man pages for details
299 
300 */
301 NN_EXTERN_C void nnosAlarmSetOneShot(nnosAlarm* this_, s64 time, nnosAlarmHandler handler, void* param);
302 
303 /* Please see man pages for details
304 
305 */
306 NN_EXTERN_C void nnosAlarmSetPeriodic(nnosAlarm* this_, s64 initial, s64 interval, nnosAlarmHandler handler, void* param);
307 
308 /* Please see man pages for details
309 
310 */
311 NN_EXTERN_C void nnosAlarmCancel(nnosAlarm* p);
312 
313 /* Please see man pages for details
314 
315 */
316 NN_EXTERN_C bool nnosAlarmCanSet(const nnosAlarm* p);
317 
318 /*
319 
320 
321 
322 */
323 
324 #endif
325