KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
oneshot_timer.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/oneshot_timer.h
4 Copyright (C) 2024 Paul Cercueil
5*/
6
7/** \file kos/oneshot_timer.h
8 \brief One-shot timer support.
9 \ingroup kthreads
10
11 This file contains the one-shot timer API. A one-shot timer is a timer that
12 will trigger an action (through a pre-registered callback) after a timeout
13 expires.
14
15 \author Paul Cercueil
16
17 \see kos/thread.h
18 \see kos/worker_thread.h
19*/
20
21#ifndef __KOS_ONESHOT_TIMER_H
22#define __KOS_ONESHOT_TIMER_H
23
24#include <sys/cdefs.h>
25__BEGIN_DECLS
26
27struct oneshot_timer;
28
29/** \struct oneshot_timer_t
30 \brief Opaque structure describing one one-shot timer.
31*/
32typedef struct oneshot_timer oneshot_timer_t;
33
34/** \brief Create a new one-shot timer.
35 \relatesalso oneshot_timer_t
36
37 This function will create a one-shot timer using the specified callback,
38 programmed to expire after the given timeout. The timer will be stopped
39 by default and still need to be started using oneshot_timer_start().
40
41 \param cb The function to call in case the one-shot timer
42 expires.
43 \param data A parameter to pass to the function called.
44 \param timeout_ms The timeout value for the one-shot timer, in
45 milliseconds.
46
47 \return The new one-shot timer on success, NULL on failure.
48
49 \sa oneshot_timer_destroy, oneshot_timer_start
50*/
51oneshot_timer_t *oneshot_timer_create(void (*cb)(void *), void *data,
52 unsigned int timeout_ms);
53
54/** \brief Stop and destroy a one-shot timer.
55 \relatesalso oneshot_timer_t
56
57 This function will stop the one-shot timer and free its memory.
58
59 \param timer A pointer to the one-shot timer.
60
61 \sa oneshot_timer_create
62*/
64
65/** \brief Re-configure a one-shot timer.
66 \relatesalso oneshot_timer_t
67
68 This function can be used to change the registered callback or the timeout.
69 Using it on a running timer is unsupported.
70
71 \param timer A pointer to the one-shot timer.
72 \param cb The function to call in case the one-shot timer
73 expires.
74 \param data A parameter to pass to the function called.
75 \param timeout_ms The timeout value for the one-shot timer, in
76 milliseconds.
77
78 \sa oneshot_timer_stop, oneshot_timer_start
79*/
80void oneshot_timer_setup(oneshot_timer_t *timer, void (*cb)(void *),
81 void *data, unsigned int timeout_ms);
82
83/** \brief Start a one-shot timer.
84 \relatesalso oneshot_timer_t
85
86 This function will start the one-shot timer. If not stopped until the
87 timeout value expires, the one-shot timer will call the registered
88 callback function.
89
90 \param timer A pointer to the one-shot timer.
91
92 \sa oneshot_timer_stop, oneshot_timer_reset
93*/
95
96/** \brief Stop a one-shot timer.
97 \relatesalso oneshot_timer_t
98
99 This function will stop the one-shot timer. If it already expired, this
100 function does nothing. If it did not expire yet, the one-shot timer is
101 stopped and the registered callback won't be called.
102
103 \param timer A pointer to the one-shot timer.
104
105 \sa oneshot_timer_start, oneshot_timer_reset
106*/
108
109/** \brief Reset a one-shot timer.
110 \relatesalso oneshot_timer_t
111
112 A convenience function to reset the one-shot timer by stopping it then
113 starting it again.
114
115 \param timer A pointer to the one-shot timer.
116
117 \sa oneshot_timer_start, oneshot_timer_stop
118*/
119static inline void oneshot_timer_reset(oneshot_timer_t *timer) {
120 oneshot_timer_stop(timer);
121 oneshot_timer_start(timer);
122}
123
124__END_DECLS
125
126#endif /* __KOS_ONESHOT_TIMER_H */
void oneshot_timer_stop(oneshot_timer_t *timer)
Stop a one-shot timer.
void oneshot_timer_start(oneshot_timer_t *timer)
Start a one-shot timer.
Opaque structure describing one one-shot timer.
void oneshot_timer_stop(oneshot_timer_t *timer)
Stop a one-shot timer.
void oneshot_timer_destroy(oneshot_timer_t *timer)
Stop and destroy a one-shot timer.
void oneshot_timer_start(oneshot_timer_t *timer)
Start a one-shot timer.
void oneshot_timer_setup(oneshot_timer_t *timer, void(*cb)(void *), void *data, unsigned int timeout_ms)
Re-configure a one-shot timer.
oneshot_timer_t * oneshot_timer_create(void(*cb)(void *), void *data, unsigned int timeout_ms)
Create a new one-shot timer.
static void oneshot_timer_reset(oneshot_timer_t *timer)
Reset a one-shot timer.
Definition oneshot_timer.h:119