Repository navigation
Expand file tree
/
Copy pathtimer.h
More file actions
67 lines (61 loc) · 2.31 KB
/
Copy pathtimer.h
File metadata and controls
67 lines (61 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
/* ************************************************************************** */
/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
/* */
/**
* @copyright 2023, SAFRAN T4DS, ALL RIGHTS RESERVED
* @file timer.h
* @author Antoine GAGNIERE
* @brief Use timeouts backed by signals and realtime clock
*/
/* ************************************************************************** */
#include <signal.h> // sigaction
#include <stdbool.h> // bool
#include <time.h> // timer_t
typedef struct o2s_timer
{
timer_t timer_id;
bool created;
bool armed;
} o2s_timer_t;
bool o2s_timer_setup_process(void (*handle)(int, siginfo_t*, void*));
o2s_timer_t o2s_timer_create(void);
o2s_timer_t o2s_timer_start(o2s_timer_t timer, unsigned milliseconds);
void o2s_timer_stop(o2s_timer_t* timer);
void o2s_timer_delete(o2s_timer_t* timer);
/**
* Automatically delete the timer when going out of scope.
*
* In a situation where one wants to declare a timer in a local scope,
* this "typedef" can be used for that timer to be deleted
* automatically when the variable goes out of scope.
*
* It means this "typedef" can only be used like this:
* @code{.c}
* {
* O2sTimer my_timer = o2s_timer_create(...);
* ...
* } // <- the timer will be deleted at that point
* @endcode
*/
#define O2sTimer __attribute__((cleanup(o2s_timer_delete))) o2s_timer_t
/**
* Automatically disarm the timer when going out of scope.
*
* In a situation where one wants to declare a timer in a local scope,
* this "typedef" can be used for that timer to be disarmed
* automatically when the variable goes out of scope.
*
* It means this "typedef" can only be used like this:
* @code{.c}
* {
* ArmedTimer copy = o2s_timer_start(my_timer, ...);
* ...
* } // <- the timer will be disarmed at that point
* @endcode
*/
#define ArmedTimer __attribute__((cleanup(o2s_timer_stop))) o2s_timer_t