libnx  v4.2.2
utimer.h
Go to the documentation of this file.
1 /**
2  * @file utimer.h
3  * @brief User-mode timer synchronization primitive.
4  * @author plutoo
5  * @copyright libnx Authors
6  */
7 #pragma once
8 #include "wait.h"
9 
10 typedef struct UTimer UTimer;
11 
12 /// Valid types for a user-mode timer.
13 typedef enum {
14  TimerType_OneShot, ///< Timers of this kind fire once and then stop automatically.
15  TimerType_Repeating, ///< Timers of this kind fire periodically.
16 } TimerType;
17 
18 /// User-mode timer object.
19 struct UTimer {
20  Waitable waitable;
21  TimerType type : 8;
22  bool started : 1;
23  u64 next_tick;
24  u64 interval;
25 };
26 
27 /// Creates a waiter for a user-mode timer.
28 static inline Waiter waiterForUTimer(UTimer* t)
29 {
30  Waiter wait_obj;
31  wait_obj.type = WaiterType_Waitable;
32  wait_obj.waitable = &t->waitable;
33  return wait_obj;
34 }
35 
36 /**
37  * @brief Creates a user-mode timer.
38  * @param[out] t UTimer object.
39  * @param[in] interval Interval (in nanoseconds).
40  * @param[in] type Type of timer to create (see \ref TimerType).
41  * @note The timer is stopped when it is created. Use \ref utimerStart to start it.
42  * @note It is safe to wait on this timer with several threads simultaneously.
43  * @note If more than one thread is listening on it, at least one thread will get the signal. No other guarantees.
44  * @note For a repeating timer: If the timer triggers twice before you wait on it, you will only get one signal.
45  */
46 void utimerCreate(UTimer* t, u64 interval, TimerType type);
47 
48 /**
49  * @brief Starts the timer.
50  * @param[in] t UTimer object.
51  */
52 void utimerStart(UTimer* t);
53 
54 /**
55  * @brief Stops the timer.
56  * @param[in] t UTimer object.
57  */
58 void utimerStop(UTimer* t);
TimerType
TimerType
Valid types for a user-mode timer.
Definition: utimer.h:13
utimerStop
void utimerStop(UTimer *t)
Stops the timer.
TimerType_Repeating
@ TimerType_Repeating
Timers of this kind fire periodically.
Definition: utimer.h:15
utimerStart
void utimerStart(UTimer *t)
Starts the timer.
UTimer
User-mode timer object.
Definition: utimer.h:19
wait.h
User mode synchronization primitive waiting operations.
Waitable
Definition: wait.h:21
utimerCreate
void utimerCreate(UTimer *t, u64 interval, TimerType type)
Creates a user-mode timer.
TimerType_OneShot
@ TimerType_OneShot
Timers of this kind fire once and then stop automatically.
Definition: utimer.h:14
Waiter
Waiter structure, representing any generic waitable synchronization object; both kernel-mode and user...
Definition: wait.h:36
u64
uint64_t u64
64-bit unsigned integer.
Definition: types.h:22
waiterForUTimer
static Waiter waiterForUTimer(UTimer *t)
Creates a waiter for a user-mode timer.
Definition: utimer.h:28