Clock.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright 2010 Jukka Jylänki
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. /** @file Clock.cpp
  12. @brief */
  13. // Modified by Lasse Oorni for Urho3D
  14. // Urho3D: ensure that kNetBuildConfig.h is included for WinXP compatibility
  15. #include "kNetBuildConfig.h"
  16. #if defined(__unix__) || defined(__native_client__) || defined(EMSCRIPTEN) || defined(ANDROID) || defined(__APPLE__) || defined (__CYGWIN__)
  17. #include <time.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <sys/time.h>
  21. #endif
  22. #ifdef WIN32
  23. #define NOMINMAX
  24. #include <windows.h>
  25. #endif
  26. #ifdef EMSCRIPTEN
  27. #include <emscripten.h>
  28. #endif
  29. #include "kNet/Clock.h"
  30. #include "kNet/NetworkLogging.h"
  31. namespace kNet
  32. {
  33. #ifdef WIN32
  34. LARGE_INTEGER Clock::ddwTimerFrequency;
  35. #endif
  36. tick_t Clock::appStartTime = 0;
  37. Clock impl;
  38. void Clock::InitClockData()
  39. {
  40. if (appStartTime == 0)
  41. appStartTime = Tick();
  42. #ifdef WIN32
  43. if (!QueryPerformanceFrequency(&ddwTimerFrequency))
  44. {
  45. KNET_LOG(LogError, "The system doesn't support high-resolution timers!");
  46. ddwTimerFrequency.HighPart = (unsigned long)-1;
  47. ddwTimerFrequency.LowPart = (unsigned long)-1;
  48. }
  49. if (ddwTimerFrequency.HighPart > 0)
  50. KNET_LOG(LogError, "Warning: Clock::TicksPerSec will yield invalid timing data!");
  51. if (appStartTime == 0)
  52. {
  53. #if WINVER >= 0x0600 && !defined(KNET_ENABLE_WINXP_SUPPORT)
  54. appStartTime = (tick_t)GetTickCount64();
  55. #else
  56. appStartTime = (tick_t)GetTickCount();
  57. #endif
  58. }
  59. ///\todo Test here that the return values of QueryPerformanceCounter is nondecreasing.
  60. #endif
  61. }
  62. Clock::Clock()
  63. {
  64. InitClockData();
  65. }
  66. void Clock::Sleep(int milliseconds)
  67. {
  68. #ifdef WIN8RT
  69. #pragma WARNING(Clock::Sleep has not been implemented!)
  70. #elif defined(WIN32)
  71. ::Sleep(milliseconds);
  72. #elif !defined(__native_client__) && !defined(EMSCRIPTEN) && !defined(__APPLE__)
  73. // http://linux.die.net/man/2/nanosleep
  74. timespec ts;
  75. ts.tv_sec = milliseconds / 1000;
  76. ts.tv_nsec = (milliseconds - ts.tv_sec * 1000) * 1000 * 1000;
  77. int ret = nanosleep(&ts, NULL);
  78. if (ret == -1)
  79. KNET_LOG(LogError, "nanosleep returned -1! Reason: %s(%d).", strerror(errno), (int)errno);
  80. #else
  81. #warning Clock::Sleep has not been implemented!
  82. #endif
  83. }
  84. int Clock::Year()
  85. {
  86. #ifdef WIN32
  87. SYSTEMTIME s;
  88. GetSystemTime(&s);
  89. return s.wYear;
  90. #else
  91. ///\todo.
  92. return 0;
  93. #endif
  94. }
  95. int Clock::Month()
  96. {
  97. #ifdef WIN32
  98. SYSTEMTIME s;
  99. GetSystemTime(&s);
  100. return s.wMonth;
  101. #else
  102. ///\todo.
  103. return 0;
  104. #endif
  105. }
  106. int Clock::Day()
  107. {
  108. #ifdef WIN32
  109. SYSTEMTIME s;
  110. GetSystemTime(&s);
  111. return s.wDay;
  112. #else
  113. ///\todo.
  114. return 0;
  115. #endif
  116. }
  117. int Clock::Hour()
  118. {
  119. #ifdef WIN32
  120. SYSTEMTIME s;
  121. GetSystemTime(&s);
  122. return s.wHour;
  123. #else
  124. ///\todo.
  125. return 0;
  126. #endif
  127. }
  128. int Clock::Min()
  129. {
  130. #ifdef WIN32
  131. SYSTEMTIME s;
  132. GetSystemTime(&s);
  133. return s.wMinute;
  134. #else
  135. ///\todo.
  136. return 0;
  137. #endif
  138. }
  139. int Clock::Sec()
  140. {
  141. #ifdef WIN32
  142. SYSTEMTIME s;
  143. GetSystemTime(&s);
  144. return s.wSecond;
  145. #else
  146. ///\todo.
  147. return 0;
  148. #endif
  149. }
  150. unsigned long Clock::SystemTime()
  151. {
  152. #ifdef WIN32
  153. #if WINVER >= 0x0600 && !defined(KNET_ENABLE_WINXP_SUPPORT)
  154. return (unsigned long)GetTickCount64();
  155. #else
  156. return (unsigned long)GetTickCount();
  157. #endif
  158. #else
  159. return TickU32();
  160. #endif
  161. }
  162. /*
  163. tick_t Clock::ApplicationStartupTick()
  164. {
  165. return appStartTime;
  166. }
  167. */
  168. unsigned long Clock::Time()
  169. {
  170. return (unsigned long)(Tick() - appStartTime);
  171. }
  172. tick_t Clock::Tick()
  173. {
  174. #if defined(ANDROID)
  175. struct timespec res;
  176. clock_gettime(CLOCK_REALTIME, &res);
  177. return 1000000000ULL*res.tv_sec + (tick_t)res.tv_nsec;
  178. #elif defined(EMSCRIPTEN)
  179. // emscripten_get_now() returns a wallclock time as a float in milliseconds (1e-3).
  180. // scale it to microseconds (1e-6) and return as a tick.
  181. return (tick_t)(((double)emscripten_get_now()) * 1e3);
  182. // return (tick_t)clock();
  183. #elif defined(WIN32)
  184. LARGE_INTEGER ddwTimer;
  185. QueryPerformanceCounter(&ddwTimer);
  186. return ddwTimer.QuadPart;
  187. #elif defined(_POSIX_MONOTONIC_CLOCK)
  188. timespec t;
  189. clock_gettime(CLOCK_MONOTONIC, &t);
  190. return (tick_t)t.tv_sec * 1000 * 1000 * 1000 + (tick_t)t.tv_nsec;
  191. #elif defined(_POSIX_C_SOURCE) || defined(__APPLE__)
  192. timeval t;
  193. gettimeofday(&t, NULL);
  194. return (tick_t)t.tv_sec * 1000 * 1000 + (tick_t)t.tv_usec;
  195. #else
  196. return (tick_t)clock();
  197. #endif
  198. }
  199. unsigned long Clock::TickU32()
  200. {
  201. #ifdef WIN32
  202. LARGE_INTEGER ddwTimer;
  203. QueryPerformanceCounter(&ddwTimer);
  204. return ddwTimer.LowPart;
  205. #else
  206. return (unsigned long)Tick();
  207. #endif
  208. }
  209. tick_t Clock::TicksPerSec()
  210. {
  211. #if defined(ANDROID)
  212. return 1000000000ULL; // 1e9 == nanoseconds.
  213. #elif defined(EMSCRIPTEN)
  214. return 1000000ULL; // 1e6 == microseconds.
  215. // return CLOCKS_PER_SEC;
  216. #elif defined(WIN32)
  217. return ddwTimerFrequency.QuadPart;
  218. #elif defined(_POSIX_MONOTONIC_CLOCK)
  219. return 1000 * 1000 * 1000;
  220. #elif defined(_POSIX_C_SOURCE) || defined(__APPLE__)
  221. return 1000 * 1000;
  222. #else
  223. return CLOCKS_PER_SEC;
  224. #endif
  225. }
  226. } // ~kNet