BTime.h 4.2 KB

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