BTime.h 3.4 KB

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