folly copyright 2015 -> copyright 2016
[folly.git] / folly / experimental / fibers / LoopController.h
1 /*
2  * Copyright 2016 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 #pragma once
17
18 #include <chrono>
19 #include <functional>
20
21 namespace folly { namespace fibers {
22
23 class FiberManager;
24
25 class LoopController {
26  public:
27   typedef std::chrono::steady_clock Clock;
28   typedef std::chrono::time_point<Clock> TimePoint;
29
30   virtual ~LoopController() {}
31
32   /**
33    * Called by FiberManager to associate itself with the LoopController.
34    */
35   virtual void setFiberManager(FiberManager*) = 0;
36
37   /**
38    * Called by FiberManager to schedule the loop function run
39    * at some point in the future.
40    */
41   virtual void schedule() = 0;
42
43   /**
44    * Same as schedule(), but safe to call from any thread.
45    * Runs func and only schedules if func returned true.
46    */
47   virtual void scheduleThreadSafe(std::function<bool()> func) = 0;
48
49   /**
50    * Called by FiberManager to cancel a previously scheduled
51    * loop function run.
52    */
53   virtual void cancel() = 0;
54
55   /**
56    * Called by FiberManager to schedule some function to be run at some time.
57    */
58   virtual void timedSchedule(std::function<void()> func, TimePoint time) = 0;
59 };
60
61 }}  // folly::fibers