proxygen
folly::SharedPromise< T > Class Template Reference

#include <SharedPromise.h>

Public Member Functions

 SharedPromise ()=default
 
 ~SharedPromise ()=default
 
 SharedPromise (SharedPromise const &)=delete
 
SharedPromiseoperator= (SharedPromise const &)=delete
 
 SharedPromise (SharedPromise< T > &&) noexcept
 
SharedPromiseoperator= (SharedPromise< T > &&) noexcept
 
SemiFuture< TgetSemiFuture ()
 
Future< TgetFuture ()
 
size_t size ()
 
void setException (exception_wrapper ew)
 
template<class E >
std::enable_if< std::is_base_of< std::exception, E >::value >::type setException (E const &)
 
void setInterruptHandler (std::function< void(exception_wrapper const &)>)
 
template<class B = T>
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue ()
 Sugar to fulfill this SharedPromise<Unit> More...
 
template<class M >
void setValue (M &&value)
 
void setTry (Try< T > &&t)
 
template<class F >
void setWith (F &&func)
 
bool isFulfilled ()
 

Private Attributes

std::mutex mutex_
 
size_t size_ {0}
 
bool hasValue_ {false}
 
Try< Ttry_
 
std::vector< Promise< T > > promises_
 
std::function< void(exception_wrapper const &)> interruptHandler_
 

Detailed Description

template<class T>
class folly::SharedPromise< T >

Definition at line 41 of file SharedPromise.h.

Constructor & Destructor Documentation

template<class T>
folly::SharedPromise< T >::SharedPromise ( )
default
template<class T>
folly::SharedPromise< T >::~SharedPromise ( )
default
template<class T>
folly::SharedPromise< T >::SharedPromise ( SharedPromise< T > const &  )
delete
template<class T>
folly::SharedPromise< T >::SharedPromise ( SharedPromise< T > &&  other)
noexcept

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

References folly::gen::move.

