3 #ifndef CDSLIB_OS_LINUX_TIMER_H
4 #define CDSLIB_OS_LINUX_TIMER_H
6 #ifndef CDSLIB_OS_TIMER_H
7 # error "<cds/os/timer.h> must be included"
11 //#include <sys/time.h>
14 namespace cds { namespace OS {
15 CDS_CXX11_INLINE_NAMESPACE namespace Linux {
17 // High resolution timer
20 typedef struct timespec native_timer_type;
21 typedef long long native_duration_type;
24 native_timer_type m_tmStart;
28 Timer() { current( m_tmStart ) ; }
30 static void current( native_timer_type& tmr )
32 // faster than gettimeofday() and posix
33 clock_gettime( CLOCK_REALTIME, &tmr );
36 static native_timer_type current()
38 native_timer_type tmr;
47 double dblRet = ( ts.tv_sec - m_tmStart.tv_sec ) + ( ts.tv_nsec - m_tmStart.tv_nsec ) / 1.0E9;
52 double duration( native_duration_type dur )
54 return double( dur ) / 1.0E9;
59 return duration( native_duration() );
62 native_duration_type native_duration()
66 return native_duration( m_tmStart, ts );
69 static native_duration_type native_duration( const native_timer_type& nStart, const native_timer_type& nEnd )
71 return native_duration_type( nEnd.tv_sec - nStart.tv_sec ) * 1000000000 + ( nEnd.tv_nsec - nStart.tv_nsec);
74 static unsigned long long random_seed()
76 native_timer_type tmr;
78 return ( ((unsigned long long)(tmr.tv_sec)) << 32 ) + tmr.tv_nsec;
83 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
84 typedef Linux::Timer Timer;
87 }} // namespace cds::OS
90 #endif // #ifndef CDSLIB_OS_LINUX_TIMER_H