]> git.saurik.com Git - wxWidgets.git/blame - src/common/stopwatch.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / common / stopwatch.cpp
CommitLineData
e2478fde 1///////////////////////////////////////////////////////////////////////////////
57bd4c60 2// Name: src/common/stopwatch.cpp
e2478fde
VZ
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)
77ffb593 11// Copyright: (c) 1998-2003 wxWidgets Team
526954c5 12// Licence: wxWindows licence
e2478fde
VZ
13///////////////////////////////////////////////////////////////////////////////
14
15// ============================================================================
16// declarations
17// ============================================================================
18
19// ----------------------------------------------------------------------------
20// headers
21// ----------------------------------------------------------------------------
22
23// for compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
bb90a3e6
WS
30#include "wx/stopwatch.h"
31
173a5ddc
VZ
32#if wxUSE_STOPWATCH
33
e2478fde 34#ifndef WX_PRECOMP
d98a58c5 35 #ifdef __WINDOWS__
57bd4c60
WS
36 #include "wx/msw/wrapwin.h"
37 #endif
8df6de97 38 #include "wx/log.h"
0c717dd1 39 #include "wx/thread.h"
e2478fde
VZ
40#endif //WX_PRECOMP
41
e2478fde
VZ
42// ============================================================================
43// implementation
44// ============================================================================
45
46// ----------------------------------------------------------------------------
47// wxStopWatch
48// ----------------------------------------------------------------------------
49
54647bb7
VZ
50namespace
51{
52
d98a58c5 53#ifdef __WINDOWS__
b0ec0023 54
54647bb7
VZ
55struct PerfCounter
56{
57 PerfCounter()
58 {
59 init = false;
60 }
61
62 bool CanBeUsed() const
63 {
64 return freq.QuadPart != 0;
65 }
66
0c717dd1 67 wxCRIT_SECT_DECLARE_MEMBER(cs);
54647bb7
VZ
68 LARGE_INTEGER freq;
69 bool init;
70} gs_perfCounter;
71
d98a58c5 72#endif // __WINDOWS__
54647bb7 73
b0ec0023 74const int MILLISECONDS_PER_SECOND = 1000;
a43503cb 75const int MICROSECONDS_PER_MILLISECOND = 1000;
b0ec0023
VZ
76const int MICROSECONDS_PER_SECOND = 1000*1000;
77
78} // anonymous namespace
79
80void wxStopWatch::DoStart()
e2478fde 81{
d98a58c5 82#ifdef __WINDOWS__
54647bb7 83 if ( !gs_perfCounter.init )
2555c77a 84 {
0c717dd1 85 wxCRIT_SECT_LOCKER(lock, gs_perfCounter.cs);
54647bb7 86 ::QueryPerformanceFrequency(&gs_perfCounter.freq);
b0ec0023
VZ
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
54647bb7 99 gs_perfCounter.init = true;
2555c77a 100 }
d98a58c5 101#endif // __WINDOWS__
54647bb7 102
b0ec0023
VZ
103 m_t0 = GetCurrentClockValue();
104}
105
106wxLongLong wxStopWatch::GetClockFreq() const
107{
d98a58c5 108#ifdef __WINDOWS__
b0ec0023
VZ
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;
d98a58c5 113#endif // __WINDOWS__
54647bb7 114
796e54ef
VZ
115#ifdef HAVE_GETTIMEOFDAY
116 // With gettimeofday() we can have nominally microsecond precision and
117 // while this is not the case in practice, it's still better than
118 // millisecond.
119 return MICROSECONDS_PER_SECOND;
120#else // !HAVE_GETTIMEOFDAY
b0ec0023
VZ
121 // Currently milliseconds are used everywhere else.
122 return MILLISECONDS_PER_SECOND;
796e54ef 123#endif // HAVE_GETTIMEOFDAY/!HAVE_GETTIMEOFDAY
e2478fde
VZ
124}
125
b0ec0023
VZ
126void wxStopWatch::Start(long t0)
127{
17d72a48
VZ
128 // Calling Start() makes the stop watch run however many times it was
129 // paused before.
130 m_pauseCount = 0;
131
b0ec0023
VZ
132 DoStart();
133
134 m_t0 -= (wxLongLong(t0)*GetClockFreq())/MILLISECONDS_PER_SECOND;
135}
136
137wxLongLong wxStopWatch::GetCurrentClockValue() const
e2478fde 138{
d98a58c5 139#ifdef __WINDOWS__
b0ec0023 140 if ( gs_perfCounter.CanBeUsed() )
2555c77a 141 {
b0ec0023
VZ
142 LARGE_INTEGER counter;
143 ::QueryPerformanceCounter(&counter);
144 return counter.QuadPart;
2555c77a 145 }
d98a58c5 146#endif // __WINDOWS__
b0ec0023 147
796e54ef
VZ
148#ifdef HAVE_GETTIMEOFDAY
149 return wxGetUTCTimeUSec();
150#else // !HAVE_GETTIMEOFDAY
e1645882 151 return wxGetUTCTimeMillis();
796e54ef 152#endif // HAVE_GETTIMEOFDAY/!HAVE_GETTIMEOFDAY
e2478fde
VZ
153}
154
b0ec0023 155wxLongLong wxStopWatch::TimeInMicro() const
e2478fde 156{
b0ec0023
VZ
157 const wxLongLong elapsed(m_pauseCount ? m_elapsedBeforePause
158 : GetCurrentClockValue() - m_t0);
159
160 return (elapsed*MICROSECONDS_PER_SECOND)/GetClockFreq();
e2478fde
VZ
161}
162
163#endif // wxUSE_STOPWATCH
164
165// ----------------------------------------------------------------------------
166// old timer functions superceded by wxStopWatch
167// ----------------------------------------------------------------------------
168
169#if wxUSE_LONGLONG
170
171static wxLongLong wxStartTime = 0l;
172
173// starts the global timer
174void wxStartTimer()
175{
e1645882 176 wxStartTime = wxGetUTCTimeMillis();
e2478fde
VZ
177}
178
179// Returns elapsed time in milliseconds
180long wxGetElapsedTime(bool resetTimer)
181{
182 wxLongLong oldTime = wxStartTime;
e1645882 183 wxLongLong newTime = wxGetUTCTimeMillis();
e2478fde
VZ
184
185 if ( resetTimer )
186 wxStartTime = newTime;
187
188 return (newTime - oldTime).GetLo();
189}
190
191#endif // wxUSE_LONGLONG