c9109c993f52d6ae4ddadf01458b3ff7c1e9ba08
[folly.git] / folly / fibers / EventBaseLoopController-inl.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 #include <folly/Memory.h>
17 #include <folly/fibers/EventBaseLoopController.h>
18 #include <folly/fibers/FiberManager.h>
19
20 namespace folly {
21 namespace fibers {
22
23 inline EventBaseLoopController::EventBaseLoopController()
24     : callback_(*this), aliveWeak_(destructionCallback_.getWeak()) {}
25
26 inline EventBaseLoopController::~EventBaseLoopController() {
27   callback_.cancelLoopCallback();
28 }
29
30 inline void EventBaseLoopController::attachEventBase(
31     folly::EventBase& eventBase) {
32   if (eventBase_ != nullptr) {
33     LOG(ERROR) << "Attempt to reattach EventBase to LoopController";
34   }
35
36   eventBase_ = &eventBase;
37   eventBase_->runOnDestruction(&destructionCallback_);
38
39   eventBaseAttached_ = true;
40
41   if (awaitingScheduling_) {
42     schedule();
43   }
44 }
45
46 inline void EventBaseLoopController::setFiberManager(FiberManager* fm) {
47   fm_ = fm;
48 }
49
50 inline void EventBaseLoopController::schedule() {
51   if (eventBase_ == nullptr) {
52     // In this case we need to postpone scheduling.
53     awaitingScheduling_ = true;
54   } else {
55     // Schedule it to run in current iteration.
56     eventBase_->runInLoop(&callback_, true);
57     awaitingScheduling_ = false;
58   }
59 }
60
61 inline void EventBaseLoopController::cancel() {
62   callback_.cancelLoopCallback();
63 }
64
65 inline void EventBaseLoopController::runLoop() {
66   fm_->loopUntilNoReady();
67 }
68
69 inline void EventBaseLoopController::scheduleThreadSafe(
70     std::function<bool()> func) {
71   /* The only way we could end up here is if
72      1) Fiber thread creates a fiber that awaits (which means we must
73         have already attached, fiber thread wouldn't be running).
74      2) We move the promise to another thread (this move is a memory fence)
75      3) We fulfill the promise from the other thread. */
76   assert(eventBaseAttached_);
77
78   auto alive = aliveWeak_.lock();
79
80   if (func() && alive) {
81     auto aliveWeak = aliveWeak_;
82     eventBase_->runInEventBaseThread([this, aliveWeak]() {
83       if (!aliveWeak.expired()) {
84         runLoop();
85       }
86     });
87   }
88 }
89
90 inline void EventBaseLoopController::timedSchedule(
91     std::function<void()> func,
92     TimePoint time) {
93   assert(eventBaseAttached_);
94
95   // We want upper bound for the cast, thus we just add 1
96   auto delay_ms =
97       std::chrono::duration_cast<std::chrono::milliseconds>(time - Clock::now())
98           .count() +
99       1;
100   // If clock is not monotonic
101   delay_ms = std::max<decltype(delay_ms)>(delay_ms, 0L);
102   eventBase_->tryRunAfterDelay(func, delay_ms);
103 }
104 }
105 } // folly::fibers