blob: 79ef64919f62b0118ab19f19b4466f123a8873d9 [file] [log] [blame]
bigbiff7b4c7a62015-01-01 19:44:14 -05001#ifndef UTIL_LINUX_TIMER_H
2#define UTIL_LINUX_TIMER_H
3
4#include <signal.h>
5#include <sys/time.h>
6
7static inline int setup_timer(
8 struct itimerval *timer,
9 struct itimerval *old_timer,
10 struct sigaction *old_sa,
11 void (*timeout_handler)(int))
12{
13 struct sigaction sa;
14
15 memset(&sa, 0, sizeof sa);
16 sa.sa_handler = timeout_handler;
17 sa.sa_flags = SA_RESETHAND;
18 sigaction(SIGALRM, &sa, old_sa);
19
20 return setitimer(ITIMER_REAL, timer, old_timer);
21}
22
23static inline void cancel_timer(
24 struct itimerval *old_timer,
25 struct sigaction *old_sa)
26{
27 setitimer(ITIMER_REAL, old_timer, NULL);
28 sigaction(SIGALRM, old_sa, NULL);
29}
30
31#endif