2 * Copyright 2004-present 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.
22 #include <folly/Function.h>
29 * Base interface to be implemented by all classes expecting to manage
30 * timeouts. AsyncTimeout will use implementations of this interface
31 * to schedule/cancel timeouts.
33 class TimeoutManager {
35 typedef std::chrono::milliseconds timeout_type;
36 using Func = folly::Function<void()>;
38 enum class InternalEnum {
45 virtual ~TimeoutManager();
48 * Attaches/detaches TimeoutManager to AsyncTimeout
50 virtual void attachTimeoutManager(AsyncTimeout* obj,
51 InternalEnum internal) = 0;
52 virtual void detachTimeoutManager(AsyncTimeout* obj) = 0;
55 * Schedules AsyncTimeout to fire after `timeout` milliseconds
57 virtual bool scheduleTimeout(AsyncTimeout* obj,
58 timeout_type timeout) = 0;
61 * Cancels the AsyncTimeout, if scheduled
63 virtual void cancelTimeout(AsyncTimeout* obj) = 0;
66 * This is used to mark the beginning of a new loop cycle by the
67 * first handler fired within that cycle.
69 virtual void bumpHandlingTime() = 0;
72 * Helper method to know whether we are running in the timeout manager
75 virtual bool isInTimeoutManagerThread() = 0;
78 * Runs the given Cob at some time after the specified number of
79 * milliseconds. (No guarantees exactly when.)
81 * Throws a std::system_error if an error occurs.
85 uint32_t milliseconds,
86 InternalEnum internal = InternalEnum::NORMAL);
89 * @see tryRunAfterDelay for more details
91 * @return true iff the cob was successfully registered.
93 bool tryRunAfterDelay(
95 uint32_t milliseconds,
96 InternalEnum internal = InternalEnum::NORMAL);
99 void clearCobTimeouts();
103 std::unique_ptr<CobTimeouts> cobTimeouts_;