Move fibers out of experimental
[folly.git] / folly / fibers / EventBaseLoopController.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 <folly/fibers/LoopController.h>
19 #include <folly/io/async/EventBase.h>
20 #include <atomic>
21 #include <memory>
22
23 namespace folly {
24 class EventBase;
25 }
26
27 namespace folly {
28 namespace fibers {
29
30 class FiberManager;
31
32 class EventBaseLoopController : public LoopController {
33  public:
34   explicit EventBaseLoopController();
35   ~EventBaseLoopController();
36
37   /**
38    * Attach EventBase after LoopController was created.
39    */
40   void attachEventBase(folly::EventBase& eventBase);
41
42   folly::EventBase* getEventBase() {
43     return eventBase_;
44   }
45
46  private:
47   class ControllerCallback : public folly::EventBase::LoopCallback {
48    public:
49     explicit ControllerCallback(EventBaseLoopController& controller)
50         : controller_(controller) {}
51
52     void runLoopCallback() noexcept override {
53       controller_.runLoop();
54     }
55
56    private:
57     EventBaseLoopController& controller_;
58   };
59
60   class DestructionCallback : public folly::EventBase::LoopCallback {
61    public:
62     DestructionCallback() : alive_(new int(42)) {}
63     ~DestructionCallback() {
64       reset();
65     }
66
67     void runLoopCallback() noexcept override {
68       reset();
69     }
70
71     std::weak_ptr<void> getWeak() {
72       return {alive_};
73     }
74
75    private:
76     void reset() {
77       std::weak_ptr<void> aliveWeak(alive_);
78       alive_.reset();
79
80       while (!aliveWeak.expired()) {
81         // Spin until all operations requiring EventBaseLoopController to be
82         // alive are complete.
83       }
84     }
85
86     std::shared_ptr<void> alive_;
87   };
88
89   bool awaitingScheduling_{false};
90   folly::EventBase* eventBase_{nullptr};
91   ControllerCallback callback_;
92   DestructionCallback destructionCallback_;
93   FiberManager* fm_{nullptr};
94   std::atomic<bool> eventBaseAttached_{false};
95   std::weak_ptr<void> aliveWeak_;
96
97   /* LoopController interface */
98
99   void setFiberManager(FiberManager* fm) override;
100   void schedule() override;
101   void cancel() override;
102   void runLoop();
103   void scheduleThreadSafe(std::function<bool()> func) override;
104   void timedSchedule(std::function<void()> func, TimePoint time) override;
105
106   friend class FiberManager;
107 };
108 }
109 } // folly::fibers
110
111 #include "EventBaseLoopController-inl.h"