Move libcds 1.6.0 from SVN
[libcds.git] / cds / os / sunos / timer.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_SUNOS_TIMER_H
4 #define __CDS_OS_SUNOS_TIMER_H
5
6 #ifndef __CDS_OS_TIMER_H
7 #   error "<cds/os/timer.h> must be included"
8 #endif
9
10 #include <sys/time.h>
11
12 //@cond none
13 namespace cds { namespace OS {
14     CDS_CXX11_INLINE_NAMESPACE namespace Sun {
15
16         // High resolution timer
17         class Timer {
18             hrtime_t    m_tmStart;
19         public:
20             typedef hrtime_t    native_timer_type;
21             typedef long long    native_duration_type;
22
23             Timer() : m_tmStart( current() ) {}
24
25             static native_timer_type    current()            { return gethrtime();    }
26             static void current( native_timer_type& tmr )    { tmr = gethrtime();    }
27
28             double reset()
29             {
30                 native_timer_type tt = current();
31                 double ret = (tt - m_tmStart) / 1.0E9;  //gethrtime() freq = nanoseconds
32                 m_tmStart = tt;
33                 return ret;
34             }
35
36             double duration( native_duration_type dur )
37             {
38                 return double( dur ) / 1.0E9;
39             }
40
41             double duration()
42             {
43                 return duration( native_duration() );
44             }
45
46             native_duration_type    native_duration()
47             {
48                 return native_duration( m_tmStart, current() );
49             }
50
51             static native_duration_type    native_duration( native_timer_type nStart, native_timer_type nEnd )
52             {
53                 return native_duration_type( nEnd - nStart );
54             }
55
56             static unsigned long long random_seed()
57             {
58                 return (unsigned long long) current();
59             }
60         };
61
62     }   // namespace Sun
63
64 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
65     typedef Sun::Timer    Timer;
66 #endif
67
68 }}  // namespace cds::OS
69 //@endcond
70
71 #endif // #ifndef __CDS_OS_SUNOS_TIMER_H