BTime.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #endif
  33. #include <stdint.h>
  34. #include <misc/debug.h>
  35. #include <misc/overflow.h>
  36. typedef int64_t btime_t;
  37. struct _BTime_global {
  38. #ifndef NDEBUG
  39. int initialized; // initialized statically
  40. #endif
  41. #ifdef BADVPN_USE_WINAPI
  42. LARGE_INTEGER start_time;
  43. #else
  44. btime_t start_time;
  45. #endif
  46. };
  47. extern struct _BTime_global btime_global;
  48. static void BTime_Init (void)
  49. {
  50. ASSERT(!btime_global.initialized)
  51. #ifdef BADVPN_USE_WINAPI
  52. ASSERT_FORCE(QueryPerformanceCounter(&btime_global.start_time))
  53. #else
  54. struct timespec ts;
  55. ASSERT_FORCE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  56. btime_global.start_time = (int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000;
  57. #endif
  58. #ifndef NDEBUG
  59. btime_global.initialized = 1;
  60. #endif
  61. }
  62. static btime_t btime_gettime ()
  63. {
  64. ASSERT(btime_global.initialized)
  65. #ifdef BADVPN_USE_WINAPI
  66. LARGE_INTEGER count;
  67. LARGE_INTEGER freq;
  68. ASSERT_FORCE(QueryPerformanceCounter(&count))
  69. ASSERT_FORCE(QueryPerformanceFrequency(&freq))
  70. return (((count.QuadPart - btime_global.start_time.QuadPart) * 1000) / freq.QuadPart);
  71. #else
  72. struct timespec ts;
  73. ASSERT_FORCE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  74. return (((int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000) - btime_global.start_time);
  75. #endif
  76. }
  77. static btime_t btime_add (btime_t t1, btime_t t2)
  78. {
  79. // handle overflow
  80. int overflows = add_int64_overflows(t1, t2);
  81. btime_t sum;
  82. if (overflows != 0) {
  83. if (overflows > 0) {
  84. sum = INT64_MAX;
  85. } else {
  86. sum = INT64_MIN;
  87. }
  88. } else {
  89. sum = t1 + t2;
  90. }
  91. return sum;
  92. }
  93. #endif