proxygen
folly::futures::detail::DeferredExecutor Class Referencefinal

#include <Future-inl.h>

Inheritance diagram for folly::futures::detail::DeferredExecutor:
folly::Executor

Public Member Functions

void add (Func func) override
 
void setExecutor (folly::Executor::KeepAlive<> executor)
 
void detach ()
 
void setNestedExecutors (std::vector< folly::Executor::KeepAlive< DeferredExecutor >> executors)
 
- Public Member Functions inherited from folly::Executor
virtual ~Executor ()
 
virtual void addWithPriority (Func, int8_t priority)
 
virtual uint8_t getNumPriorities () const
 

Static Public Member Functions

static KeepAlive< DeferredExecutorcreate ()
 
- 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)
 

Private Types

enum  State { State::EMPTY, State::HAS_FUNCTION, State::HAS_EXECUTOR, State::DETACHED }
 

Private Member Functions

 DeferredExecutor ()
 
bool keepAliveAcquire () override
 
void keepAliveRelease () override
 

Private Attributes

std::atomic< Statestate_ {State::EMPTY}
 
Func func_
 
folly::Executor::KeepAlive executor_
 
std::unique_ptr< std::vector< folly::Executor::KeepAlive< DeferredExecutor > > > nestedExecutors_
 
std::atomic< ssize_t > keepAliveCount_ {1}
 

Additional Inherited Members

- 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

Defer work until executor is actively boosted.

NOTE: that this executor is a private implementation detail belonging to the Folly Futures library and not intended to be used elsewhere. It is designed specifically for the use case of deferring work on a SemiFuture. It is NOT thread safe. Please do not use for any other purpose without great care.

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

Member Enumeration Documentation

Enumerator
EMPTY 
HAS_FUNCTION 
HAS_EXECUTOR 
DETACHED 

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

601 { EMPTY, HAS_FUNCTION, HAS_EXECUTOR, DETACHED };

Constructor & Destructor Documentation

folly::futures::detail::DeferredExecutor::DeferredExecutor ( )
inlineprivate

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

583 {}

Member Function Documentation

void folly::futures::detail::DeferredExecutor::add ( Func  )
inlineoverridevirtual

Enqueue a function to executed by this executor. This and all variants must be threadsafe.

Implements folly::Executor.

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

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

499  {
500  auto state = state_.load(std::memory_order_acquire);
501  if (state == State::DETACHED) {
502  return;
503  }
504  if (state == State::HAS_EXECUTOR) {
505  executor_->add(std::move(func));
506  return;
507  }
508  DCHECK(state == State::EMPTY);
509  func_ = std::move(func);
510  if (state_.compare_exchange_strong(
511  state,
513  std::memory_order_release,
514  std::memory_order_acquire)) {
515  return;
516  }
518  if (state == State::DETACHED) {
519  std::exchange(func_, nullptr);
520  return;
521  }
522  executor_->add(std::exchange(func_, nullptr));
523  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Executor::KeepAlive executor_
Definition: Future-inl.h:604
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
state
Definition: http_parser.c:272
static KeepAlive<DeferredExecutor> folly::futures::detail::DeferredExecutor::create ( )
inlinestatic

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

Referenced by folly::SemiFuture< T >::defer().

578  {
579  return makeKeepAlive<DeferredExecutor>(new DeferredExecutor());
580  }
void folly::futures::detail::DeferredExecutor::detach ( )
inline

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

References folly::exchange().

548  {
549  if (nestedExecutors_) {
550  auto nestedExecutors = std::exchange(nestedExecutors_, nullptr);
551  for (auto& nestedExecutor : *nestedExecutors) {
552  nestedExecutor->detach();
553  }
554  }
555  auto state = state_.load(std::memory_order_acquire);
556  if (state == State::EMPTY &&
557  state_.compare_exchange_strong(
558  state,
560  std::memory_order_release,
561  std::memory_order_acquire)) {
562  return;
563  }
564 
565  DCHECK(state == State::HAS_FUNCTION);
566  state_.store(State::DETACHED, std::memory_order_release);
567  std::exchange(func_, nullptr);
568  }
std::unique_ptr< std::vector< folly::Executor::KeepAlive< DeferredExecutor > > > nestedExecutors_
Definition: Future-inl.h:606
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
state
Definition: http_parser.c:272
bool folly::futures::detail::DeferredExecutor::keepAliveAcquire ( )
inlineoverrideprivatevirtual

Reimplemented from folly::Executor.

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

585  {
586  auto keepAliveCount =
587  keepAliveCount_.fetch_add(1, std::memory_order_relaxed);
588  DCHECK(keepAliveCount > 0);
589  return true;
590  }
std::atomic< ssize_t > keepAliveCount_
Definition: Future-inl.h:607
void folly::futures::detail::DeferredExecutor::keepAliveRelease ( )
inlineoverrideprivatevirtual

Reimplemented from folly::Executor.

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

592  {
593  auto keepAliveCount =
594  keepAliveCount_.fetch_sub(1, std::memory_order_acq_rel);
595  DCHECK(keepAliveCount > 0);
596  if (keepAliveCount == 1) {
597  delete this;
598  }
599  }
std::atomic< ssize_t > keepAliveCount_
Definition: Future-inl.h:607
void folly::futures::detail::DeferredExecutor::setExecutor ( folly::Executor::KeepAlive<>  executor)
inline

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

References folly::Executor::KeepAlive< ExecutorT >::copy(), folly::exchange(), and folly::gen::move.

525  {
526  if (nestedExecutors_) {
527  auto nestedExecutors = std::exchange(nestedExecutors_, nullptr);
528  for (auto& nestedExecutor : *nestedExecutors) {
529  nestedExecutor->setExecutor(executor.copy());
530  }
531  }
532  executor_ = std::move(executor);
533  auto state = state_.load(std::memory_order_acquire);
534  if (state == State::EMPTY &&
535  state_.compare_exchange_strong(
536  state,
538  std::memory_order_release,
539  std::memory_order_acquire)) {
540  return;
541  }
542 
543  DCHECK(state == State::HAS_FUNCTION);
544  state_.store(State::HAS_EXECUTOR, std::memory_order_release);
545  executor_->add(std::exchange(func_, nullptr));
546  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< std::vector< folly::Executor::KeepAlive< DeferredExecutor > > > nestedExecutors_
Definition: Future-inl.h:606
folly::Executor::KeepAlive executor_
Definition: Future-inl.h:604
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
KeepAlive copy() const
Definition: Executor.h:113
state
Definition: http_parser.c:272
void folly::futures::detail::DeferredExecutor::setNestedExecutors ( std::vector< folly::Executor::KeepAlive< DeferredExecutor >>  executors)
inline

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

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

571  {
572  DCHECK(!nestedExecutors_);
574  std::vector<folly::Executor::KeepAlive<DeferredExecutor>>>(
575  std::move(executors));
576  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< std::vector< folly::Executor::KeepAlive< DeferredExecutor > > > nestedExecutors_
Definition: Future-inl.h:606
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259

Member Data Documentation

folly::Executor::KeepAlive folly::futures::detail::DeferredExecutor::executor_
private

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

Func folly::futures::detail::DeferredExecutor::func_
private

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

std::atomic<ssize_t> folly::futures::detail::DeferredExecutor::keepAliveCount_ {1}
private

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

std::unique_ptr<std::vector<folly::Executor::KeepAlive<DeferredExecutor> > > folly::futures::detail::DeferredExecutor::nestedExecutors_
private

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

std::atomic<State> folly::futures::detail::DeferredExecutor::state_ {State::EMPTY}
private

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


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