summaryrefslogtreecommitdiffstats
path: root/src/lib/sleep.c
blob: 7b3b6fa911f8c3e814b37b7d7ff9fc646fa2471c (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
/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "sleep.h"

#include <time.h>

static bool ATTR_NOWARN_UNUSED_RESULT
sleep_timespec(const struct timespec *ts_sleep, bool interruptible)
{
	struct timespec ts_remain = *ts_sleep;

	while (nanosleep(&ts_remain, &ts_remain) < 0) {
		if (errno != EINTR)
			i_fatal("nanosleep(): %m");
		if (interruptible)
			return FALSE;
	}
	return TRUE;
}

void i_sleep_usecs(unsigned long long usecs)
{
	struct timespec ts_sleep;

	ts_sleep.tv_sec = (time_t)(usecs / 1000000);
	ts_sleep.tv_nsec = (long)(usecs % 1000000) * 1000;
	sleep_timespec(&ts_sleep, FALSE);
}

void i_sleep_msecs(unsigned int msecs)
{
	struct timespec ts_sleep;

	ts_sleep.tv_sec = (time_t)(msecs / 1000);
	ts_sleep.tv_nsec = (long)(msecs % 1000) * 1000000;
	sleep_timespec(&ts_sleep, FALSE);
}

void i_sleep_secs(time_t secs)
{
	struct timespec ts_sleep;

	ts_sleep.tv_sec = secs;
	ts_sleep.tv_nsec = 0;
	sleep_timespec(&ts_sleep, FALSE);
}

bool i_sleep_intr_usecs(unsigned long long usecs)
{
	struct timespec ts_sleep;

	ts_sleep.tv_sec = (time_t)(usecs / 1000000);
	ts_sleep.tv_nsec = (long)(usecs % 1000000) * 1000;
	return sleep_timespec(&ts_sleep, TRUE);
}

bool i_sleep_intr_msecs(unsigned int msecs)
{
	struct timespec ts_sleep;

	ts_sleep.tv_sec = (time_t)(msecs / 1000);
	ts_sleep.tv_nsec = (long)(msecs % 1000) * 1000000;
	return sleep_timespec(&ts_sleep, TRUE);
}

bool i_sleep_intr_secs(time_t secs)
{
	struct timespec ts_sleep;

	ts_sleep.tv_sec = secs;
	ts_sleep.tv_nsec = 0;
	return sleep_timespec(&ts_sleep, TRUE);
}