2 * Copyright 2014 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.
17 #include <folly/TimeoutQueue.h>
23 TimeoutQueue::Id TimeoutQueue::add(
28 timeouts_.insert({id, now + delay, -1, std::move(callback)});
32 TimeoutQueue::Id TimeoutQueue::addRepeating(
37 timeouts_.insert({id, now + interval, interval, std::move(callback)});
41 int64_t TimeoutQueue::nextExpiration() const {
42 return (timeouts_.empty() ? std::numeric_limits<int64_t>::max() :
43 timeouts_.get<BY_EXPIRATION>().begin()->expiration);
46 bool TimeoutQueue::erase(Id id) {
47 return timeouts_.get<BY_ID>().erase(id);
50 int64_t TimeoutQueue::runInternal(int64_t now, bool onceOnly) {
51 auto& byExpiration = timeouts_.get<BY_EXPIRATION>();
54 auto end = byExpiration.upper_bound(now);
55 std::vector<Event> expired;
56 std::move(byExpiration.begin(), end, std::back_inserter(expired));
57 byExpiration.erase(byExpiration.begin(), end);
58 for (auto& event : expired) {
59 // Reinsert if repeating, do this before executing callbacks
60 // so the callbacks have a chance to call erase
61 if (event.repeatInterval >= 0) {
62 timeouts_.insert({event.id, now + event.repeatInterval,
63 event.repeatInterval, event.callback});
68 for (auto& event : expired) {
69 event.callback(event.id, now);
71 nextExp = nextExpiration();
72 } while (!onceOnly && nextExp <= now);