3 #ifndef __CDS_OS_POSIX_TIMER_H
4 #define __CDS_OS_POSIX_TIMER_H
6 #ifndef __CDS_OS_TIMER_H
7 # error "<cds/os/timer.h> must be included"
13 namespace cds { namespace OS {
14 CDS_CXX11_INLINE_NAMESPACE namespace posix {
16 // High resolution timer
17 // From Linux as an example
20 typedef struct timespec native_timer_type;
21 typedef long long native_duration_type;
24 native_timer_type m_tmStart;
27 Timer() { current( m_tmStart ) ; }
29 static void current( native_timer_type& tmr )
31 // faster than gettimeofday() and posix
32 clock_gettime( CLOCK_REALTIME, &tmr );
35 static native_timer_type current()
37 native_timer_type tmr;
46 double dblRet = ( ts.tv_sec - m_tmStart.tv_sec ) + ( ts.tv_nsec - m_tmStart.tv_nsec ) / 1.0E9;
51 double duration( native_duration_type dur )
53 return double( dur ) / 1.0E9;
58 return duration( native_duration() );
61 native_duration_type native_duration()
65 return native_duration( m_tmStart, ts );
68 static native_duration_type native_duration( const native_timer_type& nStart, const native_timer_type& nEnd )
70 return native_duration_type( nEnd.tv_sec - nStart.tv_sec ) * 1000000000 + ( nEnd.tv_nsec - nStart.tv_nsec);
73 static unsigned long long random_seed()
75 native_timer_type tmr;
77 return ( ((unsigned long long)(tmr.tv_sec)) << 32 ) + tmr.tv_nsec;
82 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
86 }} // namespace cds::OS
89 #endif // #ifndef __CDS_OS_POSIX_TIMER_H