aboutsummaryrefslogtreecommitdiff
path: root/src/util/timeout.c
blob: 21f9591d762d439aa586a5946a89a11e35f72361 (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
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017-2018 J08nY
 */
#include "timeout.h"
#include "util/memory.h"

__thread jmp_buf timeout_ptr;
__thread bool timeout_in = false;
__thread timer_t *timeout_timer;
__thread struct sigevent *sevp;

void timeout_handle(int signum, siginfo_t *siginfo, void *other) {
	sigset_t mask;
	sigemptyset(&mask);
	sigaddset(&mask, SIGALRM);
	sigprocmask(SIG_UNBLOCK, &mask, NULL);

	if (timeout_in) siglongjmp(timeout_ptr, 1);
}

void timeout_thread_init() {
	timeout_timer = try_calloc(sizeof(timer_t));
	sevp = try_calloc(sizeof(struct sigevent));
}

void timeout_thread_quit() {
	try_free(timeout_timer);
	try_free(sevp);
}

bool timeout_init() {
	// init for the main thread.
	timeout_thread_init();
	struct sigaction new_action;

	new_action.sa_sigaction = timeout_handle;
	sigemptyset(&new_action.sa_mask);
	new_action.sa_flags = SA_SIGINFO;

	sigaction(SIGALRM, &new_action, NULL);
	return true;
}

void timeout_quit() {
	// deinit the main thread.
	timeout_thread_quit();
}