Move libcds 1.6.0 from SVN
[libcds.git] / cds / os / aix / timer.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_AIX_TIMER_H
4 #define __CDS_OS_AIX_TIMER_H
5
6 // Source: http://publib16.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/basetrf2/read_real_time.htm
7
8 #ifndef __CDS_OS_TIMER_H
9 #   error "<cds/os/timer.h> must be included"
10 #endif
11
12 #include <sys/time.h>
13 #include <sys/systemcfg.h>
14
15 //@cond none
16 namespace cds { namespace OS {
17     CDS_CXX11_INLINE_NAMESPACE namespace Aix {
18
19         // High resolution timer
20         class Timer {
21             timebasestruct_t    m_tmStart;
22
23         protected:
24             static unsigned long long nano( native_timer_type const& nStart, native_timer_type const& nEnd )
25             {
26                 return (((unsigned long long) (nEnd.tb_high - nStart.tb_high)) << 32) + (nEnd.tb_low - nStart.tb_low);
27             }
28
29         public:
30             typedef timebasestruct_t    native_timer_type;
31             typedef long long            native_duration_type;
32
33             Timer() : m_tmStart( current() ) {}
34
35             static native_timer_type    current()
36             {
37                 native_timer_type tm;
38                 current( &tm );
39                 return tm;
40             }
41             static void current( native_timer_type& tmr )
42             {
43                 read_real_time( &tmr, sizeof(tmr) );
44                 time_base_to_time( &tmr, sizeof(tmr ));
45             }
46
47             double reset()
48             {
49                 native_timer_type tt;
50                 current( tt );
51                 double ret = nano( m_tmStart, tt ) / 1.0E9;
52                 m_tmStart = tt;
53                 return ret;
54             }
55
56             double duration( native_duration_type dur )
57             {
58                 return double( dur ) / 1.0E9;
59             }
60
61             double duration()
62             {
63                 return duration( native_duration() );
64             }
65
66             native_duration_type    native_duration()
67             {
68                 return native_duration( m_tmStart, current() );
69             }
70
71             static native_duration_type    native_duration( native_timer_type const & nStart, native_timer_type const & nEnd )
72             {
73                 return nano( nStart, nEnd );
74             }
75
76             static unsigned long long random_seed()
77             {
78                 native_timer_type tmr;
79                 read_real_time( &tmr, sizeof(tmr) );
80                 return ( ((unsigned long long)(tmr.tb_hight)) << 32 ) + tmr.tb_low;
81             }
82         };
83     }    // namespace Aix
84
85 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
86     typedef Aix::Timer    Timer;
87 #endif
88
89 }}   // namespace cds::OS
90 //@endcond
91
92 #endif // #ifndef __CDS_OS_AIX_TIMER_H