1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 <functional>
21 
22 #include <folly/io/async/HHWheelTimer-fwd.h>
23 
24 namespace folly {
25 namespace fibers {
26 
27 class Fiber;
28 class FiberManager;
29 
30 class LoopController {
31  public:
32   typedef std::chrono::steady_clock Clock;
33   typedef std::chrono::time_point<Clock> TimePoint;
34 
~LoopController()35   virtual ~LoopController() {}
36 
37   /**
38    * Called by FiberManager to associate itself with the LoopController.
39    */
40   virtual void setFiberManager(FiberManager*) = 0;
41 
42   /**
43    * Called by FiberManager to schedule the loop function run
44    * at some point in the future.
45    */
46   virtual void schedule() = 0;
47 
48   /**
49    * Run FiberManager loopUntilNoReadyImpl(). May have additional logic specific
50    * to a LoopController.
51    */
52   virtual void runLoop() = 0;
53 
54   /**
55    * Run FiberManager runEagerFiberImpl(fiber). May have additional logic
56    * specific to a LoopController.
57    */
58   virtual void runEagerFiber(Fiber*) = 0;
59 
60   /**
61    * Same as schedule(), but safe to call from any thread.
62    */
63   virtual void scheduleThreadSafe() = 0;
64 
65   /**
66    * Used by FiberManager to schedule some function to be run at some time.
67    * May return null, but only if called outside of runLoop() call (e.g. if
68    * Executor backing the timer is already destroyed).
69    */
70   virtual HHWheelTimer* timer() = 0;
71 };
72 } // namespace fibers
73 } // namespace folly
74