bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 1 | #ifndef UTIL_LINUX_TIMER_H |
| 2 | #define UTIL_LINUX_TIMER_H |
| 3 | |
| 4 | #include <signal.h> |
| 5 | #include <sys/time.h> |
| 6 | |
| 7 | static 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 | |
| 23 | static 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 |