30#define MS_IN_NS 1000000UL
42 struct sigaction signal_action;
44 sigemptyset(&signal_action.sa_mask);
45 signal_action.sa_sigaction = handle;
46 signal_action.sa_flags = SA_SIGINFO;
47 if (sigaction(SIGALRM, &signal_action, NULL) == 0)
49 log_error(
"Unable to set signal action: %s", strerror(errno));
62 struct sigevent event = {
63 .sigev_notify = SIGEV_THREAD_ID,
64 .sigev_signo = SIGALRM,
66 ._sigev_un._tid = gettid(),
68 .sigev_notify_thread_id = gettid(),
72 timer.created = (timer_create(CLOCK_REALTIME, &event, &timer.timer_id) == 0);
73 if (not timer.created)
74 log_error(
"Unable to create a timer: %s", strerror(errno));
84 struct itimerspec duration = {
86 .it_value.tv_sec = milliseconds /
MS_PER_S,
89 timer.armed = (timer_settime(timer.timer_id, 0, &duration, NULL) == 0);
91 log_error(
"Unable to arm timer: %s", strerror(errno));
98 struct itimerspec duration = {0};
100 if (not timer->armed)
102 log_debug(
"No need to disarm a disarmed timer");
105 if (timer_settime(timer->timer_id, 0, &duration, NULL) == 0)
107 log_error(
"Unable to disarm timer: %s", strerror(errno));
113 if (not timer->created)
115 log_debug(
"No need to delete a non-created timer");
118 if (timer_delete(timer->timer_id) == 0)
120 log_error(
"Unable to delete timer: %s", strerror(errno));
Simplistic logging utilities.
#define log_error(...)
Report a condition causing the current operation to abort.
#define log_debug(...)
Report information useful to the developer.
Dynamic string implementation.
#define MS_PER_S
Number of milli-seconds in a second.
bool o2s_timer_setup_process(void(*handle)(int, siginfo_t *, void *))
Prepare the process for timeouts.
#define MS_IN_NS
Milli-seconds expressed in nano-seconds.
o2s_timer_t o2s_timer_start(o2s_timer_t timer, unsigned milliseconds)
Arm timer for the specified duration, in milliseconds.
void o2s_timer_stop(o2s_timer_t *timer)
Disarm timer.
o2s_timer_t o2s_timer_create()
Initialize a timer, that will create a SIGALRM in the current thread when timing out.
void o2s_timer_delete(o2s_timer_t *timer)
Destructor.
Use timeouts backed by signals and realtime clock.