-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplatform.h
84 lines (60 loc) · 2.25 KB
/
platform.h
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
// Copyright © 2015, The Network Protocol Company, Inc. All Rights Reserved.
#ifndef PLATFORM_H
#define PLATFORM_H
#include <assert.h>
#if __APPLE__
// ===========================================================================================================================================
// MacOSX platform
// ===========================================================================================================================================
#include <unistd.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
inline void platform_sleep( double time )
{
usleep( (int) ( time * 1000000 ) );
}
inline double platform_time()
{
static uint64_t start = 0;
static mach_timebase_info_data_t timebase_info;
if ( start == 0 )
{
mach_timebase_info( &timebase_info );
start = mach_absolute_time();
return 0.0;
}
uint64_t current = mach_absolute_time();
assert( current > start );
return ( double( current - start ) * double( timebase_info.numer ) / double( timebase_info.denom ) ) / 1000000000.0;
}
// ===========================================================================================================================================
#elif __linux
// ===========================================================================================================================================
// Linux platform
// ===========================================================================================================================================
#include <unistd.h>
#include <time.h>
inline void platform_sleep( double time )
{
usleep( (int) ( time * 1000000 ) );
}
inline double platform_time()
{
static double start = -1;
if ( start == -1 )
{
timespec ts;
clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
start = ts.tv_sec + double(ts.tv_nsec) / 1000000000.0;
return 0.0;
}
timespec ts;
clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
double current = ts.tv_sec + double(ts.tv_nsec) / 1000000000.0;
return current - start;
}
// ===========================================================================================================================================
#else
#error unsupported platform!
#endif
#endif // #ifndef SHARED_H