You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/timing.h"
  28. #include "talk/base/timeutils.h"
  29. #if defined(POSIX)
  30. #include <errno.h>
  31. #include <math.h>
  32. #include <sys/time.h>
  33. #if defined(OSX)
  34. #include <mach/mach.h>
  35. #include <mach/clock.h>
  36. #endif
  37. #elif defined(WIN32)
  38. #include <sys/timeb.h>
  39. #include "talk/base/win32.h"
  40. #endif
  41. namespace talk_base {
  42. Timing::Timing() {
  43. #if defined(WIN32)
  44. // This may fail, but we handle failure gracefully in the methods
  45. // that use it (use alternative sleep method).
  46. //
  47. // TODO: Make it possible for user to tell if IdleWait will
  48. // be done at lesser resolution because of this.
  49. timer_handle_ = CreateWaitableTimer(NULL, // Security attributes.
  50. FALSE, // Manual reset?
  51. NULL); // Timer name.
  52. #endif
  53. }
  54. Timing::~Timing() {
  55. #if defined(WIN32)
  56. if (timer_handle_ != NULL)
  57. CloseHandle(timer_handle_);
  58. #endif
  59. }
  60. double Timing::WallTimeNow() {
  61. #if defined(POSIX)
  62. struct timeval time;
  63. gettimeofday(&time, NULL);
  64. // Convert from second (1.0) and microsecond (1e-6).
  65. return (static_cast<double>(time.tv_sec) +
  66. static_cast<double>(time.tv_usec) * 1.0e-6);
  67. #elif defined(WIN32)
  68. struct _timeb time;
  69. _ftime(&time);
  70. // Convert from second (1.0) and milliseconds (1e-3).
  71. return (static_cast<double>(time.time) +
  72. static_cast<double>(time.millitm) * 1.0e-3);
  73. #endif
  74. }
  75. double Timing::TimerNow() {
  76. return (static_cast<double>(TimeNanos()) / kNumNanosecsPerSec);
  77. }
  78. double Timing::BusyWait(double period) {
  79. double start_time = TimerNow();
  80. while (TimerNow() - start_time < period) {
  81. }
  82. return TimerNow() - start_time;
  83. }
  84. double Timing::IdleWait(double period) {
  85. double start_time = TimerNow();
  86. #if defined(POSIX)
  87. double sec_int, sec_frac = modf(period, &sec_int);
  88. struct timespec ts;
  89. ts.tv_sec = static_cast<time_t>(sec_int);
  90. ts.tv_nsec = static_cast<long>(sec_frac * 1.0e9); // NOLINT
  91. // NOTE(liulk): for the NOLINT above, long is the appropriate POSIX
  92. // type.
  93. // POSIX nanosleep may be interrupted by signals.
  94. while (nanosleep(&ts, &ts) == -1 && errno == EINTR) {
  95. }
  96. #elif defined(WIN32)
  97. if (timer_handle_ != NULL) {
  98. LARGE_INTEGER due_time;
  99. // Negative indicates relative time. The unit is 100 nanoseconds.
  100. due_time.QuadPart = -LONGLONG(period * 1.0e7);
  101. SetWaitableTimer(timer_handle_, &due_time, 0, NULL, NULL, TRUE);
  102. WaitForSingleObject(timer_handle_, INFINITE);
  103. } else {
  104. // Still attempts to sleep with lesser resolution.
  105. // The unit is in milliseconds.
  106. Sleep(DWORD(period * 1.0e3));
  107. }
  108. #endif
  109. return TimerNow() - start_time;
  110. }
  111. } // namespace talk_base