3 #ifndef __CDS_OS_FREE_BSD_TIMER_H
4 #define __CDS_OS_FREE_BSD_TIMER_H
6 #ifndef __CDS_OS_TIMER_H
7 # error "<cds/os/timer.h> must be included"
11 #include <sys/timespec.h>
14 namespace cds { namespace OS {
16 CDS_CXX11_INLINE_NAMESPACE namespace Free_BSD {
18 // High resolution timer
21 typedef struct timespec native_timer_type;
22 typedef long long native_duration_type;
25 native_timer_type m_tmStart;
29 Timer() { current( m_tmStart ) ; }
31 static void current( native_timer_type& tmr )
33 // faster than gettimeofday() and posix
34 # ifdef CLOCK_REALTIME_FAST
35 ::clock_gettime( CLOCK_REALTIME_FAST, &tmr );
37 ::clock_gettime( CLOCK_REALTIME_FAST, &tmr );
41 static native_timer_type current()
43 native_timer_type tmr;
52 double dblRet = ( ts.tv_sec - m_tmStart.tv_sec ) + ( ts.tv_nsec - m_tmStart.tv_nsec ) / 1.0E9;
57 double duration( native_duration_type dur )
59 return double( dur ) / 1.0E9;
64 return duration( native_duration() );
67 native_duration_type native_duration()
71 return native_duration( m_tmStart, ts );
74 static native_duration_type native_duration( const native_timer_type& nStart, const native_timer_type& nEnd )
76 return native_duration_type( nEnd.tv_sec - nStart.tv_sec ) * 1000000000 + ( nEnd.tv_nsec - nStart.tv_nsec);
79 static unsigned long long random_seed()
81 native_timer_type tmr;
83 return ( ((unsigned long long)(tmr.tv_sec)) << 32 ) + tmr.tv_nsec;
87 } // namespace Free_BSD
89 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
90 typedef Free_BSD::Timer Timer;
93 }} // namespace cds::OS
96 #endif // #ifndef __CDS_OS_FREE_BSD_TIMER_H