fb3a1b0e74dd6c5acf7e409269b8090d6b484f0f
[folly.git] / folly / io / async / TimeoutManager.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements. See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership. The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License. You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied. See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21 #pragma once
22
23 #include <chrono>
24 #include <stdint.h>
25
26 namespace folly {
27
28 class AsyncTimeout;
29
30 /**
31  * Base interface to be implemented by all classes expecting to manage
32  * timeouts. AsyncTimeout will use implementations of this interface
33  * to schedule/cancel timeouts.
34  */
35 class TimeoutManager {
36  public:
37   enum class InternalEnum {
38     INTERNAL,
39     NORMAL
40   };
41
42   virtual ~TimeoutManager() {}
43
44   /**
45    * Attaches/detaches TimeoutManager to AsyncTimeout
46    */
47   virtual void attachTimeoutManager(AsyncTimeout* obj,
48                                     InternalEnum internal) = 0;
49   virtual void detachTimeoutManager(AsyncTimeout* obj) = 0;
50
51   /**
52    * Schedules AsyncTimeout to fire after `timeout` milliseconds
53    */
54   virtual bool scheduleTimeout(AsyncTimeout* obj,
55                                std::chrono::milliseconds timeout) = 0;
56
57   /**
58    * Cancels the AsyncTimeout, if scheduled
59    */
60   virtual void cancelTimeout(AsyncTimeout* obj) = 0;
61
62   /**
63    * This is used to mark the beginning of a new loop cycle by the
64    * first handler fired within that cycle.
65    */
66   virtual bool bumpHandlingTime() = 0;
67
68   /**
69    * Helper method to know whether we are running in the timeout manager
70    * thread
71    */
72   virtual bool isInTimeoutManagerThread() = 0;
73 };
74
75 } // folly