proxygen
folly::fibers::Promise< T, BatonT > Class Template Reference

#include <Promise.h>

Public Types

typedef T value_type
 
typedef BatonT baton_type
 

Public Member Functions

 ~Promise ()
 
 Promise (const Promise &)=delete
 
Promiseoperator= (const Promise &)=delete
 
 Promise (Promise &&) noexcept
 
Promiseoperator= (Promise &&)
 
void setValue ()
 
template<class M >
void setValue (M &&value)
 
void setTry (folly::Try< T > &&t)
 
template<class F >
void setWith (F &&func)
 
void setException (folly::exception_wrapper)
 

Static Public Member Functions

template<class F >
static value_type await (F &&func)
 

Private Member Functions

 Promise (folly::Try< T > &value, BatonT &baton)
 
void throwIfFulfilled () const
 
template<class F >
std::enable_if< std::is_convertible< invoke_result_t< F >, T >::value &&!std::is_same< T, void >::value >::type fulfilHelper (F &&func)
 
template<class F >
std::enable_if< std::is_same< invoke_result_t< F >, void >::value &&std::is_same< T, void >::value >::type fulfilHelper (F &&func)
 

Private Attributes

folly::Try< T > * value_
 
BatonT * baton_
 

Detailed Description

template<typename T, typename BatonT = Baton>
class folly::fibers::Promise< T, BatonT >

Definition at line 28 of file Promise.h.

Member Typedef Documentation

template<typename T, typename BatonT = Baton>
typedef BatonT folly::fibers::Promise< T, BatonT >::baton_type

Definition at line 31 of file Promise.h.

template<typename T, typename BatonT = Baton>
typedef T folly::fibers::Promise< T, BatonT >::value_type

Definition at line 30 of file Promise.h.

Constructor & Destructor Documentation

template<class T , class BatonT >
folly::fibers::Promise< T, BatonT >::~Promise ( )

Definition at line 47 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::setException(), and folly::fibers::Promise< T, BatonT >::value_.

47  {
48  if (value_) {
49  setException(folly::make_exception_wrapper<std::logic_error>(
50  "promise not fulfilled"));
51  }
52 }
folly::Try< T > * value_
Definition: Promise.h:86
void setException(folly::exception_wrapper)
Definition: Promise-inl.h:55
template<typename T, typename BatonT = Baton>
folly::fibers::Promise< T, BatonT >::Promise ( const Promise< T, BatonT > &  )
delete
template<class T , class BatonT >
folly::fibers::Promise< T, BatonT >::Promise ( Promise< T, BatonT > &&  other)
noexcept

Definition at line 26 of file Promise-inl.h.

27  : value_(other.value_), baton_(other.baton_) {
28  other.value_ = nullptr;
29  other.baton_ = nullptr;
30 }
folly::Try< T > * value_
Definition: Promise.h:86
template<class T , class BatonT >
folly::fibers::Promise< T, BatonT >::Promise ( folly::Try< T > &  value,
BatonT &  baton 
)
private

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

23  : value_(&value), baton_(&baton) {}
folly::Try< T > * value_
Definition: Promise.h:86

Member Function Documentation

template<class T , class BatonT >
template<class F >
Promise< T, BatonT >::value_type folly::fibers::Promise< T, BatonT >::await ( F &&  func)
static

Blocks task execution until given promise is fulfilled.

Calls function passing in a Promise<T>, which has to be fulfilled.

Returns
data which was used to fulfill the promise.

Definition at line 94 of file Promise-inl.h.

References folly::gen::move, and UNLIKELY.

Referenced by folly::fibers::await().

94  {
96  std::exception_ptr funcException;
97 
98  BatonT baton;
99  baton.wait([&func, &result, &baton, &funcException]() mutable {
100  try {
101  func(Promise<value_type, BatonT>(result, baton));
102  } catch (...) {
103  // Save the exception, but still wait for baton to be posted by user code
104  // or promise destructor.
105  funcException = std::current_exception();
106  }
107  });
108 
109  if (UNLIKELY(funcException != nullptr)) {
110  std::rethrow_exception(funcException);
111  }
112 
113  return std::move(result).value();
114 }
void rethrow_exception(std::exception_ptr ep)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Definition: Try.h:51
#define UNLIKELY(x)
Definition: Likely.h:48
template<typename T, typename BatonT = Baton>
template<class F >
std::enable_if< std::is_convertible<invoke_result_t<F>, T>::value && !std::is_same<T, void>::value>::type folly::fibers::Promise< T, BatonT >::fulfilHelper ( F &&  func)
private
template<typename T, typename BatonT = Baton>
template<class F >
std::enable_if< std::is_same<invoke_result_t<F>, void>::value && std::is_same<T, void>::value>::type folly::fibers::Promise< T, BatonT >::fulfilHelper ( F &&  func)
private
template<typename T, typename BatonT = Baton>
Promise& folly::fibers::Promise< T, BatonT >::operator= ( const Promise< T, BatonT > &  )
delete
template<class T , class BatonT >
Promise< T, BatonT > & folly::fibers::Promise< T, BatonT >::operator= ( Promise< T, BatonT > &&  other)

