aboutsummaryrefslogtreecommitdiffstats
path: root/nstime.c
blob: bad73d57bb28e9aa55ea672a08e50d21338ada80 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Nanosecond-resolution clock, or as close as available.
 * Return a 64-bit value with an arbitrary epoch; the
 * function nstime_init() should initialize the baseline
 * so that wraparound is as unlikely as possible.
 */

#include "compiler.h"
#include "nstime.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <SDL.h>

#ifdef __WIN32__

#include <synchapi.h>
#include <mmsystem.h>
#ifdef HAVE_TIMEAPI_H
# include <timeapi.h>
#endif

static uint64_t tzero;
static double tscfactor;

static inline uint64_t tsc(void)
{
    LARGE_INTEGER tsc;
    QueryPerformanceCounter(&tsc);
    return tsc.QuadPart;
}

uint64_t nstime(void)
{
    return llround((tsc() - tzero) * tscfactor);
}

static UINT wTimerRes;

static void nstime_end(void)
{
    timeEndPeriod(wTimerRes);
}

#define TARGET_TIMER_RESOLUTION 1	/* 1 ms */
void nstime_init(void)
{
    TIMECAPS tc;
    UINT wTimerRes;
    LARGE_INTEGER tscfreq;

    QueryPerformanceFrequency(&tscfreq);
    /* 10^9/frequency in Hz = ns per tick */
    tscfactor = 1.0e+9/((uint64_t)tscfreq.QuadPart);

    if (timeGetDevCaps(&tc, sizeof tc) != TIMERR_NOERROR)
	return;			/* Can't improve timer resolution */

    wTimerRes = min(max(tc.wPeriodMin, TARGET_TIMER_RESOLUTION), tc.wPeriodMax);
    atexit(nstime_end);
    timeBeginPeriod(wTimerRes);

    tzero = tsc();
}

#define mynssleep mynssleep
void mynssleep(uint64_t until, uint64_t since)
{
    DWORD ms;

    if (until <= since)
	return;

    ms = (until - since + UINT64_C(999999)) / UINT64_C(1000000);
    Sleep(ms);
}

#elif defined(_POSIX_TIMERS)

#ifdef _POSIX_MONOTONIC_CLOCK
#define WHICHCLOCK CLOCK_MONOTONIC
#else
#define WHICHCLOCK CLOCK_REALTIME
#endif

static time_t tv_sec_zero;

uint64_t nstime(void)
{
    struct timespec ts;
    clock_gettime(WHICHCLOCK, &ts);
    return ((ts.tv_sec - tv_sec_zero) * UINT64_C(1000000000)) + ts.tv_nsec;
}

void nstime_init(void)
{
    struct timespec ts;
    clock_gettime(WHICHCLOCK, &ts);
    tv_sec_zero = ts.tv_sec;
}

#elif defined(HAVE_GETTIMEOFDAY)

static time_t tv_sec_zero;

uint64_t nstime(void)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);
    return ((tv.tv_sec - tv_sec_zero) * UINT64_C(1000000000))
        + (tv.tv_usec * UINT64_C(1000));
}

void nstime_init(void)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);
    tv_sec_zero = tv.tv_sec;
}

#else
#error "Need to implement a different fine-grained timer function here"
#endif

#ifdef mynssleep

/* Already defined */

#elif defined(WHICHCLOCK) && defined(HAVE_CLOCK_NANOSLEEP)

void mynssleep(uint64_t until, uint64_t since)
{
    (void)since;
    struct timespec req;

    req.tv_sec = until / UINT64_C(1000000000) + tv_sec_zero;
    req.tv_nsec = until % UINT64_C(1000000000);

    clock_nanosleep(WHICHCLOCK, TIMER_ABSTIME, &req, NULL);
}

#elif defined(HAVE_NANOSLEEP)

void mynssleep(uint64_t until, uint64_t since)
{
    struct timespec req;

    until -= since;

    req.tv_sec = until / UINT64_C(1000000000);
    req.tv_nsec = until % UINT64_C(1000000000);

    nanosleep(&req, NULL);
}

#else

void mynssleep(uint64_t until, uint64_t since)
{
    until -= since;

    SDL_Delay((until + UINT64_C(999999)) / UINT64_C(1000000));
}

#endif