A clone of btpd with my configuration changes.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <time.h>
  2. #include "evloop.h"
  3. #include "timeheap.h"
  4. #if defined(HAVE_CLOCK_MONOTONIC)
  5. #ifdef CLOCK_MONOTONIC_FAST
  6. #define TIMER_CLOCK CLOCK_MONOTONIC_FAST
  7. #else
  8. #define TIMER_CLOCK CLOCK_MONOTONIC
  9. #endif
  10. int
  11. evtimer_gettime(struct timespec *ts)
  12. {
  13. return clock_gettime(TIMER_CLOCK, ts);
  14. }
  15. #elif defined(HAVE_MACH_ABSOLUTE_TIME)
  16. #include <mach/mach_time.h>
  17. int
  18. evtimer_gettime(struct timespec *ts)
  19. {
  20. uint64_t nsecs;
  21. static double nsmul;
  22. static mach_timebase_info_data_t nsratio = { 0, 0 };
  23. if (nsratio.denom == 0) {
  24. mach_timebase_info(&nsratio);
  25. nsmul = (double)nsratio.numer / nsratio.denom;
  26. }
  27. nsecs = mach_absolute_time() * nsmul;
  28. ts->tv_sec = nsecs / 1000000000ULL;
  29. ts->tv_nsec = nsecs - ts->tv_sec * 1000000000ULL;
  30. return 0;
  31. }
  32. #else
  33. #error No supported time mechanism
  34. #endif
  35. static struct timespec
  36. addtime(struct timespec a, struct timespec b)
  37. {
  38. struct timespec ret;
  39. ret.tv_sec = a.tv_sec + b.tv_sec;
  40. ret.tv_nsec = a.tv_nsec + b.tv_nsec;
  41. if (ret.tv_nsec >= 1000000000) {
  42. ret.tv_sec += 1;
  43. ret.tv_nsec -= 1000000000;
  44. }
  45. return ret;
  46. }
  47. static struct timespec
  48. subtime(struct timespec a, struct timespec b)
  49. {
  50. struct timespec ret;
  51. ret.tv_sec = a.tv_sec - b.tv_sec;
  52. ret.tv_nsec = a.tv_nsec - b.tv_nsec;
  53. if (ret.tv_nsec < 0) {
  54. ret.tv_sec -= 1;
  55. ret.tv_nsec += 1000000000;
  56. }
  57. return ret;
  58. }
  59. void
  60. evtimer_init(struct timeout *h, evloop_cb_t cb, void *arg)
  61. {
  62. h->cb = cb;
  63. h->arg = arg;
  64. h->th.i = -1;
  65. h->th.data = h;
  66. }
  67. int
  68. evtimer_add(struct timeout *h, struct timespec *t)
  69. {
  70. struct timespec now, sum;
  71. evtimer_gettime(&now);
  72. sum = addtime(now, *t);
  73. if (h->th.i == -1)
  74. return timeheap_insert(&h->th, &sum);
  75. else {
  76. timeheap_change(&h->th, &sum);
  77. return 0;
  78. }
  79. }
  80. void
  81. evtimer_del(struct timeout *h)
  82. {
  83. if (h->th.i >= 0) {
  84. timeheap_remove(&h->th);
  85. h->th.i = -1;
  86. }
  87. }
  88. void
  89. evtimers_run(void)
  90. {
  91. struct timespec now;
  92. evtimer_gettime(&now);
  93. while (timeheap_size() > 0) {
  94. struct timespec diff = subtime(timeheap_top(), now);
  95. if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec < 1000000)) {
  96. struct timeout *t = timeheap_remove_top();
  97. t->th.i = -1;
  98. t->cb(-1, EV_TIMEOUT, t->arg);
  99. } else
  100. break;
  101. }
  102. }
  103. struct timespec
  104. evtimer_delay(void)
  105. {
  106. struct timespec now, diff;
  107. if (timeheap_size() == 0) {
  108. diff.tv_sec = -1;
  109. diff.tv_nsec = 0;
  110. } else {
  111. evtimer_gettime(&now);
  112. diff = subtime(timeheap_top(), now);
  113. if (diff.tv_sec < 0) {
  114. diff.tv_sec = 0;
  115. diff.tv_nsec = 0;
  116. }
  117. }
  118. return diff;
  119. }