proxygen
SimpleLoopController.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present 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 <atomic>
19 
20 #include <folly/Function.h>
21 #include <folly/Likely.h>
22 
25 
26 namespace folly {
27 namespace fibers {
28 
29 class FiberManager;
30 
32  public:
34 
36  scheduled_ = false;
37  }
38 
39  void setTimeFunc(Function<TimePoint()> timeFunc) {
40  timeFunc_ = std::move(timeFunc);
41  }
42 
48  template <typename F>
49  void loop(F&& func) {
50  bool waiting = false;
51  stopRequested_ = false;
52 
53  while (LIKELY(waiting || !stopRequested_)) {
54  func();
55 
56  auto time = timeFunc_();
57 
58  for (size_t i = 0; i < scheduledFuncs_.size(); ++i) {
59  if (scheduledFuncs_[i].first <= time) {
60  scheduledFuncs_[i].second();
62  scheduledFuncs_.pop_back();
63  --i;
64  }
65  }
66 
67  if (scheduled_) {
68  scheduled_ = false;
69  runLoop();
70  waiting = fm_->hasTasks();
71  }
72  }
73  }
74 
78  void stop() {
79  stopRequested_ = true;
80  }
81 
82  int remoteScheduleCalled() const {
83  return remoteScheduleCalled_;
84  }
85 
86  void runLoop() override {
87  do {
90  if (fm_->shouldRunLoopRemote()) {
92  }
93  }
94  } else {
96  }
98  }
99 
100  void schedule() override {
101  scheduled_ = true;
102  }
103 
104  void timedSchedule(std::function<void()> func, TimePoint time) override {
105  scheduledFuncs_.emplace_back(time, std::move(func));
106  }
107 
108  private:
110  std::atomic<bool> scheduled_{false};
112  std::atomic<int> remoteScheduleCalled_{0};
114  std::vector<std::pair<TimePoint, std::function<void()>>> scheduledFuncs_;
116 
117  /* LoopController interface */
118 
119  void setFiberManager(FiberManager* fm) override {
120  fm_ = fm;
121  }
122 
123  void scheduleThreadSafe() override {
125  scheduled_ = true;
126  }
127 
128  friend class FiberManager;
129 };
130 } // namespace fibers
131 } // namespace folly
void setTimeFunc(Function< TimePoint()> timeFunc)
std::vector< std::pair< TimePoint, std::function< void()> > > scheduledFuncs_
std::chrono::steady_clock::time_point now()
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define LIKELY(x)
Definition: Likely.h:47
void setFiberManager(FiberManager *fm) override
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define nullptr
Definition: http_parser.c:41
std::chrono::time_point< Clock > TimePoint
Single-threaded task execution engine.
void swap(exception_wrapper &a, exception_wrapper &b) noexcept
void timedSchedule(std::function< void()> func, TimePoint time) override
std::chrono::nanoseconds time()
constexpr detail::First first
Definition: Base-inl.h:2553