Definition at line 33 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::baton_, folly::f14::swap(), and folly::fibers::Promise< T, BatonT >::value_.

33  {
34  std::swap(value_, other.value_);
35  std::swap(baton_, other.baton_);
36  return *this;
37 }
folly::Try< T > * value_
Definition: Promise.h:86
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
template<class T , class BatonT >
void folly::fibers::Promise< T, BatonT >::setException ( folly::exception_wrapper  e)

Fulfill the Promise with an exception_wrapper, e.g. auto ew = folly::try_and_catch<std::exception>([]{ ... }); if (ew) { p.setException(std::move(ew)); }

Definition at line 55 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::setTry().

Referenced by folly::fibers::Promise< T, BatonT >::~Promise().

55  {
57 }
void setTry(folly::Try< T > &&t)
Definition: Promise-inl.h:60
Definition: Try.h:51
template<class T , class BatonT >
void folly::fibers::Promise< T, BatonT >::setTry ( folly::Try< T > &&  t)

Fulfill the promise with a given try

Parameters
t

Definition at line 60 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::baton_, folly::gen::move, folly::pushmi::detail::t, folly::fibers::Promise< T, BatonT >::throwIfFulfilled(), and folly::fibers::Promise< T, BatonT >::value_.

Referenced by folly::fibers::Promise< T, BatonT >::setException(), folly::fibers::Promise< T, BatonT >::setValue(), and folly::fibers::Promise< T, BatonT >::setWith().

60  {
62 
63  *value_ = std::move(t);
64  value_ = nullptr;
65 
66  // Baton::post has to be the last step here, since if Promise is not owned by
67  // the posting thread, it may be destroyed right after Baton::post is called.
68  baton_->post();
69 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Try< T > * value_
Definition: Promise.h:86
void throwIfFulfilled() const
Definition: Promise-inl.h:40
template<class T , class BatonT >
void folly::fibers::Promise< T, BatonT >::setValue ( )

Fulfill this promise (only for Promise<void>)

Definition at line 80 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::setTry(), and value.

Referenced by TEST().

80  {
81  static_assert(std::is_same<T, void>::value, "Use setValue(value) instead");
82 
84 }
void setTry(folly::Try< T > &&t)
Definition: Promise-inl.h:60
static const char *const value
Definition: Conv.cpp:50
template<class T , class BatonT >
template<class M >
void folly::fibers::Promise< T, BatonT >::setValue ( M &&  value)

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

Definition at line 73 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::setTry(), and value.

73  {
74  static_assert(!std::is_same<T, void>::value, "Use setValue() instead");
75 
76  setTry(folly::Try<T>(std::forward<M>(v)));
77 }
void setTry(folly::Try< T > &&t)
Definition: Promise-inl.h:60
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
template<class T , class BatonT >
template<class F >
void folly::fibers::Promise< T, BatonT >::setWith ( F &&  func)

Fulfill this promise 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 88 of file Promise-inl.h.

References folly::makeTryWith(), and folly::fibers::Promise< T, BatonT >::setTry().

88  {
89  setTry(makeTryWith(std::forward<F>(func)));
90 }
void setTry(folly::Try< T > &&t)
Definition: Promise-inl.h:60
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 , class BatonT >
void folly::fibers::Promise< T, BatonT >::throwIfFulfilled ( ) const
private

Definition at line 40 of file Promise-inl.h.

References folly::fibers::Promise< T, BatonT >::value_.

Referenced by folly::fibers::Promise< T, BatonT >::setTry().

40  {
41  if (!value_) {
42  throw std::logic_error("promise already fulfilled");
43  }
44 }
folly::Try< T > * value_
Definition: Promise.h:86

Member Data Documentation

template<typename T, typename BatonT = Baton>
BatonT* folly::fibers::Promise< T, BatonT >::baton_
private

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