proxygen
folly::VirtualEventBase Class Reference

#include <VirtualEventBase.h>

Inheritance diagram for folly::VirtualEventBase:
folly::Executor folly::TimeoutManager

Public Member Functions

 VirtualEventBase (EventBase &evb)
 
 VirtualEventBase (const VirtualEventBase &)=delete
 
VirtualEventBaseoperator= (const VirtualEventBase &)=delete
 
 ~VirtualEventBase () override
 
EventBasegetEventBase ()
 
void runOnDestruction (EventBase::LoopCallback *callback)
 
template<typename F >
void runInEventBaseThread (F &&f)
 
HHWheelTimertimer ()
 
void attachTimeoutManager (AsyncTimeout *obj, TimeoutManager::InternalEnum internal) override
 
void detachTimeoutManager (AsyncTimeout *obj) override
 
bool scheduleTimeout (AsyncTimeout *obj, TimeoutManager::timeout_type timeout) override
 
void cancelTimeout (AsyncTimeout *obj) override
 
void bumpHandlingTime () override
 
bool isInTimeoutManagerThread () override
 
void add (folly::Func f) override
 
bool inRunningEventBaseThread () const
 
- Public Member Functions inherited from folly::Executor
virtual ~Executor ()
 
virtual void addWithPriority (Func, int8_t priority)
 
virtual uint8_t getNumPriorities () const
 
- Public Member Functions inherited from folly::TimeoutManager
 TimeoutManager ()
 
virtual ~TimeoutManager ()
 
void runAfterDelay (Func cob, uint32_t milliseconds, InternalEnum internal=InternalEnum::NORMAL)
 
bool tryRunAfterDelay (Func cob, uint32_t milliseconds, InternalEnum internal=InternalEnum::NORMAL)
 

Protected Member Functions

bool keepAliveAcquire () override
 
void keepAliveRelease () override
 
- Protected Member Functions inherited from folly::TimeoutManager
void clearCobTimeouts ()
 

Private Types

using LoopCallbackList = EventBase::LoopCallback::List
 

Private Member Functions

ssize_t keepAliveCount ()
 
std::future< void > destroy ()
 
void destroyImpl ()
 

Private Attributes

KeepAlive< EventBaseevb_
 
ssize_t loopKeepAliveCount_ {1}
 
std::atomic< ssize_t > loopKeepAliveCountAtomic_ {0}
 
std::promise< void > destroyPromise_
 
std::future< void > destroyFuture_ {destroyPromise_.get_future()}
 
KeepAlive< VirtualEventBaseloopKeepAlive_
 
folly::Synchronized< LoopCallbackListonDestructionCallbacks_
 

Friends

class EventBase
 

Additional Inherited Members

- Public Types inherited from folly::TimeoutManager
enum  InternalEnum { InternalEnum::INTERNAL, InternalEnum::NORMAL }
 
typedef std::chrono::milliseconds timeout_type
 
using Func = folly::Function< void()>
 
- Static Public Member Functions inherited from folly::Executor
template<typename ExecutorT >
static KeepAlive< ExecutorT > getKeepAliveToken (ExecutorT *executor)
 
template<typename ExecutorT >
static KeepAlive< ExecutorT > getKeepAliveToken (ExecutorT &executor)
 
- Static Public Attributes inherited from folly::Executor
static const int8_t LO_PRI = SCHAR_MIN
 
static const int8_t MID_PRI = 0
 
static const int8_t HI_PRI = SCHAR_MAX
 
- Static Protected Member Functions inherited from folly::Executor
template<typename ExecutorT >
static bool isKeepAliveDummy (const KeepAlive< ExecutorT > &keepAlive)
 
template<typename ExecutorT >
static KeepAlive< ExecutorT > makeKeepAlive (ExecutorT *executor)
 

Detailed Description

VirtualEventBase implements a light-weight view onto existing EventBase.

