d397ba11aafa37a1b153c79e27337cf0a10e1d7b
[libcds.git] / cds / os / osx / timer.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_OS_OSX_TIMER_H
4 #define CDSLIB_OS_OSX_TIMER_H
5
6 #ifndef CDSLIB_OS_TIMER_H
7 #   error "<cds/os/timer.h> must be included"
8 #endif
9
10 #include <time.h>
11 #include <sys/time.h>
12 #include <mach/clock.h>
13 #include <mach/mach.h>
14
15 // From http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
16
17 //@cond none
18 namespace cds { namespace OS {
19     CDS_CXX11_INLINE_NAMESPACE namespace OS_X {
20
21         // High resolution timer
22         class Timer {
23         public:
24             typedef mach_timespec_t native_timer_type;
25             typedef long long       native_duration_type;
26
27         private:
28             native_timer_type    m_tmStart;
29             clock_serv_t         m_Service;
30
31         public:
32
33             Timer()
34             {
35                 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &m_Service);
36                 current( m_tmStart );
37             }
38             ~Timer()
39             {
40                 mach_port_deallocate(mach_task_self(), m_Service);
41             }
42
43             void current( native_timer_type& tmr )
44             {
45                 clock_get_time(m_Service, &tmr);
46             }
47
48             native_timer_type    current()
49             {
50                 native_timer_type    tmr;
51                 current(tmr);
52                 return tmr;
53             }
54
55             double reset()
56             {
57                 native_timer_type ts;
58                 current( ts );
59                 double dblRet = ( ts.tv_sec - m_tmStart.tv_sec ) + ( ts.tv_nsec - m_tmStart.tv_nsec ) / 1.0E9;
60                 m_tmStart = ts;
61                 return dblRet;
62             }
63
64             double duration( native_duration_type dur )
65             {
66                 return double( dur ) / 1.0E9;
67             }
68
69             double duration()
70             {
71                 return duration( native_duration() );
72             }
73
74             native_duration_type    native_duration()
75             {
76                 native_timer_type ts;
77                 current( ts );
78                 return native_duration( m_tmStart, ts );
79             }
80
81             static native_duration_type    native_duration( const native_timer_type& nStart, const native_timer_type& nEnd )
82             {
83                 return native_duration_type( nEnd.tv_sec - nStart.tv_sec ) * 1000000000 + ( nEnd.tv_nsec - nStart.tv_nsec);
84             }
85
86             static unsigned long long random_seed()
87             {
88                 native_timer_type tmr;
89                 Timer tm;
90                 tm.current( tmr );
91                 return ( ((unsigned long long)(tmr.tv_sec)) << 32 ) + tmr.tv_nsec;
92             }
93         };
94     }    // namespace OS_X
95
96 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
97     typedef OS_X::Timer    Timer;
98 #endif
99
100 }}    // namespace cds::OS
101 //@endcond
102
103 #endif // #ifndef CDSLIB_OS_LINUX_TIMER_H