proxygen
folly::Timekeeper Class Referenceabstract

#include <Future.h>

Inheritance diagram for folly::Timekeeper:
folly::ThreadWheelTimekeeper wangle::TimekeeperTester

Public Member Functions

virtual ~Timekeeper ()=default
 
virtual Future< Unitafter (Duration dur)=0
 
template<class Clock >
Future< Unitat (std::chrono::time_point< Clock > when)
 

Detailed Description

A Timekeeper handles the details of keeping time and fulfilling delay promises. The returned Future<Unit> will either complete after the elapsed time, or in the event of some kind of exceptional error may hold an exception. These Futures respond to cancellation. If you use a lot of Delays and many of them ultimately are unneeded (as would be the case for Delays that are used to trigger timeouts of async operations), then you can and should cancel them to reclaim resources.

Users will typically get one of these via Future::sleep(Duration dur) or use them implicitly behind the scenes by passing a timeout to some Future operation.

Although we don't formally alias Delay = Future<Unit>, that's an appropriate term for it. People will probably also call these Timeouts, and that's ok I guess, but that term is so overloaded I thought it made sense to introduce a cleaner term.

Remember that Duration is a std::chrono duration (millisecond resolution at the time of writing). When writing code that uses specific durations, prefer using the explicit std::chrono type, e.g. std::chrono::milliseconds over Duration. This makes the code more legible and means you won't be unpleasantly surprised if we redefine Duration to microseconds, or something.

timekeeper.after(std::chrono::duration_cast<Duration>(someNanoseconds))

Definition at line 2005 of file Future.h.

Constructor & Destructor Documentation

virtual folly::Timekeeper::~Timekeeper ( )
virtualdefault

Member Function Documentation

virtual Future<Unit> folly::Timekeeper::after ( Duration  dur)
pure virtual

Returns a future that will complete after the given duration with the elapsed time. Exceptional errors can happen but they must be exceptional. Use the steady (monotonic) clock.

The consumer thread may cancel this Future to reclaim resources.

This future probably completes on the timer thread. You should almost certainly follow it with a via() call or the accuracy of other timers will suffer.

Implemented in wangle::TimekeeperTester, and folly::ThreadWheelTimekeeper.

Referenced by folly::futures::sleep().

template<class Clock >
Future< Unit > folly::Timekeeper::at ( std::chrono::time_point< Clock >  when)

Returns a future that will complete at the requested time.

You may cancel this Future to reclaim resources.

NB This is sugar for after(when - now), so while you are welcome to use a std::chrono::system_clock::time_point it will not track changes to the system clock but rather execute that many milliseconds in the future according to the steady clock.

Definition at line 2386 of file Future-inl.h.

References folly::Future< T >::makeFuture, and now().

2386  {
2387  auto now = Clock::now();
2388 
2389  if (when <= now) {
2390  return makeFuture();
2391  }
2392 
2393  return after(std::chrono::duration_cast<Duration>(when - now));
2394 }
std::chrono::steady_clock::time_point now()
Future< Unit > when(bool p, F &&thunk)
Definition: Future-inl.h:2330
virtual Future< Unit > after(Duration dur)=0
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310

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