Replace cds::OS::yield() with std::this_thread::yield()
[libcds.git] / cds / os / win / timer.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_WIN_TIMER_H
4 #define __CDS_OS_WIN_TIMER_H
5
6 #ifndef __CDS_OS_TIMER_H
7 #   error "<cds/os/timer.h> must be included"
8 #endif
9
10 #include <windows.h>
11
12 //@cond none
13 namespace cds { namespace OS {
14     CDS_CXX11_INLINE_NAMESPACE namespace Win32 {
15
16         /// High resolution timer
17         /**
18             Implementation of high resolution timer for Windows platforms.
19             The implementation build on QueryPerformanceCounter API.
20         */
21         class Timer {
22         public:
23             typedef LARGE_INTEGER    native_timer_type        ;    ///< Native timer type
24             typedef long long        native_duration_type    ;    ///< Native duration type
25
26         private:
27             native_timer_type    m_nFrequency;
28             native_timer_type    m_nStart;
29
30         public:
31             Timer()
32             {
33                 ::QueryPerformanceFrequency( &m_nFrequency );
34                 current( m_nStart );
35             }
36
37             /// Places into \p tmr the current time in native Windows format
38             static void current( native_timer_type& tmr )
39             {
40                 ::QueryPerformanceCounter( &tmr );
41             }
42
43             /// Returns current time in native Windows format
44             static native_timer_type current()
45             {
46                 native_timer_type    tmr;
47                 current(tmr);
48                 return tmr;
49             }
50
51             /// Sets internal start time to current time. Returns duration from prevoius start time to current.
52             double reset()
53             {
54                 native_timer_type nCur;
55                 current( nCur );
56                 double dblRet = double(nCur.QuadPart - m_nStart.QuadPart) / m_nFrequency.QuadPart;
57                 m_nStart.QuadPart = nCur.QuadPart;
58                 return dblRet;
59             }
60
61             /// Translates \p dur from native format to seconds
62             double duration( native_duration_type dur )
63             {
64                 return double( dur ) / m_nFrequency.QuadPart;
65             }
66
67             /// Returns duration (in seconds) from start time to current
68             double duration()
69             {
70                 return duration( native_duration() );
71             }
72
73             /// Returns duration (in native format) from start time to current
74             native_duration_type    native_duration()
75             {
76                 native_timer_type ts;
77                 current( ts );
78                 return native_duration( m_nStart, ts );
79             }
80
81             /// Calculates duration (in native format) between \p nEnd and \p nStart
82             static native_duration_type    native_duration( const native_timer_type& nStart, const native_timer_type& nEnd )
83             {
84                 return nEnd.QuadPart - nStart.QuadPart;
85             }
86
87             static unsigned long long random_seed()
88             {
89                 return current().QuadPart;
90             }
91         };
92
93
94     }   // namespace Win32
95
96 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
97     typedef Win32::Timer    Timer;
98 #endif
99
100 }}  // namespae cds::OS
101 //@endcond
102
103 #endif // #ifndef __CDS_OS_WIN_TIMER_H