blob: 5a5c0d27d56ece17edc36c6471c36bb93da9134f (
plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/*
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017-2018 J08nY
*/
/**
* @file timeout.h
*/
#ifndef ECGEN_UTIL_TIMEOUT_H
#define ECGEN_UTIL_TIMEOUT_H
#include <setjmp.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#include "io/output.h"
#include "misc/config.h"
extern __thread sigjmp_buf timeout_ptr;
extern __thread bool timeout_in;
extern __thread timer_t *timeout_timer;
extern __thread struct sigevent *sevp;
/**
* @brief Start a timer that times out after <code>seconds-</code>.
*
* The timer is thread-local. This function is an if-like statement,
* it runs the <code>else</code> part for the duration of the timeout or
* until <code>timeout_stop</code> is called. If that branch times out, then the
* <code>if</code> part is run. The times should be always stopped by
* <code>timeout_stop</code> when the timed interval ends.
* @param seconds how long to run the <code>else</code> branch for
*/
#define timeout_start(seconds) \
if ((seconds) != 0) { \
sevp->sigev_notify = SIGEV_THREAD_ID; \
sevp->sigev_signo = SIGALRM; \
sevp->sigev_value.sival_int = 0; \
sevp->_sigev_un._tid = (__pid_t)syscall(SYS_gettid); \
\
timer_create(CLOCK_MONOTONIC, sevp, timeout_timer); \
struct itimerspec timer_time = { \
.it_interval = {.tv_sec = 0, .tv_nsec = 0}, \
.it_value = {.tv_sec = (seconds), .tv_nsec = 0}}; \
timer_settime(*timeout_timer, 0, &timer_time, NULL); \
timeout_in = true; \
}; \
if ((seconds) != 0 && sigsetjmp(timeout_ptr, 1) == 1)
/**
* @brief Stop a timer.
*/
#define timeout_stop() \
{ \
if (timeout_in) { \
timeout_in = false; \
timer_delete(*timeout_timer); \
} \
}
void timeout_thread_init();
void timeout_thread_quit();
/**
* @brief Initialize the timeout system.
* @return whether the initalization was successful
*/
bool timeout_init();
/**
* @brief Deinitialize the timeout system.
*/
void timeout_quit();
#endif // ECGEN_UTIL_TIMEOUT_H
|