2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <folly/fibers/LoopController.h>
19 #include <folly/io/async/EventBase.h>
32 class EventBaseLoopController : public LoopController {
34 explicit EventBaseLoopController();
35 ~EventBaseLoopController();
38 * Attach EventBase after LoopController was created.
40 void attachEventBase(folly::EventBase& eventBase);
42 folly::EventBase* getEventBase() {
47 class ControllerCallback : public folly::EventBase::LoopCallback {
49 explicit ControllerCallback(EventBaseLoopController& controller)
50 : controller_(controller) {}
52 void runLoopCallback() noexcept override {
53 controller_.runLoop();
57 EventBaseLoopController& controller_;
60 class DestructionCallback : public folly::EventBase::LoopCallback {
62 DestructionCallback() : alive_(new int(42)) {}
63 ~DestructionCallback() {
67 void runLoopCallback() noexcept override {
71 std::weak_ptr<void> getWeak() {
77 std::weak_ptr<void> aliveWeak(alive_);
80 while (!aliveWeak.expired()) {
81 // Spin until all operations requiring EventBaseLoopController to be
82 // alive are complete.
86 std::shared_ptr<void> alive_;
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_;
97 /* LoopController interface */
99 void setFiberManager(FiberManager* fm) override;
100 void schedule() override;
101 void cancel() override;
103 void scheduleThreadSafe(std::function<bool()> func) override;
104 void timedSchedule(std::function<void()> func, TimePoint time) override;
106 friend class FiberManager;
111 #include "EventBaseLoopController-inl.h"