SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GetTime.cpp
Go to the documentation of this file.
1 /*
2  * Original work: Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
18 
19 
20 #if defined(_WIN32)
22 
23  #if !defined(WINDOWS_PHONE_8)
24  // To call timeGetTime
25  // on Code::Blocks, this needs to be libwinmm.a instead
26  #pragma comment(lib, "Winmm.lib")
27  #endif
28 
29 #endif
30 
31 #include "slikenet/GetTime.h"
32 
33 
34 
35 
36 #if defined(_WIN32)
37 //DWORD mProcMask;
38 //DWORD mSysMask;
39 //HANDLE mThread;
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 #else
50 #include <sys/time.h>
51 #include <unistd.h>
53 #endif
54 
55 static bool initialized=false;
56 
57 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
58 #include "slikenet/SimpleMutex.h"
59 SLNet::TimeUS lastNormalizedReturnedValue=0;
60 SLNet::TimeUS lastNormalizedInputValue=0;
64 SLNet::TimeUS NormalizeTime(SLNet::TimeUS timeIn)
65 {
66  SLNet::TimeUS diff, lastNormalizedReturnedValueCopy;
67  static SLNet::SimpleMutex mutex;
68 
69  mutex.Lock();
70  if (timeIn>=lastNormalizedInputValue)
71  {
72  diff = timeIn-lastNormalizedInputValue;
73  if (diff > GET_TIME_SPIKE_LIMIT)
74  lastNormalizedReturnedValue+=GET_TIME_SPIKE_LIMIT;
75  else
76  lastNormalizedReturnedValue+=diff;
77  }
78  else
79  lastNormalizedReturnedValue+=GET_TIME_SPIKE_LIMIT;
80 
81  lastNormalizedInputValue=timeIn;
82  lastNormalizedReturnedValueCopy=lastNormalizedReturnedValue;
83  mutex.Unlock();
84 
85  return lastNormalizedReturnedValueCopy;
86 }
87 #endif // #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
89 {
90  return (SLNet::Time)(GetTimeUS()/1000);
91 }
93 {
94  return (SLNet::TimeMS)(GetTimeUS()/1000);
95 }
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 #if defined(_WIN32)
145 SLNet::TimeUS GetTimeUS_Windows( void )
146 {
147  if ( initialized == false)
148  {
149  initialized = true;
150 
151  // Save the current process
152 #if !defined(_WIN32_WCE)
153 // HANDLE mProc = GetCurrentProcess();
154 
155  // Get the current Affinity
156 #if defined (_M_X64)
157 // GetProcessAffinityMask(mProc, (PDWORD_PTR)&mProcMask, (PDWORD_PTR)&mSysMask);
158 #else
159 // GetProcessAffinityMask(mProc, &mProcMask, &mSysMask);
160 #endif
161 // mThread = GetCurrentThread();
162 
163 #endif // _WIN32_WCE
164  }
165 
166  // 9/26/2010 In China running LuDaShi, QueryPerformanceFrequency has to be called every time because CPU clock speeds can be different
167  SLNet::TimeUS curTime;
168  LARGE_INTEGER PerfVal;
169  LARGE_INTEGER yo1;
170 
171  QueryPerformanceFrequency( &yo1 );
172  QueryPerformanceCounter( &PerfVal );
173 
174  __int64 quotient, remainder;
175  quotient=((PerfVal.QuadPart) / yo1.QuadPart);
176  remainder=((PerfVal.QuadPart) % yo1.QuadPart);
177  curTime = (SLNet::TimeUS) quotient*(SLNet::TimeUS)1000000 + (remainder*(SLNet::TimeUS)1000000 / yo1.QuadPart);
178 
179 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
180  return NormalizeTime(curTime);
181 #else
182  return curTime;
183 #endif // #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
184 }
185 #elif defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__)
186 SLNet::TimeUS GetTimeUS_Linux( void )
187 {
188  timeval tp;
189  if ( initialized == false)
190  {
191  gettimeofday( &tp, 0 );
192  initialized=true;
193  // I do this because otherwise SLNet::Time in milliseconds won't work as it will underflow when dividing by 1000 to do the conversion
194  initialTime = ( tp.tv_sec ) * (SLNet::TimeUS) 1000000 + ( tp.tv_usec );
195  }
196 
197  // GCC
198  SLNet::TimeUS curTime;
199  gettimeofday( &tp, 0 );
200 
201  curTime = ( tp.tv_sec ) * (SLNet::TimeUS) 1000000 + ( tp.tv_usec );
202 
203 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
204  return NormalizeTime(curTime - initialTime);
205 #else
206  return curTime - initialTime;
207 #endif // #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
208 }
209 #endif
210 
212 {
213 
214 
215 
216 
217 
218 
219 #if defined(_WIN32)
220  return GetTimeUS_Windows();
221 #else
222  return GetTimeUS_Linux();
223 #endif
224 }
226 {
227  // a > b?
228  const SLNet::Time halfSpan =(SLNet::Time) (((SLNet::Time)(const SLNet::Time)-1)/(SLNet::Time)2);
229  return b!=a && b-a>halfSpan;
230 }
232 {
233  // a < b?
234  const SLNet::Time halfSpan = ((SLNet::Time)(const SLNet::Time)-1)/(SLNet::Time)2;
235  return b!=a && b-a<halfSpan;
236 }