-
Notifications
You must be signed in to change notification settings - Fork 3
/
timeout.c
45 lines (42 loc) · 852 Bytes
/
timeout.c
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
#include "timeout.h"
#include <time.h>
#include <sys/time.h>
/**
* Returns current time in ms. (since 1970)
*/
double
timeout_gettime(void)
{
struct timeval v;
gettimeofday(&v, NULL);
return v.tv_sec + v.tv_usec / 1.0e6;
}
/**
* Init timeout structure.
*/
void
timeout_init(struct timeout *tm, double timeout)
{
tm->tm_timeout = timeout;
if (tm->tm_timeout <= 0) {
tm->tm_deadline = -1;
} else {
tm->tm_deadline = timeout_gettime() + tm->tm_timeout;
}
}
/**
* Determine how much time we have left.
*
* Returns the number of ms left or -1 if there is no time limit.
*/
double
timeout_left(struct timeout *tm)
{
if (tm->tm_timeout <= 0) {
return -1;
} else {
double left = tm->tm_deadline - timeout_gettime();
if (left < 0.0) left = 0.0;
return left;
}
}