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.
 
 
 
 
 
 

165 lines
4.1 KiB

  1. // Copyright (c) Vitaliy Filippov, 2019+
  2. // License: VNPL-1.0 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.millis/1000;
  34. t.next.tv_nsec += (t.millis%1000)*1000000;
  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. int timer_id = id++;
  44. timespec start;
  45. clock_gettime(CLOCK_MONOTONIC, &start);
  46. timers.push_back({
  47. .id = timer_id,
  48. .millis = millis,
  49. .start = start,
  50. .next = start,
  51. .repeat = repeat,
  52. .callback = callback,
  53. });
  54. inc_timer(timers[timers.size()-1]);
  55. set_nearest();
  56. return timer_id;
  57. }
  58. void timerfd_manager_t::clear_timer(int timer_id)
  59. {
  60. for (int i = 0; i < timers.size(); i++)
  61. {
  62. if (timers[i].id == timer_id)
  63. {
  64. timers.erase(timers.begin()+i, timers.begin()+i+1);
  65. if (nearest == i)
  66. {
  67. nearest = -1;
  68. wait_state = wait_state & ~1;
  69. }
  70. else if (nearest > i)
  71. {
  72. nearest--;
  73. }
  74. set_nearest();
  75. break;
  76. }
  77. }
  78. }
  79. void timerfd_manager_t::set_nearest()
  80. {
  81. again:
  82. if (!timers.size())
  83. {
  84. nearest = -1;
  85. itimerspec exp = { 0 };
  86. if (timerfd_settime(timerfd, 0, &exp, NULL))
  87. {
  88. throw std::runtime_error(std::string("timerfd_settime: ") + strerror(errno));
  89. }
  90. wait_state = wait_state & ~1;
  91. }
  92. else
  93. {
  94. nearest = 0;
  95. for (int i = 1; i < timers.size(); i++)
  96. {
  97. if (timers[i].next.tv_sec < timers[nearest].next.tv_sec ||
  98. timers[i].next.tv_sec == timers[nearest].next.tv_sec &&
  99. timers[i].next.tv_nsec < timers[nearest].next.tv_nsec)
  100. {
  101. nearest = i;
  102. }
  103. }
  104. timespec now;
  105. clock_gettime(CLOCK_MONOTONIC, &now);
  106. itimerspec exp = {
  107. .it_interval = { 0 },
  108. .it_value = timers[nearest].next,
  109. };
  110. exp.it_value.tv_sec -= now.tv_sec;
  111. exp.it_value.tv_nsec -= now.tv_nsec;
  112. if (exp.it_value.tv_nsec < 0)
  113. {
  114. exp.it_value.tv_sec--;
  115. exp.it_value.tv_nsec += 1000000000;
  116. }
  117. if (exp.it_value.tv_sec < 0 || !exp.it_value.tv_sec && !exp.it_value.tv_nsec)
  118. {
  119. // It already happened
  120. trigger_nearest();
  121. goto again;
  122. }
  123. if (timerfd_settime(timerfd, 0, &exp, NULL))
  124. {
  125. throw std::runtime_error(std::string("timerfd_settime: ") + strerror(errno));
  126. }
  127. wait_state = wait_state | 1;
  128. }
  129. }
  130. void timerfd_manager_t::handle_readable()
  131. {
  132. uint64_t n;
  133. size_t res = read(timerfd, &n, 8);
  134. if (res == 8 && nearest >= 0)
  135. {
  136. trigger_nearest();
  137. }
  138. wait_state = 0;
  139. set_nearest();
  140. }
  141. void timerfd_manager_t::trigger_nearest()
  142. {
  143. int nearest_id = timers[nearest].id;
  144. auto cb = timers[nearest].callback;
  145. if (timers[nearest].repeat)
  146. {
  147. inc_timer(timers[nearest]);
  148. }
  149. else
  150. {
  151. timers.erase(timers.begin()+nearest, timers.begin()+nearest+1);
  152. }
  153. cb(nearest_id);
  154. nearest = -1;
  155. }