BTime.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #ifdef BADVPN_USE_WINAPI
  36. #include <windows.h>
  37. #else
  38. #include <time.h>
  39. #include <sys/time.h>
  40. #endif
  41. #include <stdint.h>
  42. #include <misc/debug.h>
  43. #include <misc/overflow.h>
  44. #include <base/BLog.h>
  45. #include <generated/blog_channel_BTime.h>
  46. typedef int64_t btime_t;
  47. struct _BTime_global {
  48. #ifndef NDEBUG
  49. int initialized; // initialized statically
  50. #endif
  51. #ifdef BADVPN_USE_WINAPI
  52. LARGE_INTEGER start_time;
  53. #else
  54. btime_t start_time;
  55. int use_gettimeofday;
  56. #endif
  57. };
  58. extern struct _BTime_global btime_global;
  59. static void BTime_Init (void)
  60. {
  61. ASSERT(!btime_global.initialized)
  62. #ifdef BADVPN_USE_WINAPI
  63. ASSERT_FORCE(QueryPerformanceCounter(&btime_global.start_time))
  64. #else
  65. struct timespec ts;
  66. if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
  67. BLog(BLOG_WARNING, "CLOCK_MONOTONIC is not available. Timers will be confused by clock changes.");
  68. struct timeval tv;
  69. ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
  70. btime_global.start_time = (int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000;
  71. btime_global.use_gettimeofday = 1;
  72. } else {
  73. btime_global.start_time = (int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000;
  74. btime_global.use_gettimeofday = 0;
  75. }
  76. #endif
  77. #ifndef NDEBUG
  78. btime_global.initialized = 1;
  79. #endif
  80. }
  81. static btime_t btime_gettime (void)
  82. {
  83. ASSERT(btime_global.initialized)
  84. #ifdef BADVPN_USE_WINAPI
  85. LARGE_INTEGER count;
  86. LARGE_INTEGER freq;
  87. ASSERT_FORCE(QueryPerformanceCounter(&count))
  88. ASSERT_FORCE(QueryPerformanceFrequency(&freq))
  89. return (((count.QuadPart - btime_global.start_time.QuadPart) * 1000) / freq.QuadPart);
  90. #else
  91. if (btime_global.use_gettimeofday) {
  92. struct timeval tv;
  93. ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
  94. return ((int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000);
  95. } else {
  96. struct timespec ts;
  97. ASSERT_FORCE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  98. return (((int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000) - btime_global.start_time);
  99. }
  100. #endif
  101. }
  102. static btime_t btime_add (btime_t t1, btime_t t2)
  103. {
  104. // handle overflow
  105. int overflows = add_int64_overflows(t1, t2);
  106. btime_t sum;
  107. if (overflows != 0) {
  108. if (overflows > 0) {
  109. sum = INT64_MAX;
  110. } else {
  111. sum = INT64_MIN;
  112. }
  113. } else {
  114. sum = t1 + t2;
  115. }
  116. return sum;
  117. }
  118. static btime_t btime_getpast (void)
  119. {
  120. return INT64_MIN;
  121. }
  122. #endif