more fixes to 1049
[k8sterm.git] / src / getticks.c
blob147f1dfbc8b401f1ed3d017d288616f53f2ce480
1 ////////////////////////////////////////////////////////////////////////////////
2 // getticks
3 static struct timespec mclk_sttime; // starting time of monotonic clock
5 #ifdef __MACH__
7 #include <sys/time.h>
9 #define CLOCK_REALTIME 0
10 #define CLOCK_MONOTONIC 1
11 #define CLOCK_PROCESS_CPUTIME_ID 2
12 #define CLOCK_THREAD_CPUTIME_ID 3
13 #define CLOCK_MONOTONIC_RAW 4
14 #define CLOCK_REALTIME_COARSE 5
15 #define CLOCK_MONOTONIC_COARSE 6
17 //clock_gettime is not implemented on OSX
18 // This implementation is found on the internet. Not sure if it's 100% correct
20 int clock_gettime(int clk_id, struct timespec* t) {
21 struct timeval now;
22 int rv = gettimeofday(&now, NULL);
23 if (rv) return rv;
24 t->tv_sec = now.tv_sec;
25 t->tv_nsec = now.tv_usec * 1000;
26 return 0;
29 #endif
31 static void mclock_init (void) {
32 clock_gettime(CLOCK_MONOTONIC /*CLOCK_MONOTONIC_RAW*/, &mclk_sttime);
36 static K8TTimeMSec mclock_ticks (void) {
37 struct timespec tp;
39 clock_gettime(CLOCK_MONOTONIC /*CLOCK_MONOTONIC_RAW*/, &tp);
40 tp.tv_sec -= mclk_sttime.tv_sec;
41 return (K8TTimeMSec)tp.tv_sec*1000+(tp.tv_nsec/1000000);