| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "time-shim.h" |
| 11 |
|
| 12 |
#if defined(_WIN32) && !HAVE_CLOCK_GETTIME |
| 13 |
#define _WINSOCKAPI_ |
| 14 |
# include <windows.h> |
| 15 |
# include <errno.h> |
| 16 |
# include <stdlib.h> |
| 17 |
# include <limits.h> |
| 18 |
# include <stdint.h> |
| 19 |
#include "common/attributes.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define FILETIME_1970 116444736000000000ull |
| 31 |
#define HECTONANOSEC_PER_SEC 10000000ull |
| 32 |
|
| 33 |
int gettimeofday(struct timeval *tv, void * not_implemented) |
| 34 |
{ |
| 35 |
RIST_MARK_UNUSED(not_implemented); |
| 36 |
|
| 37 |
union { |
| 38 |
unsigned long long ns100; |
| 39 |
FILETIME ft; |
| 40 |
} _now; |
| 41 |
|
| 42 |
if (tv != NULL) |
| 43 |
{ |
| 44 |
struct timespec tp; |
| 45 |
GetSystemTimeAsFileTime (&_now.ft); |
| 46 |
|
| 47 |
_now.ns100 -= FILETIME_1970; |
| 48 |
tp.tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC; |
| 49 |
tp.tv_nsec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) * 100; |
| 50 |
tv->tv_sec = tp.tv_sec; |
| 51 |
tv->tv_usec = tp.tv_nsec/1000; |
| 52 |
} |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define POW10_7 10000000 |
| 66 |
#define POW10_9 1000000000 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#define DELTA_EPOCH_IN_100NS INT64_C(116444736000000000) |
| 73 |
|
| 74 |
static inline int lc_set_errno(int result) |
| 75 |
{ |
| 76 |
if (result != 0) { |
| 77 |
errno = result; |
| 78 |
return -1; |
| 79 |
} |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
int clock_gettime(clockid_t clock_id, struct timespec *tp) |
| 84 |
{ |
| 85 |
uint64_t t; |
| 86 |
LARGE_INTEGER pf, pc; |
| 87 |
union { |
| 88 |
uint64_t u64; |
| 89 |
FILETIME ft; |
| 90 |
} ct, et, kt, ut; |
| 91 |
|
| 92 |
switch(clock_id) { |
| 93 |
case CLOCK_REALTIME: |
| 94 |
{ |
| 95 |
GetSystemTimeAsFileTime(&ct.ft); |
| 96 |
t = ct.u64 - DELTA_EPOCH_IN_100NS; |
| 97 |
tp->tv_sec = t / POW10_7; |
| 98 |
tp->tv_nsec = ((int) (t % POW10_7)) * 100; |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
case CLOCK_MONOTONIC: |
| 104 |
{ |
| 105 |
if (QueryPerformanceFrequency(&pf) == 0) |
| 106 |
return lc_set_errno(EINVAL); |
| 107 |
|
| 108 |
if (QueryPerformanceCounter(&pc) == 0) |
| 109 |
return lc_set_errno(EINVAL); |
| 110 |
|
| 111 |
tp->tv_sec = pc.QuadPart / pf.QuadPart; |
| 112 |
tp->tv_nsec = (int) (((pc.QuadPart % pf.QuadPart) * POW10_9 + (pf.QuadPart >> 1)) / pf.QuadPart); |
| 113 |
if (tp->tv_nsec >= POW10_9) { |
| 114 |
tp->tv_sec ++; |
| 115 |
tp->tv_nsec -= POW10_9; |
| 116 |
} |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
case CLOCK_PROCESS_CPUTIME_ID: |
| 122 |
{ |
| 123 |
if(0 == GetProcessTimes(GetCurrentProcess(), &ct.ft, &et.ft, &kt.ft, &ut.ft)) |
| 124 |
return lc_set_errno(EINVAL); |
| 125 |
t = kt.u64 + ut.u64; |
| 126 |
tp->tv_sec = t / POW10_7; |
| 127 |
tp->tv_nsec = ((int) (t % POW10_7)) * 100; |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
case CLOCK_THREAD_CPUTIME_ID: |
| 133 |
{ |
| 134 |
if(0 == GetThreadTimes(GetCurrentThread(), &ct.ft, &et.ft, &kt.ft, &ut.ft)) |
| 135 |
return lc_set_errno(EINVAL); |
| 136 |
t = kt.u64 + ut.u64; |
| 137 |
tp->tv_sec = t / POW10_7; |
| 138 |
tp->tv_nsec = ((int) (t % POW10_7)) * 100; |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
default: |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
return lc_set_errno(EINVAL); |
| 148 |
} |
| 149 |
|
| 150 |
#endif |
| 151 |
|
| 152 |
#ifdef __APPLE__ |
| 153 |
#include <sys/errno.h> |
| 154 |
#include <sys/types.h> |
| 155 |
#include <sys/time.h> |
| 156 |
#include <sys/sysctl.h> |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static int rist__get_system_boottime(struct timeval *tv) |
| 173 |
{ |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
return sysctl((int[]){ CTL_KERN, KERN_BOOTTIME }, 2, |
| 179 |
tv, &(size_t){ sizeof(*tv) }, NULL, 0); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static int rist__get_monotonic(struct timeval *tv) |
| 204 |
{ |
| 205 |
int ret; |
| 206 |
struct timeval currenttime; |
| 207 |
struct timeval boottime_begin; |
| 208 |
struct timeval boottime_end; |
| 209 |
|
| 210 |
do { |
| 211 |
ret = rist__get_system_boottime(&boottime_begin); |
| 212 |
if (ret != 0) |
| 213 |
return ret; |
| 214 |
|
| 215 |
ret = gettimeofday(¤ttime, NULL); |
| 216 |
if (ret != 0) |
| 217 |
return ret; |
| 218 |
|
| 219 |
ret = rist__get_system_boottime(&boottime_end); |
| 220 |
if (ret != 0) |
| 221 |
return ret; |
| 222 |
} while (timercmp(&boottime_begin, &boottime_end, !=)); |
| 223 |
|
| 224 |
timersub(¤ttime, &boottime_begin, tv); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
#if HAVE_CLOCK_GETTIME |
| 229 |
int clock_gettime_osx(clock_id_t clock_id, timespec_t *tp) |
| 230 |
#else |
| 231 |
int clock_gettime_osx(clockid_t_osx clock_id, timespec_t *tp) |
| 232 |
#endif |
| 233 |
{ |
| 234 |
int ret = 0; |
| 235 |
struct timeval tv; |
| 236 |
|
| 237 |
switch (clock_id) { |
| 238 |
case CLOCK_MONOTONIC_OSX: |
| 239 |
ret = rist__get_monotonic(&tv); |
| 240 |
break; |
| 241 |
case CLOCK_REALTIME_OSX: |
| 242 |
ret = gettimeofday(&tv, NULL); |
| 243 |
break; |
| 244 |
default: |
| 245 |
errno = EINVAL; |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
if (ret == 0) |
| 250 |
TIMEVAL_TO_TIMESPEC(&tv, tp); |
| 251 |
return ret; |
| 252 |
} |
| 253 |
#endif |