BTime.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @file BTime.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * System time abstraction used by {@link BReactor}.
  32. */
  33. #ifndef BADVPN_SYSTEM_BTIME_H
  34. #define BADVPN_SYSTEM_BTIME_H
  35. #if defined(BADVPN_USE_WINAPI)
  36. #include <windows.h>
  37. #elif defined(BADVPN_EMSCRIPTEN)
  38. #include <emscripten/emscripten.h>
  39. #else
  40. #include <time.h>
  41. #include <sys/time.h>
  42. #endif
  43. #include <stdint.h>
  44. #include <misc/debug.h>
  45. #include <misc/overflow.h>
  46. #include <base/BLog.h>
  47. #include <generated/blog_channel_BTime.h>
  48. typedef int64_t btime_t;
  49. #define BTIME_MIN INT64_MIN
  50. struct _BTime_global {
  51. #ifndef NDEBUG
  52. int initialized; // initialized statically
  53. #endif
  54. #if defined(BADVPN_USE_WINAPI)
  55. LARGE_INTEGER start_time;
  56. #elif defined(BADVPN_EMSCRIPTEN)
  57. btime_t start_time;
  58. #else
  59. btime_t start_time;
  60. int use_gettimeofday;
  61. #endif
  62. };
  63. extern struct _BTime_global btime_global;
  64. static void BTime_Init (void)
  65. {
  66. ASSERT(!btime_global.initialized)
  67. #if defined(BADVPN_USE_WINAPI)
  68. ASSERT_FORCE(QueryPerformanceCounter(&btime_global.start_time))
  69. #elif defined(BADVPN_EMSCRIPTEN)
  70. btime_global.start_time = emscripten_get_now();
  71. #else
  72. struct timespec ts;
  73. if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
  74. BLog(BLOG_WARNING, "CLOCK_MONOTONIC is not available. Timers will be confused by clock changes.");
  75. struct timeval tv;
  76. ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
  77. btime_global.start_time = (int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000;
  78. btime_global.use_gettimeofday = 1;
  79. } else {
  80. btime_global.start_time = (int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000;
  81. btime_global.use_gettimeofday = 0;
  82. }
  83. #endif
  84. #ifndef NDEBUG
  85. btime_global.initialized = 1;
  86. #endif
  87. }
  88. static btime_t btime_gettime (void)
  89. {
  90. ASSERT(btime_global.initialized)
  91. #if defined(BADVPN_USE_WINAPI)
  92. LARGE_INTEGER count;
  93. LARGE_INTEGER freq;
  94. ASSERT_FORCE(QueryPerformanceCounter(&count))
  95. ASSERT_FORCE(QueryPerformanceFrequency(&freq))
  96. return (((count.QuadPart - btime_global.start_time.QuadPart) * 1000) / freq.QuadPart);
  97. #elif defined(BADVPN_EMSCRIPTEN)
  98. return (btime_t)emscripten_get_now() - btime_global.start_time;
  99. #else
  100. if (btime_global.use_gettimeofday) {
  101. struct timeval tv;
  102. ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
  103. return ((int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000);
  104. } else {
  105. struct timespec ts;
  106. ASSERT_FORCE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  107. return (((int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000) - btime_global.start_time);
  108. }
  109. #endif
  110. }
  111. static btime_t btime_add (btime_t t1, btime_t t2)
  112. {
  113. // handle overflow
  114. int overflows = add_int64_overflows(t1, t2);
  115. btime_t sum;
  116. if (overflows != 0) {
  117. if (overflows > 0) {
  118. sum = INT64_MAX;
  119. } else {
  120. sum = INT64_MIN;
  121. }
  122. } else {
  123. sum = t1 + t2;
  124. }
  125. return sum;
  126. }
  127. static btime_t btime_getpast (void)
  128. {
  129. return INT64_MIN;
  130. }
  131. #endif