proxygen
folly::ThreadWheelTimekeeper Class Reference

#include <ThreadWheelTimekeeper.h>

Inheritance diagram for folly::ThreadWheelTimekeeper:
folly::Timekeeper

Public Member Functions

 ThreadWheelTimekeeper ()
 But it doesn't have to be a singleton. More...
 
 ~ThreadWheelTimekeeper () override
 
Future< Unitafter (Duration) override
 
- Public Member Functions inherited from folly::Timekeeper
virtual ~Timekeeper ()=default
 
template<class Clock >
Future< Unitat (std::chrono::time_point< Clock > when)
 

Protected Attributes

folly::EventBase eventBase_
 
std::thread thread_
 
HHWheelTimer::UniquePtr wheelTimer_
 

Detailed Description

The default Timekeeper implementation which uses a HHWheelTimer on an EventBase in a dedicated thread. Users needn't deal with this directly, it is used by default by Future methods that work with timeouts.

Definition at line 29 of file ThreadWheelTimekeeper.h.

Constructor & Destructor Documentation

folly::ThreadWheelTimekeeper::ThreadWheelTimekeeper ( )

But it doesn't have to be a singleton.

Definition at line 104 of file ThreadWheelTimekeeper.cpp.

References eventBase_, folly::EventBase::loopForever(), folly::HHWheelTimer::newTimer(), folly::EventBase::runInEventBaseThread(), folly::EventBase::setName(), folly::EventBase::waitUntilRunning(), and wheelTimer_.

105  : thread_([this] { eventBase_.loopForever(); }),
106  wheelTimer_(
107  HHWheelTimer::newTimer(&eventBase_, std::chrono::milliseconds(1))) {
110  // 15 characters max
111  eventBase_.setName("FutureTimekeepr");
112  });
113 }
static UniquePtr newTimer(Args &&...args)
Definition: HHWheelTimer.h:61
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
void setName(const std::string &name)
Definition: EventBase.cpp:740
void waitUntilRunning()
Definition: EventBase.cpp:249
HHWheelTimer::UniquePtr wheelTimer_
folly::ThreadWheelTimekeeper::~ThreadWheelTimekeeper ( )
override

Definition at line 115 of file ThreadWheelTimekeeper.cpp.

References eventBase_, folly::EventBase::runInEventBaseThreadAndWait(), folly::EventBase::terminateLoopSoon(), thread_, and wheelTimer_.

115  {
117  wheelTimer_->cancelAll();
119  });
120  thread_.join();
121 }
bool runInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:799
void terminateLoopSoon()
Definition: EventBase.cpp:493
HHWheelTimer::UniquePtr wheelTimer_

Member Function Documentation

Future< Unit > folly::ThreadWheelTimekeeper::after ( Duration  dur)
overridevirtual

Implement the Timekeeper interface This future does complete on the timer thread. You should almost certainly follow it with a via() call or the accuracy of other timers will suffer.

Implements folly::Timekeeper.

Definition at line 123 of file ThreadWheelTimekeeper.cpp.

References eventBase_, f, and folly::EventBase::runInEventBaseThread().

Referenced by TEST_F().

123  {
124  auto cob = WTCallback::create(&eventBase_);
125  auto f = cob->getFuture();
126  //
127  // Even shared_ptr of cob is captured in lambda this is still somewhat *racy*
128  // because it will be released once timeout is scheduled. So technically there
129  // is no gurantee that EventBase thread can safely call timeout callback.
130  // However due to fact that we are having circular reference here:
131  // WTCallback->Promise->Core->WTCallbak, so three of them won't go away until
132  // we break the circular reference. The break happens either in
133  // WTCallback::timeoutExpired or WTCallback::interruptHandler. Former means
134  // timeout callback is being safely executed. Latter captures shared_ptr of
135  // WTCallback again in another lambda for canceling timeout. The moment
136  // canceling timeout is executed in EventBase thread, the actual timeout
137  // callback has either been executed, or will never be executed. So we are
138  // fine here.
139  //
141  [this, cob, dur] { wheelTimer_->scheduleTimeout(cob.get(), dur); })) {
142  // Release promise to break the circular reference. Because if
143  // scheduleTimeout fails, there is nothing to *promise*. Internally
144  // Core would automatically set an exception result when Promise is
145  // destructed before fulfilling.
146  // This is either called from EventBase thread, or here.
147  // They are somewhat racy but given the rare chance this could fail,
148  // I don't see it is introducing any problem yet.
149  auto promise = cob->stealPromise();
150  if (!promise.isFulfilled()) {
151  promise.setException(FutureNoTimekeeper{});
152  }
153  }
154  return f;
155 }
auto f
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794

Member Data Documentation

folly::EventBase folly::ThreadWheelTimekeeper::eventBase_
protected

Definition at line 42 of file ThreadWheelTimekeeper.h.

Referenced by after(), ThreadWheelTimekeeper(), and ~ThreadWheelTimekeeper().

std::thread folly::ThreadWheelTimekeeper::thread_
protected

Definition at line 43 of file ThreadWheelTimekeeper.h.

Referenced by ~ThreadWheelTimekeeper().

HHWheelTimer::UniquePtr folly::ThreadWheelTimekeeper::wheelTimer_
protected

Definition at line 44 of file ThreadWheelTimekeeper.h.

Referenced by ThreadWheelTimekeeper(), and ~ThreadWheelTimekeeper().


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