1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2003-2011 Alo Sarv ( madcat_@users.sourceforge.net )
5// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6// Copyright (c) 2002-2011 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.
11//
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.
16//
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
25//
26
27#include "GetTickCount.h" // Interface
28
29uint32 TheTime = 0;
30
31#ifdef __WXMSW__
32
33void StartTickTimer(){};
34
35void StopTickTimer(){};
36
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)
41 */
42uint32 GetTickCountFullRes() {
43	return GetTickCount_64();
44}
45
46/**
47 * Returns the tickcount in 64bits.
48 */
49uint64 GetTickCount_64()
50{
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.
55
56	// Convert hires ticks to milliseconds
57	static double tickFactor;
58	_LARGE_INTEGER li;
59
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;
66	}
67
68	QueryPerformanceCounter(&li);
69	return li.QuadPart * tickFactor;
70}
71
72#else
73
74#include <sys/time.h>		// Needed for gettimeofday
75
76uint32 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;
82}
83
84#if wxUSE_GUI && wxUSE_TIMER && !defined(AMULE_DAEMON)
85/**
86 * Copyright (c) 2003-2011 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.
91 */
92	#include <wx/timer.h>
93
94	class MyTimer : public wxTimer
95	{
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(); }
102
103		static uint32 tic32;
104		static uint64 tic64;
105	};
106
107	static class MyTimer* mytimer;
108
109	// Initialization of the static MyTimer member variables.
110	uint32 MyTimer::tic32 = 0;
111	uint64 MyTimer::tic64 = 0;
112
113	void StartTickTimer() {
114		wxASSERT(mytimer == NULL);
115		mytimer = new MyTimer();
116	}
117
118	void StopTickTimer() {
119		wxASSERT(mytimer != NULL);
120		delete mytimer;
121		mytimer = NULL;
122	}
123
124	uint32 GetTickCount(){
125		wxASSERT(mytimer != NULL);
126		return MyTimer::GetTickCountNow();
127	}
128
129	uint64 GetTickCount64(){
130		wxASSERT(mytimer != NULL);
131		return MyTimer::GetTickCountNow64();
132	}
133
134#else
135/**
136 * Copyright (c) 2002-2011 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.
142 */
143	void StartTickTimer() {}
144
145	void StopTickTimer() {}
146
147	uint32 GetTickCount() { return GetTickCountFullRes(); }
148
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;
154	}
155
156#endif
157
158#endif
159// File_checked_for_headers
160