2 * Copyright 2016 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.
23 #include <boost/intrusive/list.hpp>
25 #include <folly/Likely.h>
27 #include <folly/fibers/LoopController.h>
32 class TimeoutController
33 : public std::enable_shared_from_this<TimeoutController> {
35 typedef std::chrono::steady_clock Clock;
36 typedef std::chrono::time_point<Clock> TimePoint;
37 typedef Clock::duration Duration;
39 explicit TimeoutController(LoopController& loopController);
41 intptr_t registerTimeout(std::function<void()> f, Duration duration);
42 void cancel(intptr_t id);
44 void runTimeouts(TimePoint time);
50 typedef std::queue<TimeoutHandle> TimeoutHandleList;
51 typedef std::unique_ptr<TimeoutHandleList> TimeoutHandleListPtr;
53 struct TimeoutHandle {
55 std::function<void()> func_,
57 TimeoutHandleList& list_)
58 : func(std::move(func_)), timeout(timeout_), list(list_) {}
60 std::function<void()> func;
63 TimeoutHandleList& list;
66 std::vector<std::pair<Duration, TimeoutHandleListPtr>> timeoutHandleBuckets_;
67 TimePoint nextTimeout_;
68 LoopController& loopController_;