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
| | /*
* Copyright (C) 2012-2015 all contributors <cmogstored-public@bogomips.org>
* License: GPLv3 or later (see COPYING for details)
*/
#include "cmogstored.h"
/*
* we block signals in pool threads, only the main thread receives signals
*/
static sigset_t fullset;
sigset_t mog_emptyset;
__attribute__((constructor)) static void sig_init(void)
{
CHECK(int, 0, sigfillset(&fullset));
CHECK(int, 0, sigemptyset(&mog_emptyset));
}
void mog_intr_disable(void)
{
CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &fullset, NULL));
}
void mog_intr_enable(void)
{
CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &mog_emptyset, NULL));
}
/*
* favor ppoll if available since this is our only pselect user and
* would increase the size of the executable
*/
#ifdef HAVE_PPOLL
static void sleeper(struct timespec *tsp, const sigset_t *sigmask)
{
if (ppoll(NULL, 0, tsp, sigmask) < 0)
assert((errno == EINTR || errno == ENOMEM) &&
"BUG in ppoll usage");
}
#else /* PSELECT */
static void sleeper(struct timespec *tsp, const sigset_t *sigmask)
{
if (pselect(0, NULL, NULL, NULL, tsp, sigmask) < 0)
assert((errno == EINTR || errno == ENOMEM) &&
"BUG in pselect usage");
}
#endif /* PSELECT */
/* thread-safe, interruptible sleep, negative seconds -> sleep forever */
void mog_sleep(long seconds)
{
struct timespec ts;
struct timespec *tsp;
if (seconds < 0) {
tsp = NULL;
} else {
ts.tv_sec = seconds;
ts.tv_nsec = 0;
tsp = &ts;
}
sleeper(tsp, &mog_emptyset);
}
|