Multiple VirtualEventBases can be backed by a single EventBase. Similarly to EventBase, VirtualEventBase implements loopKeepAlive() functionality, which allows callbacks holding KeepAlive token to keep EventBase looping until they are complete.

VirtualEventBase destructor blocks until all its KeepAliveTokens are released and all tasks scheduled through it are complete. EventBase destructor also blocks until all VirtualEventBases backed by it are released.

Definition at line 40 of file VirtualEventBase.h.

Member Typedef Documentation

Constructor & Destructor Documentation

folly::VirtualEventBase::VirtualEventBase ( EventBase evb)
explicit

Definition at line 20 of file VirtualEventBase.cpp.

21  : evb_(getKeepAliveToken(evb)) {}
KeepAlive< EventBase > evb_
static KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:138
folly::VirtualEventBase::VirtualEventBase ( const VirtualEventBase )
delete
folly::VirtualEventBase::~VirtualEventBase ( )
override

Definition at line 57 of file VirtualEventBase.cpp.

References destroy(), destroyFuture_, and evb_.

57  {
58  if (!destroyFuture_.valid()) {
59  return;
60  }
61  CHECK(!evb_->inRunningEventBaseThread());
62  destroy().get();
63 }
KeepAlive< EventBase > evb_
std::future< void > destroy()
std::future< void > destroyFuture_

Member Function Documentation

void folly::VirtualEventBase::add ( folly::Func  f)
inlineoverridevirtual
See also
runInEventBaseThread

Implements folly::Executor.

Definition at line 115 of file VirtualEventBase.h.

References folly::gen::move, and runInEventBaseThread().

115  {
117  }
void runInEventBaseThread(F &&f)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void folly::VirtualEventBase::attachTimeoutManager ( AsyncTimeout obj,
TimeoutManager::InternalEnum  internal 
)
inlineoverridevirtual

Attaches/detaches TimeoutManager to AsyncTimeout

Implements folly::TimeoutManager.

Definition at line 85 of file VirtualEventBase.h.

References evb_.

87  {
88  evb_->attachTimeoutManager(obj, internal);
89  }
KeepAlive< EventBase > evb_
void folly::VirtualEventBase::bumpHandlingTime ( )
inlineoverridevirtual

This is used to mark the beginning of a new loop cycle by the first handler fired within that cycle.

Implements folly::TimeoutManager.

Definition at line 104 of file VirtualEventBase.h.

References evb_.

104  {
105  evb_->bumpHandlingTime();
106  }
KeepAlive< EventBase > evb_
void folly::VirtualEventBase::cancelTimeout ( AsyncTimeout obj)
inlineoverridevirtual

Cancels the AsyncTimeout, if scheduled

Implements folly::TimeoutManager.

Definition at line 100 of file VirtualEventBase.h.

References evb_.

100  {
101  evb_->cancelTimeout(obj);
102  }
KeepAlive< EventBase > evb_
std::future< void > folly::VirtualEventBase::destroy ( )
private

Definition at line 23 of file VirtualEventBase.cpp.

References destroyFuture_, evb_, and folly::gen::move.

Referenced by keepAliveCount(), and ~VirtualEventBase().

