Clock.cpp 5.3 KB

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