Upstream tarball 9920
[amule.git] / src / GetTickCount.cpp
blob09b096b97a29efa1758b097a9aaa6d62baa928db
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 uint32 TheTime = 0;
31 #ifdef __WXMSW__
33 void StartTickTimer(){};
35 void StopTickTimer(){};
37 /**
38 * Returns the tickcount in full resolution using the highres timer.
39 * This function replaces calls to the low res system function GetTickCOunt
40 * (which can also be messed up when an app changes the system timer resolution)
42 uint32 GetTickCountFullRes() {
43 return GetTickCount_64();
46 /**
47 * Returns the tickcount in 64bits.
49 uint64 GetTickCount_64()
51 // Use highres timer for all operations on Windows
52 // The Timer starts at system boot and runs (on a Intel Quad core)
53 // with 14 million ticks per second. So it won't overflow for
54 // 35000 years.
56 // Convert hires ticks to milliseconds
57 static double tickFactor;
58 _LARGE_INTEGER li;
60 static bool first = true;
61 if (first) {
62 // calculate the conversion factor for the highres timer
63 QueryPerformanceFrequency(&li);
64 tickFactor = 1000.0 / li.QuadPart;
65 first = false;
68 QueryPerformanceCounter(&li);
69 return li.QuadPart * tickFactor;
72 #else
74 #include <sys/time.h> // Needed for gettimeofday
76 uint32 GetTickCountFullRes(void) {
77 struct timeval aika;
78 gettimeofday(&aika,NULL);
79 unsigned long msecs = aika.tv_sec * 1000;
80 msecs += (aika.tv_usec / 1000);
81 return msecs;
84 #if wxUSE_GUI && wxUSE_TIMER && !defined(AMULE_DAEMON)
85 /**
86 * Copyright (c) 2003-2008 Alo Sarv ( madcat_@users.sourceforge.net )
87 * wxTimer based implementation. wxGetLocalTimeMillis() is called every 2
88 * milliseconds and values stored in local variables. Upon requests for current
89 * time, values of those variables are returned. This means wxGetLocalTimeMillis
90 * is being called exactly 50 times per second at any case, no more no less.
92 #include <wx/timer.h>
94 class MyTimer : public wxTimer
96 public:
97 MyTimer() { tic32 = tic64 = wxGetLocalTimeMillis().GetValue(); Start(20); }
98 static uint32 GetTickCountNow() { return tic32; }
99 static uint64 GetTickCountNow64() { return tic64; }
100 private:
101 void Notify() { tic32 = tic64 = wxGetLocalTimeMillis().GetValue(); }
103 static uint32 tic32;
104 static uint64 tic64;
107 static class MyTimer* mytimer;
109 // Initialization of the static MyTimer member variables.
110 uint32 MyTimer::tic32 = 0;
111 uint64 MyTimer::tic64 = 0;
113 void StartTickTimer() {
114 wxASSERT(mytimer == NULL);
115 mytimer = new MyTimer();
118 void StopTickTimer() {
119 wxASSERT(mytimer != NULL);
120 delete mytimer;
121 mytimer = NULL;
124 uint32 GetTickCount(){
125 wxASSERT(mytimer != NULL);
126 return MyTimer::GetTickCountNow();
129 uint64 GetTickCount64(){
130 wxASSERT(mytimer != NULL);
131 return MyTimer::GetTickCountNow64();
134 #else
136 * Copyright (c) 2002-2008 Timo Kujala ( tiku@users.sourceforge.net )
137 * gettimeofday() syscall based implementation. Upon request to GetTickCount(),
138 * gettimeofday syscall is being used to retrieve system time and returned. This
139 * means EACH GetTickCount() call will produce a new syscall, thus becoming
140 * increasingly heavy on CPU as the program uptime increases and more things
141 * need to be done.
143 void StartTickTimer() {}
145 void StopTickTimer() {}
147 uint32 GetTickCount() { return GetTickCountFullRes(); }
149 // avoids 32bit rollover error for differences above 50days
150 uint64 GetTickCount64() {
151 struct timeval aika;
152 gettimeofday(&aika,NULL);
153 return aika.tv_sec * (uint64)1000 + aika.tv_usec / 1000;
156 #endif
158 #endif
159 // File_checked_for_headers