2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Simple timeout queue. Call user-specified callbacks when their timeouts
21 * This class assumes that "time" is an int64_t and doesn't care about time
22 * units (seconds, milliseconds, etc). You call runOnce() / runLoop() using
23 * the same time units that you use to specify callbacks.
25 * @author Tudor Bosman (tudorb@fb.com)
32 #include <boost/multi_index_container.hpp>
33 #include <boost/multi_index/indexed_by.hpp>
34 #include <boost/multi_index/ordered_index.hpp>
35 #include <boost/multi_index/member.hpp>
42 typedef std::function<void(Id, int64_t)> Callback;
44 TimeoutQueue() : nextId_(1) { }
47 * Add a one-time timeout event that will fire "delay" time units from "now"
48 * (that is, the first time that run*() is called with a time value >= now
51 Id add(int64_t now, int64_t delay, Callback callback);
54 * Add a repeating timeout event that will fire every "interval" time units
55 * (it will first fire when run*() is called with a time value >=
58 * run*() will always invoke each repeating event at most once, even if
59 * more than one "interval" period has passed.
61 Id addRepeating(int64_t now, int64_t interval, Callback callback);
64 * Erase a given timeout event, returns true if the event was actually
65 * erased and false if it didn't exist in our queue.
70 * Process all events that are due at times <= "now" by calling their
73 * Callbacks are allowed to call back into the queue and add / erase events;
74 * they might create more events that are already due. In this case,
75 * runOnce() will only go through the queue once, and return a "next
76 * expiration" time in the past or present (<= now); runLoop()
77 * will process the queue again, until there are no events already due.
79 * Note that it is then possible for runLoop to never return if
80 * callbacks re-add themselves to the queue (or if you have repeating
81 * callbacks with an interval of 0).
83 * Return the time that the next event will be due (same as
84 * nextExpiration(), below)
86 int64_t runOnce(int64_t now) { return runInternal(now, true); }
87 int64_t runLoop(int64_t now) { return runInternal(now, false); }
90 * Return the time that the next event will be due.
92 int64_t nextExpiration() const;
95 int64_t runInternal(int64_t now, bool runOnce);
97 TimeoutQueue(const TimeoutQueue&) = delete;
98 TimeoutQueue& operator=(const TimeoutQueue&) = delete;
103 int64_t repeatInterval;
107 typedef boost::multi_index_container<
109 boost::multi_index::indexed_by<
110 boost::multi_index::ordered_unique<boost::multi_index::member<
111 Event, Id, &Event::id
113 boost::multi_index::ordered_non_unique<boost::multi_index::member<
114 Event, int64_t, &Event::expiration