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.

152 lines
4.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, 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. #ifdef POSIX
  28. #include <sys/time.h>
  29. #if defined(OSX) || defined(IOS)
  30. #include <mach/mach_time.h>
  31. #endif
  32. #endif
  33. #ifdef WIN32
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #include <mmsystem.h>
  37. #endif
  38. #include "talk/base/common.h"
  39. #include "talk/base/timeutils.h"
  40. #define EFFICIENT_IMPLEMENTATION 1
  41. namespace talk_base {
  42. const uint32 LAST = 0xFFFFFFFF;
  43. const uint32 HALF = 0x80000000;
  44. uint64 TimeNanos() {
  45. int64 ticks = 0;
  46. #if defined(OSX) || defined(IOS)
  47. static mach_timebase_info_data_t timebase;
  48. if (timebase.denom == 0) {
  49. // Get the timebase if this is the first time we run.
  50. // Recommended by Apple's QA1398.
  51. VERIFY(KERN_SUCCESS == mach_timebase_info(&timebase));
  52. }
  53. // Use timebase to convert absolute time tick units into nanoseconds.
  54. ticks = mach_absolute_time() * timebase.numer / timebase.denom;
  55. #elif defined(POSIX)
  56. struct timespec ts;
  57. // TODO: Do we need to handle the case when CLOCK_MONOTONIC
  58. // is not supported?
  59. clock_gettime(CLOCK_MONOTONIC, &ts);
  60. ticks = kNumNanosecsPerSec * static_cast<int64>(ts.tv_sec) +
  61. static_cast<int64>(ts.tv_nsec);
  62. #elif defined(WIN32)
  63. static volatile LONG last_timegettime = 0;
  64. static volatile int64 num_wrap_timegettime = 0;
  65. volatile LONG* last_timegettime_ptr = &last_timegettime;
  66. DWORD now = timeGetTime();
  67. // Atomically update the last gotten time
  68. DWORD old = InterlockedExchange(last_timegettime_ptr, now);
  69. if (now < old) {
  70. // If now is earlier than old, there may have been a race between
  71. // threads.
  72. // 0x0fffffff ~3.1 days, the code will not take that long to execute
  73. // so it must have been a wrap around.
  74. if (old > 0xf0000000 && now < 0x0fffffff) {
  75. num_wrap_timegettime++;
  76. }
  77. }
  78. ticks = now + (num_wrap_timegettime << 32);
  79. // TODO: Calculate with nanosecond precision. Otherwise, we're just
  80. // wasting a multiply and divide when doing Time() on Windows.
  81. ticks = ticks * kNumNanosecsPerMillisec;
  82. #endif
  83. return ticks;
  84. }
  85. uint32 Time() {
  86. return static_cast<uint32>(TimeNanos() / kNumNanosecsPerMillisec);
  87. }
  88. uint32 TimeAfter(int32 elapsed) {
  89. ASSERT(elapsed >= 0);
  90. ASSERT(static_cast<uint32>(elapsed) < HALF);
  91. return Time() + elapsed;
  92. }
  93. bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later) {
  94. if (earlier <= later) {
  95. return ((earlier <= middle) && (middle <= later));
  96. } else {
  97. return !((later < middle) && (middle < earlier));
  98. }
  99. }
  100. bool TimeIsLaterOrEqual(uint32 earlier, uint32 later) {
  101. #if EFFICIENT_IMPLEMENTATION
  102. int32 diff = later - earlier;
  103. return (diff >= 0 && static_cast<uint32>(diff) < HALF);
  104. #else
  105. const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
  106. return later_or_equal;
  107. #endif
  108. }
  109. bool TimeIsLater(uint32 earlier, uint32 later) {
  110. #if EFFICIENT_IMPLEMENTATION
  111. int32 diff = later - earlier;
  112. return (diff > 0 && static_cast<uint32>(diff) < HALF);
  113. #else
  114. const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF);
  115. return !earlier_or_equal;
  116. #endif
  117. }
  118. int32 TimeDiff(uint32 later, uint32 earlier) {
  119. #if EFFICIENT_IMPLEMENTATION
  120. return later - earlier;
  121. #else
  122. const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
  123. if (later_or_equal) {
  124. if (earlier <= later) {
  125. return static_cast<long>(later - earlier);
  126. } else {
  127. return static_cast<long>(later + (LAST - earlier) + 1);
  128. }
  129. } else {
  130. if (later <= earlier) {
  131. return -static_cast<long>(earlier - later);
  132. } else {
  133. return -static_cast<long>(earlier + (LAST - later) + 1);
  134. }
  135. }
  136. #endif
  137. }
  138. } // namespace talk_base