Clock.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Clock.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Clock.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: DateTime
  8. // Module: Clock
  9. //
  10. // Copyright (c) 2013, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Clock.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/Timestamp.h"
  18. #if defined(__MACH__)
  19. #include <mach/mach.h>
  20. #include <mach/clock.h>
  21. #elif defined(POCO_OS_FAMILY_UNIX)
  22. #include <time.h>
  23. #elif defined(POCO_VXWORKS)
  24. #include <timers.h>
  25. #elif defined(POCO_OS_FAMILY_WINDOWS)
  26. #include "Poco/UnWindows.h"
  27. #endif
  28. #include <algorithm>
  29. #undef min
  30. #undef max
  31. #include <limits>
  32. namespace Poco {
  33. const Clock::ClockVal Clock::CLOCKVAL_MIN = std::numeric_limits<Clock::ClockVal>::min();
  34. const Clock::ClockVal Clock::CLOCKVAL_MAX = std::numeric_limits<Clock::ClockVal>::max();
  35. Clock::Clock()
  36. {
  37. update();
  38. }
  39. Clock::Clock(ClockVal tv)
  40. {
  41. _clock = tv;
  42. }
  43. Clock::Clock(const Clock& other)
  44. {
  45. _clock = other._clock;
  46. }
  47. Clock::~Clock()
  48. {
  49. }
  50. Clock& Clock::operator = (const Clock& other)
  51. {
  52. _clock = other._clock;
  53. return *this;
  54. }
  55. Clock& Clock::operator = (ClockVal tv)
  56. {
  57. _clock = tv;
  58. return *this;
  59. }
  60. void Clock::swap(Clock& timestamp)
  61. {
  62. std::swap(_clock, timestamp._clock);
  63. }
  64. void Clock::update()
  65. {
  66. #if defined(POCO_OS_FAMILY_WINDOWS)
  67. LARGE_INTEGER perfCounter;
  68. LARGE_INTEGER perfFreq;
  69. if (QueryPerformanceCounter(&perfCounter) && QueryPerformanceFrequency(&perfFreq))
  70. {
  71. _clock = resolution()*(perfCounter.QuadPart/perfFreq.QuadPart);
  72. _clock += (perfCounter.QuadPart % perfFreq.QuadPart)*resolution()/perfFreq.QuadPart;
  73. }
  74. else throw Poco::SystemException("cannot get system clock");
  75. #elif defined(__MACH__)
  76. clock_serv_t cs;
  77. mach_timespec_t ts;
  78. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
  79. clock_get_time(cs, &ts);
  80. mach_port_deallocate(mach_task_self(), cs);
  81. _clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
  82. #elif defined(POCO_VXWORKS)
  83. struct timespec ts;
  84. #if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
  85. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  86. throw SystemException("cannot get system clock");
  87. #else
  88. if (clock_gettime(CLOCK_REALTIME, &ts))
  89. throw SystemException("cannot get system clock");
  90. #endif
  91. _clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
  92. #elif (defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)) || defined(__QNX__)
  93. struct timespec ts;
  94. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  95. throw SystemException("cannot get system clock");
  96. _clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
  97. #else
  98. Poco::Timestamp now;
  99. _clock = now.epochMicroseconds();
  100. #endif
  101. }
  102. Clock::ClockDiff Clock::accuracy()
  103. {
  104. #if defined(POCO_OS_FAMILY_WINDOWS)
  105. LARGE_INTEGER perfFreq;
  106. if (QueryPerformanceFrequency(&perfFreq) && perfFreq.QuadPart > 0)
  107. {
  108. ClockVal acc = resolution()/perfFreq.QuadPart;
  109. return acc > 0 ? acc : 1;
  110. }
  111. else throw Poco::SystemException("cannot get system clock accuracy");
  112. #elif defined(__MACH__)
  113. clock_serv_t cs;
  114. int nanosecs;
  115. mach_msg_type_number_t n = 1;
  116. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
  117. clock_get_attributes(cs, CLOCK_GET_TIME_RES, (clock_attr_t)&nanosecs, &n);
  118. mach_port_deallocate(mach_task_self(), cs);
  119. ClockVal acc = nanosecs/1000;
  120. return acc > 0 ? acc : 1;
  121. #elif defined(POCO_VXWORKS)
  122. struct timespec ts;
  123. #if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
  124. if (clock_getres(CLOCK_MONOTONIC, &ts))
  125. throw SystemException("cannot get system clock");
  126. #else
  127. if (clock_getres(CLOCK_REALTIME, &ts))
  128. throw SystemException("cannot get system clock");
  129. #endif
  130. ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
  131. return acc > 0 ? acc : 1;
  132. #elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
  133. struct timespec ts;
  134. if (clock_getres(CLOCK_MONOTONIC, &ts))
  135. throw SystemException("cannot get system clock");
  136. ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
  137. return acc > 0 ? acc : 1;
  138. #else
  139. return 1000;
  140. #endif
  141. }
  142. bool Clock::monotonic()
  143. {
  144. #if defined(POCO_OS_FAMILY_WINDOWS)
  145. return true;
  146. #elif defined(__MACH__)
  147. return true;
  148. #elif defined(POCO_VXWORKS)
  149. #if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
  150. return true;
  151. #else
  152. return false;
  153. #endif
  154. #elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
  155. return true;
  156. #else
  157. return false;
  158. #endif
  159. }
  160. } // namespace Poco