UnixClock.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 UnixClock.cpp
  12. @brief */
  13. #include <cassert>
  14. #include <time.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include "kNet/Clock.h"
  19. #include "kNet/NetworkLogging.h"
  20. namespace kNet
  21. {
  22. tick_t Clock::appStartTime = 0;
  23. Clock impl;
  24. void Clock::InitClockData()
  25. {
  26. if (appStartTime == 0)
  27. appStartTime = Tick();
  28. }
  29. Clock::Clock()
  30. {
  31. InitClockData();
  32. }
  33. void Clock::Sleep(int milliseconds)
  34. {
  35. // http://linux.die.net/man/2/nanosleep
  36. timespec ts;
  37. ts.tv_sec = milliseconds / 1000;
  38. ts.tv_nsec = (milliseconds - ts.tv_sec * 1000) * 1000 * 1000;
  39. int ret = nanosleep(&ts, NULL);
  40. if (ret == -1)
  41. LOG(LogError, "nanosleep returned -1! Reason: %s(%d).", strerror(errno), (int)errno);
  42. }
  43. int Clock::Year()
  44. {
  45. ///\todo.
  46. return 0;
  47. }
  48. int Clock::Month()
  49. {
  50. ///\todo.
  51. return 0;
  52. }
  53. int Clock::Day()
  54. {
  55. ///\todo.
  56. return 0;
  57. }
  58. int Clock::Hour()
  59. {
  60. ///\todo.
  61. return 0;
  62. }
  63. int Clock::Min()
  64. {
  65. ///\todo.
  66. return 0;
  67. }
  68. int Clock::Sec()
  69. {
  70. ///\todo.
  71. return 0;
  72. }
  73. unsigned long Clock::SystemTime()
  74. {
  75. return TickU32();
  76. }
  77. unsigned long Clock::Time()
  78. {
  79. return (unsigned long)(Tick() - appStartTime);
  80. }
  81. tick_t Clock::Tick()
  82. {
  83. #ifdef _POSIX_MONOTONIC_CLOCK
  84. timespec t;
  85. clock_gettime(CLOCK_MONOTONIC, &t);
  86. return (tick_t)t.tv_sec * 1000 * 1000 * 1000 + (tick_t)t.tv_nsec;
  87. //_POSIX_C_SOURCE is not defined on OSX
  88. #elif defined(_POSIX_C_SOURCE) || defined(__APPLE__)
  89. timeval t;
  90. gettimeofday(&t, NULL);
  91. return (tick_t)t.tv_sec * 1000 * 1000 + (tick_t)t.tv_usec;
  92. #else
  93. return (tick_t)time(NULL);
  94. #endif
  95. }
  96. unsigned long Clock::TickU32()
  97. {
  98. return (unsigned long)Tick();
  99. }
  100. tick_t Clock::TicksPerSec()
  101. {
  102. #ifdef _POSIX_MONOTONIC_CLOCK
  103. return 1000 * 1000 * 1000;
  104. //_POSIX_C_SOURCE is not defined on OSX
  105. #elif defined(_POSIX_C_SOURCE) || defined(__APPLE__)
  106. return 1000 * 1000;
  107. #else
  108. return 1;
  109. #endif
  110. }
  111. } // ~kNet