23  {
24  CHECK(evb_->runInEventBaseThread([this] { loopKeepAlive_.reset(); }));
25 
26  return std::move(destroyFuture_);
27 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
KeepAlive< EventBase > evb_
std::future< void > destroyFuture_
void folly::VirtualEventBase::destroyImpl ( )
private

Definition at line 29 of file VirtualEventBase.cpp.

References folly::TimeoutManager::clearCobTimeouts(), destroyPromise_, evb_, onDestructionCallbacks_, SCOPE_EXIT, and folly::Synchronized< T, Mutex >::swap().

Referenced by keepAliveCount(), and keepAliveRelease().

29  {
30  try {
31  {
32  // After destroyPromise_ is posted this object may be destroyed, so make
33  // sure we release EventBase's keep-alive token before that.
34  SCOPE_EXIT {
35  evb_.reset();
36  };
37 
39 
40  // To avoid potential deadlock, do not hold the mutex while invoking
41  // user-supplied callbacks.
42  LoopCallbackList callbacks;
43  onDestructionCallbacks_.swap(callbacks);
44  while (!callbacks.empty()) {
45  auto& callback = callbacks.front();
46  callbacks.pop_front();
47  callback.runLoopCallback();
48  }
49  }
50 
51  destroyPromise_.set_value();
52  } catch (...) {
53  destroyPromise_.set_exception(std::current_exception());
54  }
55 }
std::promise< void > destroyPromise_
void swap(Synchronized &rhs)
Definition: Synchronized.h:684
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
folly::Synchronized< LoopCallbackList > onDestructionCallbacks_
KeepAlive< EventBase > evb_
EventBase::LoopCallback::List LoopCallbackList
void folly::VirtualEventBase::detachTimeoutManager ( AsyncTimeout obj)
inlineoverridevirtual

Implements folly::TimeoutManager.

Definition at line 91 of file VirtualEventBase.h.

References evb_.

91  {
92  evb_->detachTimeoutManager(obj);
93  }
KeepAlive< EventBase > evb_
EventBase& folly::VirtualEventBase::getEventBase ( )
inline

Definition at line 49 of file VirtualEventBase.h.

References evb_, and runOnDestruction().

Referenced by folly::fibers::EventBaseLoopController::schedule(), and TEST_F().

49  {
50  return *evb_;
51  }
KeepAlive< EventBase > evb_
bool folly::VirtualEventBase::inRunningEventBaseThread ( ) const
inline

Definition at line 119 of file VirtualEventBase.h.

References evb_.

119  {
120  return evb_->inRunningEventBaseThread();
121  }
KeepAlive< EventBase > evb_
bool folly::VirtualEventBase::isInTimeoutManagerThread ( )
inlineoverridevirtual

Helper method to know whether we are running in the timeout manager thread

Implements folly::TimeoutManager.

Definition at line 108 of file VirtualEventBase.h.

References evb_.

108  {
109  return evb_->isInTimeoutManagerThread();
110  }
KeepAlive< EventBase > evb_
bool folly::VirtualEventBase::keepAliveAcquire ( )
inlineoverrideprotectedvirtual

Reimplemented from folly::Executor.

Definition at line 124 of file VirtualEventBase.h.

References evb_, loopKeepAliveCount_, and loopKeepAliveCountAtomic_.

124  {
125  if (evb_->inRunningEventBaseThread()) {
126  DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0);
127 
129  } else {
131  }
132  return true;
133  }
KeepAlive< EventBase > evb_
std::atomic< ssize_t > loopKeepAliveCountAtomic_
ssize_t folly::VirtualEventBase::keepAliveCount ( )
inlineprivate

Definition at line 151 of file VirtualEventBase.h.

References destroy(), destroyImpl(), loopKeepAliveCount_, and loopKeepAliveCountAtomic_.

151  {
152  if (loopKeepAliveCountAtomic_.load()) {
154  }
155  return loopKeepAliveCount_;
156  }
std::atomic< ssize_t > loopKeepAliveCountAtomic_
void folly::VirtualEventBase::keepAliveRelease ( )
inlineoverrideprotectedvirtual

Reimplemented from folly::Executor.

Definition at line 135 of file VirtualEventBase.h.

References destroyImpl(), evb_, loopKeepAliveCount_, and loopKeepAliveCountAtomic_.

135  {
136  if (!evb_->inRunningEventBaseThread()) {
137  return evb_->add([=] { keepAliveRelease(); });
138  }
139  if (loopKeepAliveCountAtomic_.load()) {
141  }
142  DCHECK(loopKeepAliveCount_ > 0);
143  if (--loopKeepAliveCount_ == 0) {
144  destroyImpl();
145  }
146  }
void keepAliveRelease() override
KeepAlive< EventBase > evb_
std::atomic< ssize_t > loopKeepAliveCountAtomic_
VirtualEventBase& folly::VirtualEventBase::operator= ( const VirtualEventBase )
delete
template<typename F >
void folly::VirtualEventBase::runInEventBaseThread ( F &&  f)
inline