22  {
23  *this = std::move(other);
24 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567

Member Function Documentation

template<class T >
Future< T > folly::SharedPromise< T >::getFuture ( )

Return a Future tied to the shared core state. Unlike Promise::getFuture, this can be called an unlimited number of times per SharedPromise. NOTE: This function is deprecated. Please use getSemiFuture and pass the appropriate executor to .via on the returned SemiFuture to get a valid Future where necessary.

Definition at line 72 of file SharedPromise-inl.h.

References folly::InlineExecutor::instance(), type, and value.

Referenced by TEST(), and wangle::OutputBufferingHandler::write().

72  {
73  return getSemiFuture().via(&InlineExecutor::instance());
74 }
FOLLY_ATTR_VISIBILITY_HIDDEN static FOLLY_ALWAYS_INLINE InlineExecutor & instance() noexcept
SemiFuture< T > getSemiFuture()
template<class T >
SemiFuture< T > folly::SharedPromise< T >::getSemiFuture ( )

Return a Future tied to the shared core state. Unlike Promise::getFuture, this can be called an unlimited number of times per SharedPromise.

Definition at line 57 of file SharedPromise-inl.h.

References g(), and mutex_.

Referenced by TEST().

57  {
58  std::lock_guard<std::mutex> g(mutex_);
59  size_++;
60  if (hasValue_) {
61  return makeFuture<T>(Try<T>(try_));
62  } else {
63  promises_.emplace_back();
64  if (interruptHandler_) {
65  promises_.back().setInterruptHandler(interruptHandler_);
66  }
67  return promises_.back().getSemiFuture();
68  }
69 }
g_t g(f_t)
std::vector< Promise< T > > promises_
std::function< void(exception_wrapper const &)> interruptHandler_
template<class T >
bool folly::SharedPromise< T >::isFulfilled ( )

Definition at line 133 of file SharedPromise-inl.h.

References g(), and mutex_.

Referenced by folly::SharedPromise< folly::folly::Unit >::setValue(), and TEST().

133  {
134  std::lock_guard<std::mutex> g(mutex_);
135  return hasValue_;
136 }
g_t g(f_t)
template<class T>
SharedPromise& folly::SharedPromise< T >::operator= ( SharedPromise< T > const &  )
delete
template<class T>
SharedPromise< T > & folly::SharedPromise< T >::operator= ( SharedPromise< T > &&  other)
noexcept

Definition at line 27 of file SharedPromise-inl.h.

References folly::detail::lock(), mutex_, and folly::f14::swap().

28  {
29  if (this == &other) {
30  return *this;
31  }
32 
33  // std::lock will perform deadlock avoidance, in case
34  // Thread A: p1 = std::move(p2)
35  // Thread B: p2 = std::move(p1)
36  // race each other
37  std::lock(mutex_, other.mutex_);
38  std::lock_guard<std::mutex> g1(mutex_, std::adopt_lock);
39  std::lock_guard<std::mutex> g2(other.mutex_, std::adopt_lock);
40 
41  std::swap(size_, other.size_);
42  std::swap(hasValue_, other.hasValue_);
43  std::swap(try_, other.try_);
44  std::swap(interruptHandler_, other.interruptHandler_);
45  std::swap(promises_, other.promises_);
46 
47  return *this;
48 }
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
std::vector< Promise< T > > promises_
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
std::function< void(exception_wrapper const &)> interruptHandler_
template<class T >
void folly::SharedPromise< T >::setException ( exception_wrapper  ew)

Fulfill the SharedPromise with an exception_wrapper

Definition at line 84 of file SharedPromise-inl.h.

References folly::gen::move.

Referenced by wangle::OutputBufferingHandler::close().

84  {
85  setTry(Try<T>(std::move(ew)));
86 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void setTry(Try< T > &&t)
template<class T >
template<class E >
std::enable_if< std::is_base_of< std::exception, E >::value >::type folly::SharedPromise< T >::setException ( E const &  e)

Fulfill the SharedPromise with an exception type E, which can be passed to make_exception_wrapper(). Useful for originating exceptions. If you caught an exception the exception_wrapper form is more appropriate.

Definition at line 79 of file SharedPromise-inl.h.

79  {
80  setTry(Try<T>(e));
81 }
void setTry(Try< T > &&t)
template<class T >
void folly::SharedPromise< T >::setInterruptHandler ( std::function< void(exception_wrapper const &)>  fn)

Set an interrupt handler to handle interrupts. See the documentation for Future::raise(). Your handler can do whatever it wants, but if you bother to set one then you probably will want to fulfill the SharedPromise with an exception (or special value) indicating how the interrupt was handled.

Definition at line 89 of file SharedPromise-inl.h.

References g(), and mutex_.

Referenced by TEST().

90  {
91  std::lock_guard<std::mutex> g(mutex_);
92  if (hasValue_) {
93  return;
94  }
95  interruptHandler_ = fn;
96  for (auto& p : promises_) {
97  p.setInterruptHandler(fn);
98  }
99 }
g_t g(f_t)
std::vector< Promise< T > > promises_
std::function< void(exception_wrapper const &)> interruptHandler_
template<class T>
void folly::SharedPromise< T >::setTry ( Try< T > &&  t)

Definition at line 114 of file SharedPromise-inl.h.

References g(), folly::gen::move, mutex_, and folly::pushmi::detail::t.

Referenced by wangle::OutputBufferingHandler::runLoopCallback(), and folly::SharedPromise< folly::folly::Unit >::setValue().

114  {
115  std::vector<Promise<T>> promises;
116 
117  {
118  std::lock_guard<std::mutex> g(mutex_);
119  if (hasValue_) {
120  throw_exception<PromiseAlreadySatisfied>();
121  }
122  hasValue_ = true;
123  try_ = std::move(t);
124  promises.swap(promises_);
125  }
126 
127  for (auto& p : promises) {
128  p.setTry(Try<T>(try_));
129  }
130 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
g_t g(f_t)
std::vector< Promise< T > > promises_
template<class T>
template<class B = T>
std::enable_if<std::is_same<Unit, B>::value, void>::type folly::SharedPromise< T >::setValue ( )
inline

Sugar to fulfill this SharedPromise<Unit>

Definition at line 92 of file SharedPromise.h.

Referenced by folly::SharedPromise< folly::folly::Unit >::setValue(), and TEST().

92  {
93  setTry(Try<T>(T()));
94  }
folly::std T
void setTry(Try< T > &&t)
template<class T >
template<class M >
void folly::SharedPromise< T >::setValue ( M &&  value)

Set the value (use perfect forwarding for both move and copy)

Definition at line 103 of file SharedPromise-inl.h.

103  {
104  setTry(Try<T>(std::forward<M>(v)));
105 }
void setTry(Try< T > &&t)
template<class T >
template<class F >
void folly::SharedPromise< T >::setWith ( F &&  func)

Fulfill this SharedPromise with the result of a function that takes no arguments and returns something implicitly convertible to T. Captures exceptions. e.g.

p.setWith([] { do something that may throw; return a T; });

Definition at line 109 of file SharedPromise-inl.h.

References folly::makeTryWith().

Referenced by folly::SharedPromise< folly::folly::Unit >::setValue(), and TEST().

109  {
110  setTry(makeTryWith(std::forward<F>(func)));
111 }
void setTry(Try< T > &&t)
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
template<class T >
size_t folly::SharedPromise< T >::size ( )

Return the number of Futures associated with this SharedPromise

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

References g(), and mutex_.

51  {
52  std::lock_guard<std::mutex> g(mutex_);
53  return size_;
54 }
g_t g(f_t)

Member Data Documentation

template<class T>
bool folly::SharedPromise< T >::hasValue_ {false}
private

Definition at line 116 of file SharedPromise.h.

template<class T>
std::function<void(exception_wrapper const&)> folly::SharedPromise< T >::interruptHandler_
private

Definition at line 119 of file SharedPromise.h.

template<class T>
std::mutex folly::SharedPromise< T >::mutex_
private

Definition at line 114 of file SharedPromise.h.

template<class T>
std::vector<Promise<T> > folly::SharedPromise< T >::promises_
private

Definition at line 118 of file SharedPromise.h.

template<class T>
size_t folly::SharedPromise< T >::size_ {0}
private

Definition at line 115 of file SharedPromise.h.

template<class T>
Try<T> folly::SharedPromise< T >::try_
private

Definition at line 117 of file SharedPromise.h.


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