Upstream tarball 9407
[amule.git] / src / GetTickCount.cpp
blob94f7da1efcd91ebfefa4d235cc4624ec33d9df7f
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 Alo Sarv ( madcat_@users.sourceforge.net )
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2002-2008 Timo Kujala ( tiku@users.sourceforge.net )
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "GetTickCount.h" // Interface
29 #ifdef __WXMSW__
31 void StartTickTimer(){};
33 void StopTickTimer(){};
35 /**
36 * Returns the tickcount in full resolution using the highres timer.
37 * This function replaces calls to the low res system function GetTickCOunt
38 * (which can also be messed up when an app changes the system timer resolution)
40 uint32 GetTickCountFullRes() {
41 return GetTickCount_64();
44 /**
45 * Returns the tickcount in 64bits.
47 uint64 GetTickCount_64()
49 // Use highres timer for all operations on Windows
50 // The Timer starts at system boot and runs (on a Intel Quad core)
51 // with 14 million ticks per second. So it won't overflow for
52 // 35000 years.
54 // Convert hires ticks to milliseconds
55 static double tickFactor;
56 _LARGE_INTEGER li;
58 static bool first = true;
59 if (first) {
60 // calculate the conversion factor for the highres timer
61 QueryPerformanceFrequency(&li);
62 tickFactor = 1000.0 / li.QuadPart;
63 first = false;
66 QueryPerformanceCounter(&li);
67 return li.QuadPart * tickFactor;
70 #else
72 #include <sys/time.h> // Needed for gettimeofday
74 uint32 GetTickCountFullRes(void) {
75 struct timeval aika;
76 gettimeofday(&aika,NULL);
77 unsigned long msecs = aika.tv_sec * 1000;
78 msecs += (aika.tv_usec / 1000);
79 return msecs;
82 #if wxUSE_GUI && wxUSE_TIMER && !defined(AMULE_DAEMON)
83 /**
84 * Copyright (c) 2003-2008 Alo Sarv ( madcat_@users.sourceforge.net )
85 * wxTimer based implementation. wxGetLocalTimeMillis() is called every 2
86 * milliseconds and values stored in local variables. Upon requests for current
87 * time, values of those variables are returned. This means wxGetLocalTimeMillis
88 * is being called exactly 50 times per second at any case, no more no less.
90 #include <wx/timer.h>
92 class MyTimer : public wxTimer
94 public:
95 MyTimer() { tic32 = tic64 = wxGetLocalTimeMillis().GetValue(); Start(20); }
96 static uint32 GetTickCountNow() { return tic32; }
97 static uint64 GetTickCountNow64() { return tic64; }
98 private:
99 void Notify() { tic32 = tic64 = wxGetLocalTimeMillis().GetValue(); }
101 static uint32 tic32;
102 static uint64 tic64;
105 static class MyTimer* mytimer;
107 // Initialization of the static MyTimer member variables.
108 uint32 MyTimer::tic32 = 0;
109 uint64 MyTimer::tic64 = 0;
111 void StartTickTimer() {
112 wxASSERT(mytimer == NULL);
113 mytimer = new MyTimer();
116 void StopTickTimer() {
117 wxASSERT(mytimer != NULL);
118 delete mytimer;
119 mytimer = NULL;
122 uint32 GetTickCount(){
123 wxASSERT(mytimer != NULL);
124 return MyTimer::GetTickCountNow();
127 uint64 GetTickCount64(){
128 wxASSERT(mytimer != NULL);
129 return MyTimer::GetTickCountNow64();
132 #else
134 * Copyright (c) 2002-2008 Timo Kujala ( tiku@users.sourceforge.net )
135 * gettimeofday() syscall based implementation. Upon request to GetTickCount(),
136 * gettimeofday syscall is being used to retrieve system time and returned. This
137 * means EACH GetTickCount() call will produce a new syscall, thus becoming
138 * increasingly heavy on CPU as the program uptime increases and more things
139 * need to be done.
141 void StartTickTimer() {}
143 void StopTickTimer() {}
145 uint32 GetTickCount() { return GetTickCountFullRes(); }
147 // avoids 32bit rollover error for differences above 50days
148 uint64 GetTickCount64() {
149 struct timeval aika;
150 gettimeofday(&aika,NULL);
151 return aika.tv_sec * (uint64)1000 + aika.tv_usec / 1000;
154 #endif
156 #endif
157 // File_checked_for_headers