VirtualEventBase destructor blocks until all tasks scheduled through its runInEventBaseThread are complete.

See also
EventBase::runInEventBaseThread

Definition at line 72 of file VirtualEventBase.h.

References evb_, f, and folly::Executor::getKeepAliveToken().

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

72  {
73  // KeepAlive token has to be released in the EventBase thread. If
74  // runInEventBaseThread() fails, we can't extract the KeepAlive token
75  // from the callback to properly release it.
76  CHECK(evb_->runInEventBaseThread(
77  [keepAliveToken = getKeepAliveToken(this),
78  f = std::forward<F>(f)]() mutable { f(); }));
79  }
auto f
KeepAlive< EventBase > evb_
static KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:138
void folly::VirtualEventBase::runOnDestruction ( EventBase::LoopCallback callback)

Adds the given callback to a queue of things run before destruction of current VirtualEventBase.

This allows users of VirtualEventBase that run in it, but don't control it, to be notified before VirtualEventBase gets destructed.

Note: this will be called from the loop of the EventBase, backing this VirtualEventBase

Definition at line 65 of file VirtualEventBase.cpp.

References folly::EventBase::LoopCallback::cancelLoopCallback(), and onDestructionCallbacks_.

Referenced by getEventBase().

65  {
66  onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
67  callback->cancelLoopCallback();
68  callbacks.push_back(*callback);
69  });
70 }
folly::Synchronized< LoopCallbackList > onDestructionCallbacks_
EventBase::LoopCallback::List LoopCallbackList
bool folly::VirtualEventBase::scheduleTimeout ( AsyncTimeout obj,
TimeoutManager::timeout_type  timeout 
)
inlineoverridevirtual

Schedules AsyncTimeout to fire after timeout milliseconds

Implements folly::TimeoutManager.

Definition at line 95 of file VirtualEventBase.h.

References evb_.

96  {
97  return evb_->scheduleTimeout(obj, timeout);
98  }
KeepAlive< EventBase > evb_
HHWheelTimer& folly::VirtualEventBase::timer ( )
inline

Definition at line 81 of file VirtualEventBase.h.

References evb_.

81  {
82  return evb_->timer();
83  }
KeepAlive< EventBase > evb_

Friends And Related Function Documentation

friend class EventBase
friend

Definition at line 149 of file VirtualEventBase.h.

Member Data Documentation

std::future<void> folly::VirtualEventBase::destroyFuture_ {destroyPromise_.get_future()}
private

Definition at line 168 of file VirtualEventBase.h.

Referenced by destroy(), and ~VirtualEventBase().

std::promise<void> folly::VirtualEventBase::destroyPromise_
private

Definition at line 167 of file VirtualEventBase.h.

Referenced by destroyImpl().

KeepAlive<VirtualEventBase> folly::VirtualEventBase::loopKeepAlive_
private
Initial value:
{
makeKeepAlive<VirtualEventBase>(this)}

Definition at line 169 of file VirtualEventBase.h.

ssize_t folly::VirtualEventBase::loopKeepAliveCount_ {1}
private

Definition at line 165 of file VirtualEventBase.h.

Referenced by keepAliveAcquire(), keepAliveCount(), and keepAliveRelease().

std::atomic<ssize_t> folly::VirtualEventBase::loopKeepAliveCountAtomic_ {0}
private

Definition at line 166 of file VirtualEventBase.h.

Referenced by keepAliveAcquire(), keepAliveCount(), and keepAliveRelease().

folly::Synchronized<LoopCallbackList> folly::VirtualEventBase::onDestructionCallbacks_
private

Definition at line 172 of file VirtualEventBase.h.

Referenced by destroyImpl(), and runOnDestruction().


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