53093d02a74ccd197d69040266b93119e75204ce
[folly.git] / folly / io / async / TimeoutManager.h
1 /*
2  * Copyright 2016 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 #include <folly/Function.h>
27
28 namespace folly {
29
30 class AsyncTimeout;
31
32 /**
33  * Base interface to be implemented by all classes expecting to manage
34  * timeouts. AsyncTimeout will use implementations of this interface
35  * to schedule/cancel timeouts.
36  */
37 class TimeoutManager {
38  public:
39   typedef std::chrono::milliseconds timeout_type;
40   using Func = folly::Function<void()>;
41
42   enum class InternalEnum {
43     INTERNAL,
44     NORMAL
45   };
46
47   TimeoutManager();
48
49   virtual ~TimeoutManager();
50
51   /**
52    * Attaches/detaches TimeoutManager to AsyncTimeout
53    */
54   virtual void attachTimeoutManager(AsyncTimeout* obj,
55                                     InternalEnum internal) = 0;
56   virtual void detachTimeoutManager(AsyncTimeout* obj) = 0;
57
58   /**
59    * Schedules AsyncTimeout to fire after `timeout` milliseconds
60    */
61   virtual bool scheduleTimeout(AsyncTimeout* obj,
62                                timeout_type timeout) = 0;
63
64   /**
65    * Cancels the AsyncTimeout, if scheduled
66    */
67   virtual void cancelTimeout(AsyncTimeout* obj) = 0;
68
69   /**
70    * This is used to mark the beginning of a new loop cycle by the
71    * first handler fired within that cycle.
72    */
73   virtual void bumpHandlingTime() = 0;
74
75   /**
76    * Helper method to know whether we are running in the timeout manager
77    * thread
78    */
79   virtual bool isInTimeoutManagerThread() = 0;
80
81   /**
82    * Runs the given Cob at some time after the specified number of
83    * milliseconds.  (No guarantees exactly when.)
84    *
85    * Throws a std::system_error if an error occurs.
86    */
87   void runAfterDelay(
88       Func cob,
89       uint32_t milliseconds,
90       InternalEnum internal = InternalEnum::NORMAL);
91
92   /**
93    * @see tryRunAfterDelay for more details
94    *
95    * @return  true iff the cob was successfully registered.
96    */
97   bool tryRunAfterDelay(
98       Func cob,
99       uint32_t milliseconds,
100       InternalEnum internal = InternalEnum::NORMAL);
101
102  protected:
103   void clearCobTimeouts();
104
105  private:
106   struct CobTimeouts;
107   std::unique_ptr<CobTimeouts> cobTimeouts_;
108 };
109
110 } // folly