proxygen
folly::fibers::EventBaseLoopController Class Reference

#include <EventBaseLoopController.h>

Inheritance diagram for folly::fibers::EventBaseLoopController:
folly::fibers::LoopController

Classes

class  ControllerCallback
 

Public Member Functions

 EventBaseLoopController ()
 
 ~EventBaseLoopController () override
 
void attachEventBase (EventBase &eventBase)
 
void attachEventBase (VirtualEventBase &eventBase)
 
VirtualEventBasegetEventBase ()
 
void setLoopRunner (InlineFunctionRunner *loopRunner)
 
- Public Member Functions inherited from folly::fibers::LoopController
virtual ~LoopController ()
 

Private Member Functions

void setFiberManager (FiberManager *fm) override
 
void schedule () override
 
void runLoop () override
 
void scheduleThreadSafe () override
 
void timedSchedule (std::function< void()> func, TimePoint time) override
 

Private Attributes

bool awaitingScheduling_ {false}
 
VirtualEventBaseeventBase_ {nullptr}
 
Executor::KeepAlive< VirtualEventBaseeventBaseKeepAlive_
 
ControllerCallback callback_
 
FiberManagerfm_ {nullptr}
 
std::atomic< bool > eventBaseAttached_ {false}
 
InlineFunctionRunnerloopRunner_ {nullptr}
 

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 27 of file EventBaseLoopController.h.

Constructor & Destructor Documentation

folly::fibers::EventBaseLoopController::EventBaseLoopController ( )
inlineexplicit

Definition at line 22 of file EventBaseLoopController-inl.h.

22 : callback_(*this) {}
folly::fibers::EventBaseLoopController::~EventBaseLoopController ( )
inlineoverride

Member Function Documentation

void folly::fibers::EventBaseLoopController::attachEventBase ( EventBase eventBase)
inline

Attach EventBase after LoopController was created.

Definition at line 29 of file EventBaseLoopController-inl.h.

References folly::EventBase::getVirtualEventBase().

29  {
30  attachEventBase(eventBase.getVirtualEventBase());
31 }
void folly::fibers::EventBaseLoopController::attachEventBase ( VirtualEventBase eventBase)
inline

Definition at line 33 of file EventBaseLoopController-inl.h.

References awaitingScheduling_, eventBase_, eventBaseAttached_, and schedule().

34  {
35  if (eventBase_ != nullptr) {
36  LOG(ERROR) << "Attempt to reattach EventBase to LoopController";
37  }
38 
39  eventBase_ = &eventBase;
40  eventBaseAttached_ = true;
41 
42  if (awaitingScheduling_) {
43  schedule();
44  }
45 }
VirtualEventBase* folly::fibers::EventBaseLoopController::getEventBase ( )
inline

Definition at line 38 of file EventBaseLoopController.h.

References eventBase_.

Referenced by TEST_F().

38  {
39  return eventBase_;
40  }
void folly::fibers::EventBaseLoopController::runLoop ( )
inlineoverrideprivatevirtual

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

Implements folly::fibers::LoopController.

Definition at line 66 of file EventBaseLoopController-inl.h.

References eventBase_, eventBaseKeepAlive_, fm_, folly::getKeepAliveToken(), folly::fibers::FiberManager::hasReadyTasks(), folly::fibers::FiberManager::hasTasks(), loopRunner_, folly::fibers::FiberManager::loopUntilNoReadyImpl(), and folly::fibers::InlineFunctionRunner::run().

Referenced by folly::fibers::EventBaseLoopController::ControllerCallback::runLoopCallback(), and scheduleThreadSafe().

66  {
67  if (!eventBaseKeepAlive_) {
68  // runLoop can be called twice if both schedule() and scheduleThreadSafe()
69  // were called.
70  if (!fm_->hasTasks()) {
71  return;
72  }
74  }
75  if (loopRunner_) {
76  if (fm_->hasReadyTasks()) {
78  }
79  } else {
81  }
82  if (!fm_->hasTasks()) {
83  eventBaseKeepAlive_.reset();
84  }
85 }
Executor::KeepAlive< VirtualEventBase > eventBaseKeepAlive_
virtual void run(folly::Function< void()> func)=0
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
void folly::fibers::EventBaseLoopController::schedule ( )
inlineoverrideprivatevirtual

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

Implements folly::fibers::LoopController.

Definition at line 51 of file EventBaseLoopController-inl.h.

References awaitingScheduling_, callback_, eventBase_, eventBaseKeepAlive_, folly::VirtualEventBase::getEventBase(), folly::getKeepAliveToken(), and folly::EventBase::runInLoop().

Referenced by attachEventBase().

