Upstream tarball 20080414
[amule.git] / src / GetTickCount.cpp
blobe1b2dd02b7daadc87024800c8d5b9988d179d2cc
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Alo Sarv <madcat_@users.sourceforge.net>
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2003 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 __WINDOWS__
31 void StartTickTimer(){};
33 void StopTickTimer(){};
35 uint32 GetTickCountFullRes() {
36 return GetTickCount();
39 /**
40 * Returns the tickcount in 64bits.
42 uint64 GetTickCount64()
44 // The base value, can store more than 49 days worth of ms.
45 static uint64 tick = 0;
46 // The current tickcount, may have overflown any number of times.
47 static uint32 lastTick = 0;
49 uint32 curTick = GetTickCount();
51 // Check for overflow
52 if ( curTick < lastTick ) {
53 // Change the base value to contain the overflown value.
54 tick += (uint64)1 << 32;
57 lastTick = curTick;
58 return tick + lastTick;
61 #else
63 #include <sys/time.h> // Needed for gettimeofday
65 uint32 GetTickCountFullRes(void) {
66 struct timeval aika;
67 gettimeofday(&aika,NULL);
68 unsigned long msecs = aika.tv_sec * 1000;
69 msecs += (aika.tv_usec / 1000);
70 return msecs;
73 #if wxUSE_GUI && wxUSE_TIMER && !defined(AMULE_DAEMON)
74 /**
75 * Copyright (c) 2004-2008 Alo Sarv <madcat_@users.sourceforge.net>
76 * wxTimer based implementation. wxGetLocalTimeMillis() is called every 2
77 * milliseconds and values stored in local variables. Upon requests for current
78 * time, values of those variables are returned. This means wxGetLocalTimeMillis
79 * is being called exactly 50 times per second at any case, no more no less.
81 #include <wx/timer.h>
83 class MyTimer : public wxTimer
85 public:
86 MyTimer() { tic32 = tic64 = wxGetLocalTimeMillis().GetValue(); Start(20); }
87 static uint32 GetTickCountNow() { return tic32; }
88 static uint64 GetTickCountNow64() { return tic64; }
89 private:
90 void Notify() { tic32 = tic64 = wxGetLocalTimeMillis().GetValue(); }
92 static uint32 tic32;
93 static uint64 tic64;
96 static class MyTimer* mytimer;
98 // Initialization of the static MyTimer member variables.
99 uint32 MyTimer::tic32 = 0;
100 uint64 MyTimer::tic64 = 0;
102 void StartTickTimer() {
103 wxASSERT(mytimer == NULL);
104 mytimer = new MyTimer();
107 void StopTickTimer() {
108 wxASSERT(mytimer != NULL);
109 delete mytimer;
110 mytimer = NULL;
113 uint32 GetTickCount(){
114 wxASSERT(mytimer != NULL);
115 return MyTimer::GetTickCountNow();
118 uint64 GetTickCount64(){
119 wxASSERT(mytimer != NULL);
120 return MyTimer::GetTickCountNow64();
123 #else
125 * Copyright (c) 2003-2008 Timo Kujala <tiku@users.sourceforge.net>
126 * gettimeofday() syscall based implementation. Upon request to GetTickCount(),
127 * gettimeofday syscall is being used to retrieve system time and returned. This
128 * means EACH GetTickCount() call will produce a new syscall, thus becoming
129 * increasingly heavy on CPU as the program uptime increases and more things
130 * need to be done.
132 void StartTickTimer() {}
134 void StopTickTimer() {}
136 uint32 GetTickCount() { return GetTickCountFullRes(); }
138 // avoids 32bit rollover error for differences above 50days
139 uint64 GetTickCount64() {
140 struct timeval aika;
141 gettimeofday(&aika,NULL);
142 return aika.tv_sec * 1000 + aika.tv_usec / 1000;
145 #endif
147 #endif
148 // File_checked_for_headers