proxygen
folly::fibers::SimpleLoopController Class Reference

#include <SimpleLoopController.h>

Inheritance diagram for folly::fibers::SimpleLoopController:
folly::fibers::LoopController

Public Member Functions

 SimpleLoopController ()
 
 ~SimpleLoopController ()
 
void setTimeFunc (Function< TimePoint()> timeFunc)
 
template<typename F >
void loop (F &&func)
 
void stop ()
 
int remoteScheduleCalled () const
 
void runLoop () override
 
void schedule () override
 
void timedSchedule (std::function< void()> func, TimePoint time) override
 
- Public Member Functions inherited from folly::fibers::LoopController
virtual ~LoopController ()
 
virtual void timedSchedule (std::function< void()> func, TimePoint time)=0
 

Private Member Functions

void setFiberManager (FiberManager *fm) override
 
void scheduleThreadSafe () override
 

Private Attributes

FiberManagerfm_
 
std::atomic< bool > scheduled_ {false}
 
bool stopRequested_
 
std::atomic< int > remoteScheduleCalled_ {0}
 
int remoteLoopRun_ {0}
 
std::vector< std::pair< TimePoint, std::function< void()> > > scheduledFuncs_
 
Function< TimePoint()> timeFunc_ {[] { return Clock::now(); }}
 

Friends

class FiberManager
 

Additional Inherited Members

- Public Types inherited from folly::fibers::LoopController
typedef std::chrono::steady_clock Clock
 
typedef std::chrono::time_point< ClockTimePoint
 

Detailed Description

Definition at line 31 of file SimpleLoopController.h.

Constructor & Destructor Documentation

folly::fibers::SimpleLoopController::SimpleLoopController ( )
inline
folly::fibers::SimpleLoopController::~SimpleLoopController ( )
inline

Definition at line 35 of file SimpleLoopController.h.

References scheduled_.

35  {
36  scheduled_ = false;
37  }

Member Function Documentation

template<typename F >
void folly::fibers::SimpleLoopController::loop ( F &&  func)
inline

Run FiberManager loop; if no ready task are present, run provided function. Stops after both stop() has been called and no waiting tasks remain.

Definition at line 49 of file SimpleLoopController.h.

References folly::gen::first, fm_, folly::fibers::FiberManager::hasTasks(), i, LIKELY, runLoop(), scheduled_, scheduledFuncs_, stopRequested_, folly::swap(), folly::detail::distributed_mutex::time(), and timeFunc_.

Referenced by main().

49  {
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  }
std::vector< std::pair< TimePoint, std::function< void()> > > scheduledFuncs_
#define LIKELY(x)
Definition: Likely.h:47
void swap(exception_wrapper &a, exception_wrapper &b) noexcept
std::chrono::nanoseconds time()
constexpr detail::First first
Definition: Base-inl.h:2553
int folly::fibers::SimpleLoopController::remoteScheduleCalled ( ) const
inline

Definition at line 82 of file SimpleLoopController.h.

References remoteScheduleCalled_.

82  {
83  return remoteScheduleCalled_;
84  }
void folly::fibers::SimpleLoopController::runLoop ( )
inlineoverridevirtual

Run FiberManager loopUntilNoReadyImpl(). May have additional logic specific to a LoopController.

Implements folly::fibers::LoopController.

Definition at line 86 of file SimpleLoopController.h.

References fm_, folly::fibers::FiberManager::loopUntilNoReadyImpl(), remoteLoopRun_, remoteScheduleCalled_, and folly::fibers::FiberManager::shouldRunLoopRemote().

Referenced by loop().

86  {
87  do {
90  if (fm_->shouldRunLoopRemote()) {
92  }
93  }
94  } else {
96  }
97  } while (remoteLoopRun_ < remoteScheduleCalled_);
98  }
void folly::fibers::SimpleLoopController::schedule ( )
inlineoverridevirtual

Called by FiberManager to schedule the loop function run at some point in the futufre.

Implements folly::fibers::LoopController.

Definition at line 100 of file SimpleLoopController.h.

References scheduled_.

100  {
101  scheduled_ = true;
102  }
void folly::fibers::SimpleLoopController::scheduleThreadSafe ( )
inlineoverrideprivatevirtual

Same as schedule(), but safe to call from any thread.

Implements folly::fibers::LoopController.

Definition at line 123 of file SimpleLoopController.h.

References remoteScheduleCalled_, and scheduled_.

123  {
125  scheduled_ = true;
126  }
void folly::fibers::SimpleLoopController::setFiberManager ( FiberManager )
inlineoverrideprivatevirtual

Called by FiberManager to associate itself with the LoopController.

Implements folly::fibers::LoopController.

Definition at line 119 of file SimpleLoopController.h.

119  {
120  fm_ = fm;
121  }
void folly::fibers::SimpleLoopController::setTimeFunc ( Function< TimePoint()>  timeFunc)
inline

Definition at line 39 of file SimpleLoopController.h.

References folly::gen::move, and timeFunc_.

39  {
40  timeFunc_ = std::move(timeFunc);
41  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void folly::fibers::SimpleLoopController::stop ( )
inline

Requests exit from loop() as soon as all waiting tasks complete.

Definition at line 78 of file SimpleLoopController.h.

References stopRequested_.

Referenced by Application::loop().

void folly::fibers::SimpleLoopController::timedSchedule ( std::function< void()>  func,
TimePoint  time 
)
inlineoverride

Definition at line 104 of file SimpleLoopController.h.

References folly::gen::move, and scheduledFuncs_.

104  {
105  scheduledFuncs_.emplace_back(time, std::move(func));
106  }
std::vector< std::pair< TimePoint, std::function< void()> > > scheduledFuncs_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::nanoseconds time()

Friends And Related Function Documentation

friend class FiberManager
friend

Definition at line 128 of file SimpleLoopController.h.

Member Data Documentation

FiberManager* folly::fibers::SimpleLoopController::fm_
private

Definition at line 109 of file SimpleLoopController.h.

Referenced by loop(), and runLoop().

int folly::fibers::SimpleLoopController::remoteLoopRun_ {0}
private

Definition at line 113 of file SimpleLoopController.h.

Referenced by runLoop().

std::atomic<int> folly::fibers::SimpleLoopController::remoteScheduleCalled_ {0}
private

Definition at line 112 of file SimpleLoopController.h.

Referenced by remoteScheduleCalled(), runLoop(), and scheduleThreadSafe().

std::atomic<bool> folly::fibers::SimpleLoopController::scheduled_ {false}
private

Definition at line 110 of file SimpleLoopController.h.

Referenced by loop(), schedule(), scheduleThreadSafe(), and ~SimpleLoopController().

std::vector<std::pair<TimePoint, std::function<void()> > > folly::fibers::SimpleLoopController::scheduledFuncs_
private

Definition at line 114 of file SimpleLoopController.h.

Referenced by loop(), and timedSchedule().

bool folly::fibers::SimpleLoopController::stopRequested_
private

Definition at line 111 of file SimpleLoopController.h.

Referenced by loop(), and stop().

Function<TimePoint()> folly::fibers::SimpleLoopController::timeFunc_ {[] { return Clock::now(); }}
private

Definition at line 115 of file SimpleLoopController.h.

Referenced by loop(), and setTimeFunc().


The documentation for this class was generated from the following file: