/
timer.c
89 lines (66 loc) · 1.94 KB
/
timer.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#define _POSIX_C_SOURCE 199309L /* clock_gettime */
#include <string.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include "debug.h"
#include "timer.h"
/* - - - - - - - - - - - - - - - - - - - - */
#define MS_PER_S 1E3
#define US_PER_S 1E9
#define MS_PER_US 1E6
/* - - - - - - - - - - - - - - - - - - - - */
static struct timespec init_time;
static pthread_mutex_t init_time_mtx = PTHREAD_MUTEX_INITIALIZER;
/* - - - - - - - - - - - - - - - - - - - - */
static unsigned long get_ms_diff(struct timespec *ts);
static void timespec_diff(const struct timespec *const start, const struct timespec *const end, struct timespec *r);
/* - - - - - - - - - - - - - - - - - - - - */
int
timer_init(void)
{
int status;
pthread_mutex_lock(&init_time_mtx);
status = clock_gettime(CLOCK_MONOTONIC, &init_time);
pthread_mutex_unlock(&init_time_mtx);
if (status == -1) {
print_info("clock_gettime: %s", strerror(errno));
}
return status == 0;
}
int
timer_get_elapsed_ms(unsigned long *ms)
{
struct timespec ts;
int status;
status = clock_gettime(CLOCK_MONOTONIC, &ts);
if (status == -1) {
print_info("clock_gettime: %s", strerror(errno));
} else if (status == 0) {
*ms = get_ms_diff(&ts);
}
return status == 0;
}
/* - - - - - - - - - - - - - - - - - - - - */
static unsigned long
get_ms_diff(struct timespec *ts)
{
unsigned long ms;
struct timespec diff;
pthread_mutex_lock(&init_time_mtx);
timespec_diff(&init_time, ts, &diff);
pthread_mutex_unlock(&init_time_mtx);
ms = diff.tv_sec*MS_PER_S + diff.tv_nsec/MS_PER_US;
return ms;
}
static void
timespec_diff(const struct timespec *const start, const struct timespec *const end, struct timespec *r)
{
if ((end->tv_nsec - start->tv_nsec) < 0) {
r->tv_sec = end->tv_sec-start->tv_sec - 1;
r->tv_nsec = US_PER_S + end->tv_nsec-start->tv_nsec;
} else {
r->tv_sec = end->tv_sec-start->tv_sec;
r->tv_nsec = end->tv_nsec-start->tv_nsec;
}
}