Simplified distributed block storage with strong consistency, like in Ceph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
4.2 KiB

  1. // Copyright (c) Vitaliy Filippov, 2019+
  2. // License: VNPL-1.1 or GNU GPL-2.0+ (see README.md for details)
  3. #include <sys/timerfd.h>
  4. #include <sys/poll.h>
  5. #include <sys/epoll.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <string>
  10. #include <stdexcept>
  11. #include "timerfd_manager.h"
  12. timerfd_manager_t::timerfd_manager_t(std::function<void(int, bool, std::function<void(int, int)>)> set_fd_handler)
  13. {
  14. this->set_fd_handler = set_fd_handler;
  15. wait_state = 0;
  16. timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
  17. if (timerfd < 0)
  18. {
  19. throw std::runtime_error(std::string("timerfd_create: ") + strerror(errno));
  20. }
  21. set_fd_handler(timerfd, false, [this](int fd, int events)
  22. {
  23. handle_readable();
  24. });
  25. }
  26. timerfd_manager_t::~timerfd_manager_t()
  27. {
  28. set_fd_handler(timerfd, false, NULL);
  29. close(timerfd);
  30. }
  31. void timerfd_manager_t::inc_timer(timerfd_timer_t & t)
  32. {
  33. t.next.tv_sec += t.micros/1000000;
  34. t.next.tv_nsec += (t.micros%1000000)*1000;
  35. if (t.next.tv_nsec > 1000000000)
  36. {
  37. t.next.tv_sec++;
  38. t.next.tv_nsec -= 1000000000;
  39. }
  40. }
  41. int timerfd_manager_t::set_timer(uint64_t millis, bool repeat, std::function<void(int)> callback)
  42. {
  43. return set_timer_us(millis*1000, repeat, callback);
  44. }
  45. int timerfd_manager_t::set_timer_us(uint64_t micros, bool repeat, std::function<void(int)> callback)
  46. {
  47. int timer_id = id++;
  48. timespec start;
  49. clock_gettime(CLOCK_MONOTONIC, &start);
  50. timers.push_back({
  51. .id = timer_id,
  52. .micros = micros,
  53. .start = start,
  54. .next = start,
  55. .repeat = repeat,
  56. .callback = callback,
  57. });
  58. inc_timer(timers[timers.size()-1]);
  59. set_nearest();
  60. return timer_id;
  61. }
  62. void timerfd_manager_t::clear_timer(int timer_id)
  63. {
  64. for (int i = 0; i < timers.size(); i++)
  65. {
  66. if (timers[i].id == timer_id)
  67. {
  68. timers.erase(timers.begin()+i, timers.begin()+i+1);
  69. if (nearest == i)
  70. {
  71. nearest = -1;
  72. wait_state = wait_state & ~1;
  73. }
  74. else if (nearest > i)
  75. {
  76. nearest--;
  77. }
  78. set_nearest();
  79. break;
  80. }
  81. }
  82. }
  83. void timerfd_manager_t::set_nearest()
  84. {
  85. again:
  86. if (!timers.size())
  87. {
  88. nearest = -1;
  89. itimerspec exp = { 0 };
  90. if (timerfd_settime(timerfd, 0, &exp, NULL))
  91. {
  92. throw std::runtime_error(std::string("timerfd_settime: ") + strerror(errno));
  93. }
  94. wait_state = wait_state & ~1;
  95. }
  96. else
  97. {
  98. nearest = 0;
  99. for (int i = 1; i < timers.size(); i++)
  100. {
  101. if (timers[i].next.tv_sec < timers[nearest].next.tv_sec ||
  102. timers[i].next.tv_sec == timers[nearest].next.tv_sec &&
  103. timers[i].next.tv_nsec < timers[nearest].next.tv_nsec)
  104. {
  105. nearest = i;
  106. }
  107. }
  108. timespec now;
  109. clock_gettime(CLOCK_MONOTONIC, &now);
  110. itimerspec exp = {
  111. .it_interval = { 0 },
  112. .it_value = timers[nearest].next,
  113. };
  114. exp.it_value.tv_sec -= now.tv_sec;
  115. exp.it_value.tv_nsec -= now.tv_nsec;
  116. if (exp.it_value.tv_nsec < 0)
  117. {
  118. exp.it_value.tv_sec--;
  119. exp.it_value.tv_nsec += 1000000000;
  120. }
  121. if (exp.it_value.tv_sec < 0 || exp.it_value.tv_sec == 0 && exp.it_value.tv_nsec <= 0)
  122. {
  123. // It already happened
  124. trigger_nearest();
  125. goto again;
  126. }
  127. if (timerfd_settime(timerfd, 0, &exp, NULL))
  128. {
  129. throw std::runtime_error(std::string("timerfd_settime: ") + strerror(errno));
  130. }
  131. wait_state = wait_state | 1;
  132. }
  133. }
  134. void timerfd_manager_t::handle_readable()
  135. {
  136. uint64_t n;
  137. size_t res = read(timerfd, &n, 8);
  138. if (res == 8 && nearest >= 0)
  139. {
  140. trigger_nearest();
  141. }
  142. wait_state = 0;
  143. set_nearest();
  144. }
  145. void timerfd_manager_t::trigger_nearest()
  146. {
  147. int nearest_id = timers[nearest].id;
  148. auto cb = timers[nearest].callback;
  149. if (timers[nearest].repeat)
  150. {
  151. inc_timer(timers[nearest]);
  152. }
  153. else
  154. {
  155. timers.erase(timers.begin()+nearest, timers.begin()+nearest+1);
  156. }
  157. nearest = -1;
  158. cb(nearest_id);
  159. }