Consistency in namespace-closing comments
[folly.git] / folly / io / async / TimeoutManager.h
1 /*
2  * Copyright 2004-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <chrono>
20 #include <cstdint>
21
22 #include <folly/Function.h>
23
24 namespace folly {
25
26 class AsyncTimeout;
27
28 /**
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.
32  */
33 class TimeoutManager {
34  public:
35   typedef std::chrono::milliseconds timeout_type;
36   using Func = folly::Function<void()>;
37
38   enum class InternalEnum {
39     INTERNAL,
40     NORMAL
41   };
42
43   TimeoutManager();
44
45   virtual ~TimeoutManager();
46
47   /**
48    * Attaches/detaches TimeoutManager to AsyncTimeout
49    */
50   virtual void attachTimeoutManager(AsyncTimeout* obj,
51                                     InternalEnum internal) = 0;
52   virtual void detachTimeoutManager(AsyncTimeout* obj) = 0;
53
54   /**
55    * Schedules AsyncTimeout to fire after `timeout` milliseconds
56    */
57   virtual bool scheduleTimeout(AsyncTimeout* obj,
58                                timeout_type timeout) = 0;
59
60   /**
61    * Cancels the AsyncTimeout, if scheduled
62    */
63   virtual void cancelTimeout(AsyncTimeout* obj) = 0;
64
65   /**
66    * This is used to mark the beginning of a new loop cycle by the
67    * first handler fired within that cycle.
68    */
69   virtual void bumpHandlingTime() = 0;
70
71   /**
72    * Helper method to know whether we are running in the timeout manager
73    * thread
74    */
75   virtual bool isInTimeoutManagerThread() = 0;
76
77   /**
78    * Runs the given Cob at some time after the specified number of
79    * milliseconds.  (No guarantees exactly when.)
80    *
81    * Throws a std::system_error if an error occurs.
82    */
83   void runAfterDelay(
84       Func cob,
85       uint32_t milliseconds,
86       InternalEnum internal = InternalEnum::NORMAL);
87
88   /**
89    * @see tryRunAfterDelay for more details
90    *
91    * @return  true iff the cob was successfully registered.
92    */
93   bool tryRunAfterDelay(
94       Func cob,
95       uint32_t milliseconds,
96       InternalEnum internal = InternalEnum::NORMAL);
97
98  protected:
99   void clearCobTimeouts();
100
101  private:
102   struct CobTimeouts;
103   std::unique_ptr<CobTimeouts> cobTimeouts_;
104 };
105
106 } // namespace folly