summaryrefslogtreecommitdiff
path: root/src/util/timeout.h
blob: 7e4d6cea8a48d557cd2ed7c32495682e92db5d14 (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
/*
 * 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
 */
#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
 */
#define timeout_stop()                   \
	{                                    \
		if (timeout_in) {                \
			timeout_in = false;          \
			timer_delete(timeout_timer); \
		}                                \
	}

/**
 * @brief
 * @param cfg
 * @return
 */
bool timeout_init(const config_t *cfg);

#endif  // ECGEN_TIMEOUT_H