-
Notifications
You must be signed in to change notification settings - Fork 29
/
timing_mach.c
97 lines (82 loc) · 2.8 KB
/
timing_mach.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
90
91
92
93
94
95
96
#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <time.h>
#include "timing_mach.h"
/* inline functions - maintain ANSI C compatibility */
#ifdef TIMING_C99
/* *** */
/* C99 */
extern double timespec2secd(const struct timespec *ts_in);
extern void secd2timespec(struct timespec *ts_out, const double sec_d);
extern void timespec_monodiff_lmr(struct timespec *ts_out,
const struct timespec *ts_in);
extern void timespec_monodiff_rml(struct timespec *ts_out,
const struct timespec *ts_in);
extern void timespec_monoadd(struct timespec *ts_out,
const struct timespec *ts_in);
#endif
#ifdef __MACH__
/* ******** */
/* __MACH__ */
#include <mach/mach_time.h>
#include <mach/mach.h>
#include <mach/clock.h>
/* timing struct for osx */
static struct TimingMach {
mach_timebase_info_data_t timebase;
clock_serv_t cclock;
} timing_mach_g;
/* mach clock port */
extern mach_port_t clock_port;
int timing_mach_init (void) {
int retval = mach_timebase_info(&timing_mach_g.timebase);
if (retval != 0) return retval;
retval = host_get_clock_service(mach_host_self(),
CALENDAR_CLOCK, &timing_mach_g.cclock);
return retval;
}
int clock_gettime(clockid_t id, struct timespec *tspec) {
mach_timespec_t mts;
int retval = 0;
if (id == CLOCK_REALTIME) {
retval = clock_get_time(timing_mach_g.cclock, &mts);
if (retval != 0) return retval;
tspec->tv_sec = mts.tv_sec;
tspec->tv_nsec = mts.tv_nsec;
} else if (id == CLOCK_MONOTONIC) {
retval = clock_get_time(clock_port, &mts);
if (retval != 0) return retval;
tspec->tv_sec = mts.tv_sec;
tspec->tv_nsec = mts.tv_nsec;
} else {
/* only CLOCK_MONOTOIC and CLOCK_REALTIME clocks supported */
return -1;
}
return 0;
}
int clock_nanosleep_abstime(const struct timespec *req) {
struct timespec ts_delta;
int retval = clock_gettime(CLOCK_MONOTONIC, &ts_delta);
if (retval != 0) return retval;
timespec_monodiff_rml (&ts_delta, req);
/* mach does not properly return remainder from nanosleep */
retval = nanosleep(&ts_delta, NULL);
return retval;
}
/* __MACH__ */
/* ******** */
#endif
int itimer_start (struct timespec *ts_target, const struct timespec *ts_step) {
int retval = clock_gettime(CLOCK_MONOTONIC, ts_target);
if (retval != 0) return retval;
/* add step size to current monotonic time */
timespec_monoadd(ts_target, ts_step);
return retval;
}
int itimer_step (struct timespec *ts_target, const struct timespec *ts_step) {
int retval = clock_nanosleep_abstime(ts_target);
if (retval != 0) return retval;
/* move target along */
timespec_monoadd(ts_target, ts_step);
return retval;
}