3 #ifndef __CDS_OS_OSX_TIMER_H
4 #define __CDS_OS_OSX_TIMER_H
6 #ifndef __CDS_OS_TIMER_H
7 # error "<cds/os/timer.h> must be included"
12 #include <mach/clock.h>
13 #include <mach/mach.h>
15 // From http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
18 namespace cds { namespace OS {
19 CDS_CXX11_INLINE_NAMESPACE namespace OS_X {
21 // High resolution timer
24 typedef mach_timespec_t native_timer_type;
25 typedef long long native_duration_type;
28 native_timer_type m_tmStart;
29 clock_serv_t m_Service;
35 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &m_Service);
40 mach_port_deallocate(mach_task_self(), m_Service);
43 void current( native_timer_type& tmr )
45 clock_get_time(m_Service, &tmr);
48 native_timer_type current()
50 native_timer_type tmr;
59 double dblRet = ( ts.tv_sec - m_tmStart.tv_sec ) + ( ts.tv_nsec - m_tmStart.tv_nsec ) / 1.0E9;
64 double duration( native_duration_type dur )
66 return double( dur ) / 1.0E9;
71 return duration( native_duration() );
74 native_duration_type native_duration()
78 return native_duration( m_tmStart, ts );
81 static native_duration_type native_duration( const native_timer_type& nStart, const native_timer_type& nEnd )
83 return native_duration_type( nEnd.tv_sec - nStart.tv_sec ) * 1000000000 + ( nEnd.tv_nsec - nStart.tv_nsec);
86 static unsigned long long random_seed()
88 native_timer_type tmr;
91 return ( ((unsigned long long)(tmr.tv_sec)) << 32 ) + tmr.tv_nsec;
96 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
97 typedef OS_X::Timer Timer;
100 }} // namespace cds::OS
103 #endif // #ifndef __CDS_OS_LINUX_TIMER_H