51  {
52  if (eventBase_ == nullptr) {
53  // In this case we need to postpone scheduling.
54  awaitingScheduling_ = true;
55  } else {
56  // Schedule it to run in current iteration.
57 
58  if (!eventBaseKeepAlive_) {
60  }
62  awaitingScheduling_ = false;
63  }
64 }
Executor::KeepAlive< VirtualEventBase > eventBaseKeepAlive_
void runInLoop(LoopCallback *callback, bool thisIteration=false)
Definition: EventBase.cpp:520
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
void folly::fibers::EventBaseLoopController::scheduleThreadSafe ( )
inlineoverrideprivatevirtual

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

Implements folly::fibers::LoopController.

Definition at line 87 of file EventBaseLoopController-inl.h.

References eventBase_, eventBaseAttached_, eventBaseKeepAlive_, fm_, folly::getKeepAliveToken(), folly::fibers::FiberManager::hasTasks(), folly::VirtualEventBase::runInEventBaseThread(), runLoop(), and folly::fibers::FiberManager::shouldRunLoopRemote().

87  {
88  /* The only way we could end up here is if
89  1) Fiber thread creates a fiber that awaits (which means we must
90  have already attached, fiber thread wouldn't be running).
91  2) We move the promise to another thread (this move is a memory fence)
92  3) We fulfill the promise from the other thread. */
93  assert(eventBaseAttached_);
94 
96  [this, eventBaseKeepAlive = getKeepAliveToken(eventBase_)]() {
97  if (fm_->shouldRunLoopRemote()) {
98  return runLoop();
99  }
100 
101  if (!fm_->hasTasks()) {
102  eventBaseKeepAlive_.reset();
103  }
104  });
105 }
Executor::KeepAlive< VirtualEventBase > eventBaseKeepAlive_
void runInEventBaseThread(F &&f)
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
void folly::fibers::EventBaseLoopController::setFiberManager ( FiberManager )
inlineoverrideprivatevirtual

Called by FiberManager to associate itself with the LoopController.

Implements folly::fibers::LoopController.

Definition at line 47 of file EventBaseLoopController-inl.h.

References fm_.

47  {
48  fm_ = fm;
49 }
void folly::fibers::EventBaseLoopController::setLoopRunner ( InlineFunctionRunner loopRunner)
inline

Definition at line 42 of file EventBaseLoopController.h.

References loopRunner_.

42  {
43  loopRunner_ = loopRunner;
44  }
void folly::fibers::EventBaseLoopController::timedSchedule ( std::function< void()>  func,
TimePoint  time 
)
inlineoverrideprivatevirtual

Called by FiberManager to schedule some function to be run at some time.

Implements folly::fibers::LoopController.

Definition at line 107 of file EventBaseLoopController-inl.h.

References count, eventBase_, eventBaseAttached_, now(), folly::TimeoutManager::tryRunAfterDelay(), and uint32_t.

109  {
110  assert(eventBaseAttached_);
111 
112  // We want upper bound for the cast, thus we just add 1
113  auto delay_ms =
114  std::chrono::duration_cast<std::chrono::milliseconds>(time - Clock::now())
115  .count() +
116  1;
117  // If clock is not monotonic
118  delay_ms = std::max<decltype(delay_ms)>(delay_ms, 0);
119  eventBase_->tryRunAfterDelay(func, uint32_t(delay_ms));
120 }
std::chrono::steady_clock::time_point now()
bool tryRunAfterDelay(Func cob, uint32_t milliseconds, InternalEnum internal=InternalEnum::NORMAL)
int * count
std::chrono::nanoseconds time()

Friends And Related Function Documentation

friend class FiberManager
friend

Definition at line 76 of file EventBaseLoopController.h.

Member Data Documentation

bool folly::fibers::EventBaseLoopController::awaitingScheduling_ {false}
private

Definition at line 60 of file EventBaseLoopController.h.

Referenced by attachEventBase(), and schedule().

ControllerCallback folly::fibers::EventBaseLoopController::callback_
private

Definition at line 63 of file EventBaseLoopController.h.

Referenced by schedule(), and ~EventBaseLoopController().

VirtualEventBase* folly::fibers::EventBaseLoopController::eventBase_ {nullptr}
private
std::atomic<bool> folly::fibers::EventBaseLoopController::eventBaseAttached_ {false}
private

Definition at line 65 of file EventBaseLoopController.h.

Referenced by attachEventBase(), scheduleThreadSafe(), and timedSchedule().

Executor::KeepAlive<VirtualEventBase> folly::fibers::EventBaseLoopController::eventBaseKeepAlive_
private
FiberManager* folly::fibers::EventBaseLoopController::fm_ {nullptr}
private

Definition at line 64 of file EventBaseLoopController.h.

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

InlineFunctionRunner* folly::fibers::EventBaseLoopController::loopRunner_ {nullptr}
private

Definition at line 66 of file EventBaseLoopController.h.

Referenced by runLoop(), and setLoopRunner().


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