aboutsummaryrefslogtreecommitdiff
path: root/src/util/timeout.h
blob: 29b837db1ad22b03282501e4d36d89eb4a6cb08a (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
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017 J08nY
 */
/**
 * @file timeout.h
 */
#ifndef ECGEN_TIMEOUT_H
#define ECGEN_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;

/**
 * @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) {                                     \
		struct sigevent sevp;                                 \
		sevp.sigev_notify = SIGEV_THREAD_ID;                  \
		sevp.sigev_signo = SIGALRM;                           \
		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); \
		}                                \
	}

/**
 * @brief Initialize the timeout system.
 * @return whether the initalization was successful
 */
bool timeout_init();

#endif  // ECGEN_TIMEOUT_H