No changes, just move time functions from wx/stopwatch.h to wx/time.h.
[wxWidgets.git] / src / common / stopwatch.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stopwatch.cpp
3 // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h
4 // Author:
5 // Original version by Julian Smart
6 // Vadim Zeitlin got rid of all ifdefs (11.12.99)
7 // Sylvain Bougnoux added wxStopWatch class
8 // Guillermo Rodriguez <guille@iies.es> rewrote from scratch (Dic/99)
9 // Modified by:
10 // Created: 20.06.2003 (extracted from common/timercmn.cpp)
11 // RCS-ID: $Id$
12 // Copyright: (c) 1998-2003 wxWidgets Team
13 // Licence: wxWindows licence
14 ///////////////////////////////////////////////////////////////////////////////
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/stopwatch.h"
32
33 #if wxUSE_STOPWATCH
34
35 #ifndef WX_PRECOMP
36 #ifdef __WXMSW__
37 #include "wx/msw/wrapwin.h"
38 #endif
39 #include "wx/log.h"
40 #endif //WX_PRECOMP
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxStopWatch
48 // ----------------------------------------------------------------------------
49
50 namespace
51 {
52
53 #ifdef __WXMSW__
54
55 struct PerfCounter
56 {
57 PerfCounter()
58 {
59 init = false;
60 }
61
62 bool CanBeUsed() const
63 {
64 return freq.QuadPart != 0;
65 }
66
67 wxCriticalSection cs;
68 LARGE_INTEGER freq;
69 bool init;
70 } gs_perfCounter;
71
72 #endif // __WXMSW__
73
74 const int MILLISECONDS_PER_SECOND = 1000;
75 const int MICROSECONDS_PER_MILLISECOND = 1000;
76 const int MICROSECONDS_PER_SECOND = 1000*1000;
77
78 } // anonymous namespace
79
80 void wxStopWatch::DoStart()
81 {
82 #ifdef __WXMSW__
83 if ( !gs_perfCounter.init )
84 {
85 wxCriticalSectionLocker lock(gs_perfCounter.cs);
86 ::QueryPerformanceFrequency(&gs_perfCounter.freq);
87
88 // Just a sanity check: it's not supposed to happen but verify that
89 // ::QueryPerformanceCounter() succeeds so that we can really use it.
90 LARGE_INTEGER counter;
91 if ( !::QueryPerformanceCounter(&counter) )
92 {
93 wxLogDebug("QueryPerformanceCounter() unexpected failed (%s), "
94 "will not use it.", wxSysErrorMsg());
95
96 gs_perfCounter.freq.QuadPart = 0;
97 }
98
99 gs_perfCounter.init = true;
100 }
101 #endif // __WXMSW__
102
103 m_t0 = GetCurrentClockValue();
104 }
105
106 wxLongLong wxStopWatch::GetClockFreq() const
107 {
108 #ifdef __WXMSW__
109 // Under MSW we use the high resolution performance counter timer which has
110 // its own frequency (usually related to the CPU clock speed).
111 if ( gs_perfCounter.CanBeUsed() )
112 return gs_perfCounter.freq.QuadPart;
113 #endif // __WXMSW__
114
115 // Currently milliseconds are used everywhere else.
116 return MILLISECONDS_PER_SECOND;
117 }
118
119 void wxStopWatch::Start(long t0)
120 {
121 DoStart();
122
123 m_t0 -= (wxLongLong(t0)*GetClockFreq())/MILLISECONDS_PER_SECOND;
124 }
125
126 wxLongLong wxStopWatch::GetCurrentClockValue() const
127 {
128 #ifdef __WXMSW__
129 if ( gs_perfCounter.CanBeUsed() )
130 {
131 LARGE_INTEGER counter;
132 ::QueryPerformanceCounter(&counter);
133 return counter.QuadPart;
134 }
135 #endif // __WXMSW__
136
137 return wxGetLocalTimeMillis();
138 }
139
140 wxLongLong wxStopWatch::TimeInMicro() const
141 {
142 const wxLongLong elapsed(m_pauseCount ? m_elapsedBeforePause
143 : GetCurrentClockValue() - m_t0);
144
145 return (elapsed*MICROSECONDS_PER_SECOND)/GetClockFreq();
146 }
147
148 #endif // wxUSE_STOPWATCH
149
150 // ----------------------------------------------------------------------------
151 // old timer functions superceded by wxStopWatch
152 // ----------------------------------------------------------------------------
153
154 #if wxUSE_LONGLONG
155
156 static wxLongLong wxStartTime = 0l;
157
158 // starts the global timer
159 void wxStartTimer()
160 {
161 wxStartTime = wxGetLocalTimeMillis();
162 }
163
164 // Returns elapsed time in milliseconds
165 long wxGetElapsedTime(bool resetTimer)
166 {
167 wxLongLong oldTime = wxStartTime;
168 wxLongLong newTime = wxGetLocalTimeMillis();
169
170 if ( resetTimer )
171 wxStartTime = newTime;
172
173 return (newTime - oldTime).GetLo();
174 }
175
176 #endif // wxUSE_LONGLONG