Created
August 19, 2018 22:09
-
-
Save rindeal/04972b1ad6cb043b17c762a761431aed to your computer and use it in GitHub Desktop.
Lockup Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "lockup_timer.h" | |
#define PRINT_ERR(fmt, ...) \ | |
fprintf(stderr, "%s:%d:%s(): " fmt, \ | |
__FILE__, __LINE__, __func__, \ | |
__VA_ARGS__) | |
#include <stdio.h> /* fprintf */ | |
#include <errno.h> /* errno */ | |
#include <string.h> /* strerror */ | |
static void | |
lockup_timer_notifee(union sigval sval) | |
{ | |
jmp_buf * buf_p = (jmp_buf *) sval.sival_ptr; | |
if (buf_p == NULL) | |
{ | |
PRINT_ERR("sigval is empty"); | |
return; | |
} | |
longjmp(*buf_p, 1); | |
} | |
int | |
lockup_timer_create(timer_t * const timer_p, jmp_buf * const jmp_buf_p) | |
{ | |
struct sigevent sev = { | |
// .sigev_notify = SIGEV_SIGNAL, | |
// .sigev_signo = SIGALRM, | |
.sigev_notify = SIGEV_THREAD, | |
.sigev_notify_function = lockup_timer_notifee, | |
.sigev_notify_attributes = NULL, | |
.sigev_value = { .sival_ptr = jmp_buf_p }, | |
}; | |
int is_err = timer_create(CLOCK_REALTIME, &sev, timer_p); | |
if ( is_err ) | |
{ | |
PRINT_ERR("%s", strerror(errno)); | |
} | |
return is_err ? -errno : 0; | |
} | |
int | |
lockup_timer_delete(timer_t timer) | |
{ | |
int is_err = timer_delete(timer); | |
if ( is_err ) | |
{ | |
PRINT_ERR("%s", strerror(errno)); | |
} | |
return is_err ? -errno : 0; | |
} | |
int | |
lockup_timer_set(timer_t timer, int const sec) | |
{ | |
struct itimerspec const timeout = { | |
.it_value = { | |
.tv_sec = (time_t) sec, | |
.tv_nsec = 0, | |
}, | |
.it_interval = { 0 }, | |
}; | |
int is_err = timer_settime(timer, 0, &timeout, NULL); | |
if ( is_err ) | |
{ | |
PRINT_ERR("%s", strerror(errno)); | |
} | |
return is_err ? -errno : 0; | |
} | |
int | |
lockup_timer_disarm(timer_t timer) | |
{ | |
return lockup_timer_set(timer, 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LOCKUP_TIMER_H_ | |
#define LOCKUP_TIMER_H_ 1 | |
// for timers support | |
#if defined(_POSIX_C_SOURCE) | |
# if (_POSIX_C_SOURCE < 199309L) | |
# error "_POSIX_C_SOURCE set too low, at least 199309L required" | |
# endif | |
#else | |
# define _POSIX_C_SOURCE 199309L | |
#endif | |
#include <signal.h> /* struct sigevent, union sigval */ | |
#include <time.h> /* timer_create, timer_t */ | |
#include <setjmp.h> /* setjmp, longjmp */ | |
int lockup_timer_create(timer_t * const timer_p, jmp_buf * const jmp_buf_p); | |
int lockup_timer_delete(timer_t timer); | |
int lockup_timer_set(timer_t timer, int const sec); | |
int lockup_timer_disarm(timer_t timer); | |
#endif // LOCKUP_TIMER_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment