Fibers allocation/deallocation benchmarks
[folly.git] / folly / experimental / fibers / EventBaseLoopController.h
1 /*
2  * Copyright 2015 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 <memory>
19 #include <atomic>
20 #include <folly/experimental/fibers/LoopController.h>
21
22 namespace folly {
23 class EventBase;
24 }
25
26 namespace folly { namespace fibers {
27
28 class FiberManager;
29
30 class EventBaseLoopController : public LoopController {
31  public:
32   explicit EventBaseLoopController();
33   ~EventBaseLoopController();
34
35   /**
36    * Attach EventBase after LoopController was created.
37    */
38   void attachEventBase(folly::EventBase& eventBase);
39
40   folly::EventBase* getEventBase() {
41     return eventBase_;
42   }
43
44  private:
45   class ControllerCallback;
46
47   bool awaitingScheduling_{false};
48   folly::EventBase* eventBase_{nullptr};
49   std::unique_ptr<ControllerCallback> callback_;
50   FiberManager* fm_{nullptr};
51   std::atomic<bool> eventBaseAttached_{false};
52
53   /* LoopController interface */
54
55   void setFiberManager(FiberManager* fm) override;
56   void schedule() override;
57   void cancel() override;
58   void runLoop();
59   void scheduleThreadSafe() override;
60   void timedSchedule(std::function<void()> func, TimePoint time) override;
61
62   friend class FiberManager;
63 };
64
65 }}  // folly::fibers
66
67 #include "EventBaseLoopController-inl.h"