BTime.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @file BTime.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * System time abstraction used by {@link BReactor}.
  25. */
  26. #ifndef BADVPN_SYSTEM_BTIME_H
  27. #define BADVPN_SYSTEM_BTIME_H
  28. #ifdef BADVPN_USE_WINAPI
  29. #include <windows.h>
  30. #else
  31. #include <time.h>
  32. #include <sys/time.h>
  33. #endif
  34. #include <stdint.h>
  35. #include <misc/debug.h>
  36. #include <misc/overflow.h>
  37. typedef int64_t btime_t;
  38. struct _BTime_global {
  39. #ifndef NDEBUG
  40. int initialized; // initialized statically
  41. #endif
  42. #ifdef BADVPN_USE_WINAPI
  43. LARGE_INTEGER start_time;
  44. #else
  45. btime_t start_time;
  46. int use_gettimeofday;
  47. #endif
  48. };
  49. extern struct _BTime_global btime_global;
  50. static void BTime_Init (void)
  51. {
  52. ASSERT(!btime_global.initialized)
  53. #ifdef BADVPN_USE_WINAPI
  54. ASSERT_FORCE(QueryPerformanceCounter(&btime_global.start_time))
  55. #else
  56. struct timespec ts;
  57. if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
  58. DEBUG("WARNING: CLOCK_MONOTONIC is not available. Timers will be confused by clock changes.");
  59. struct timeval tv;
  60. ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
  61. btime_global.start_time = (int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000;
  62. btime_global.use_gettimeofday = 1;
  63. } else {
  64. btime_global.start_time = (int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000;
  65. btime_global.use_gettimeofday = 0;
  66. }
  67. #endif
  68. #ifndef NDEBUG
  69. btime_global.initialized = 1;
  70. #endif
  71. }
  72. static btime_t btime_gettime ()
  73. {
  74. ASSERT(btime_global.initialized)
  75. #ifdef BADVPN_USE_WINAPI
  76. LARGE_INTEGER count;
  77. LARGE_INTEGER freq;
  78. ASSERT_FORCE(QueryPerformanceCounter(&count))
  79. ASSERT_FORCE(QueryPerformanceFrequency(&freq))
  80. return (((count.QuadPart - btime_global.start_time.QuadPart) * 1000) / freq.QuadPart);
  81. #else
  82. if (btime_global.use_gettimeofday) {
  83. struct timeval tv;
  84. ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
  85. return ((int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000);
  86. } else {
  87. struct timespec ts;
  88. ASSERT_FORCE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  89. return (((int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000) - btime_global.start_time);
  90. }
  91. #endif
  92. }
  93. static btime_t btime_add (btime_t t1, btime_t t2)
  94. {
  95. // handle overflow
  96. int overflows = add_int64_overflows(t1, t2);
  97. btime_t sum;
  98. if (overflows != 0) {
  99. if (overflows > 0) {
  100. sum = INT64_MAX;
  101. } else {
  102. sum = INT64_MIN;
  103. }
  104. } else {
  105. sum = t1 + t2;
  106. }
  107. return sum;
  108. }
  109. #endif