TimeoutQueue.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Simple timeout queue. Call user-specified callbacks when their timeouts
  18. * expire.
  19. *
  20. * This class assumes that "time" is an int64_t and doesn't care about time
  21. * units (seconds, milliseconds, etc). You call runOnce() / runLoop() using
  22. * the same time units that you use to specify callbacks.
  23. *
  24. * @author Tudor Bosman (tudorb@fb.com)
  25. */
  26. #pragma once
  27. #include <cstdint>
  28. #include <functional>
  29. #include <boost/multi_index/indexed_by.hpp>
  30. #include <boost/multi_index/member.hpp>
  31. #include <boost/multi_index/ordered_index.hpp>
  32. #include <boost/multi_index_container.hpp>
  33. namespace folly {
  34. class TimeoutQueue {
  35. public:
  36. typedef int64_t Id;
  37. typedef std::function<void(Id, int64_t)> Callback;
  38. TimeoutQueue() : nextId_(1) {}
  39. /**
  40. * Add a one-time timeout event that will fire "delay" time units from "now"
  41. * (that is, the first time that run*() is called with a time value >= now
  42. * + delay).
  43. */
  44. Id add(int64_t now, int64_t delay, Callback callback);
  45. /**
  46. * Add a repeating timeout event that will fire every "interval" time units
  47. * (it will first fire when run*() is called with a time value >=
  48. * now + interval).
  49. *
  50. * run*() will always invoke each repeating event at most once, even if
  51. * more than one "interval" period has passed.
  52. */
  53. Id addRepeating(int64_t now, int64_t interval, Callback callback);
  54. /**
  55. * Erase a given timeout event, returns true if the event was actually
  56. * erased and false if it didn't exist in our queue.
  57. */
  58. bool erase(Id id);
  59. /**
  60. * Process all events that are due at times <= "now" by calling their
  61. * callbacks.
  62. *
  63. * Callbacks are allowed to call back into the queue and add / erase events;
  64. * they might create more events that are already due. In this case,
  65. * runOnce() will only go through the queue once, and return a "next
  66. * expiration" time in the past or present (<= now); runLoop()
  67. * will process the queue again, until there are no events already due.
  68. *
  69. * Note that it is then possible for runLoop to never return if
  70. * callbacks re-add themselves to the queue (or if you have repeating
  71. * callbacks with an interval of 0).
  72. *
  73. * Return the time that the next event will be due (same as
  74. * nextExpiration(), below)
  75. */
  76. int64_t runOnce(int64_t now) {
  77. return runInternal(now, true);
  78. }
  79. int64_t runLoop(int64_t now) {
  80. return runInternal(now, false);
  81. }
  82. /**
  83. * Return the time that the next event will be due.
  84. */
  85. int64_t nextExpiration() const;
  86. private:
  87. int64_t runInternal(int64_t now, bool runOnce);
  88. // noncopyable
  89. TimeoutQueue(const TimeoutQueue&) = delete;
  90. TimeoutQueue& operator=(const TimeoutQueue&) = delete;
  91. struct Event {
  92. Id id;
  93. int64_t expiration;
  94. int64_t repeatInterval;
  95. Callback callback;
  96. };
  97. typedef boost::multi_index_container<
  98. Event,
  99. boost::multi_index::indexed_by<
  100. boost::multi_index::ordered_unique<
  101. boost::multi_index::member<Event, Id, &Event::id>>,
  102. boost::multi_index::ordered_non_unique<
  103. boost::multi_index::member<Event, int64_t, &Event::expiration>>>>
  104. Set;
  105. enum {
  106. BY_ID = 0,
  107. BY_EXPIRATION = 1,
  108. };
  109. Set timeouts_;
  110. Id nextId_;
  111. };
  112. } // namespace folly