summaryrefslogtreecommitdiffstats
path: root/include/timer.h
blob: b820453d4067df4a7fb970b8d26f27722661267c (plain) (blame)
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
#ifndef UTIL_LINUX_TIMER_H
#define UTIL_LINUX_TIMER_H

#include <signal.h>
#include <sys/time.h>

static inline void setup_timer(
			struct itimerval *timer,
			struct itimerval *old_timer,
			struct sigaction *old_sa,
			void (*timeout_handler)(int))
{
	struct sigaction sa;

	memset(&sa, 0, sizeof sa);
	sa.sa_handler = timeout_handler;
	sa.sa_flags = SA_RESETHAND;
	sigaction(SIGALRM, &sa, old_sa);

	setitimer(ITIMER_REAL, timer, old_timer);
}

static inline void cancel_timer(
			struct itimerval *old_timer,
			struct sigaction *old_sa)
{
	setitimer(ITIMER_REAL, old_timer, NULL);
	sigaction(SIGALRM, old_sa, NULL);
}

#endif