proxygen
folly::pushmi::detail::trampoline< E > Class Template Reference

#include <trampoline.h>

Static Public Member Functions

static trampoline_id get_id ()
 
static bool is_owned ()
 
template<class Selector , class Derived >
static void submit (Selector, Derived &, recurse_t)
 
requires not static Same< SingleReceiver, recurse_t > void submit (ownordelegate_t, SingleReceiver awhat)
 
requires not static Same< SingleReceiver, recurse_t > void submit (ownornest_t, SingleReceiver awhat)
 

Private Types

using error_type = std::decay_t< E >
 
using work_type = any_receiver< error_type, any_executor_ref< error_type >>
 
using queue_type = std::deque< work_type >
 
using pending_type = std::tuple< int, queue_type, bool >
 

Static Private Member Functions

static pending_type *& owner ()
 
static int & depth (pending_type &p)
 
static queue_typepending (pending_type &p)
 
static bool & repeat (pending_type &p)
 

Detailed Description

template<class E>
class folly::pushmi::detail::trampoline< E >

Definition at line 50 of file trampoline.h.

Member Typedef Documentation

template<class E >
using folly::pushmi::detail::trampoline< E >::error_type = std::decay_t<E>
private

Definition at line 99 of file trampoline.h.

template<class E >
using folly::pushmi::detail::trampoline< E >::pending_type = std::tuple<int, queue_type, bool>
private

Definition at line 102 of file trampoline.h.

template<class E >
using folly::pushmi::detail::trampoline< E >::queue_type = std::deque<work_type>
private

Definition at line 101 of file trampoline.h.

Definition at line 100 of file trampoline.h.

Member Function Documentation

template<class E >
static int& folly::pushmi::detail::trampoline< E >::depth ( pending_type p)
inlinestaticprivate

Definition at line 109 of file trampoline.h.

109  {
110  return std::get<0>(p);
111  }
template<class E >
static trampoline_id folly::pushmi::detail::trampoline< E >::get_id ( )
inlinestatic

Definition at line 122 of file trampoline.h.

Referenced by folly::pushmi::get_trampoline_id().

122  {
123  return {owner()};
124  }
static pending_type *& owner()
Definition: trampoline.h:104
template<class E >
static bool folly::pushmi::detail::trampoline< E >::is_owned ( )
inlinestatic

Definition at line 126 of file trampoline.h.

Referenced by folly::pushmi::owned_by_trampoline().

126  {
127  return owner() != nullptr;
128  }
static pending_type *& owner()
Definition: trampoline.h:104
template<class E >
static pending_type*& folly::pushmi::detail::trampoline< E >::owner ( )
inlinestaticprivate

Definition at line 104 of file trampoline.h.

104  {
105  static thread_local pending_type* pending = nullptr;
106  return pending;
107  }
static queue_type & pending(pending_type &p)
Definition: trampoline.h:113
std::tuple< int, queue_type, bool > pending_type
Definition: trampoline.h:102
template<class E >
static queue_type& folly::pushmi::detail::trampoline< E >::pending ( pending_type p)
inlinestaticprivate

Definition at line 113 of file trampoline.h.

113  {
114  return std::get<1>(p);
115  }
template<class E >
static bool& folly::pushmi::detail::trampoline< E >::repeat ( pending_type p)
inlinestaticprivate

Definition at line 117 of file trampoline.h.

117  {
118  return std::get<2>(p);
119  }
template<class E >
template<class Selector , class Derived >
static void folly::pushmi::detail::trampoline< E >::submit ( Selector  ,
Derived ,
recurse_t   
)
inlinestatic

Definition at line 131 of file trampoline.h.

References folly::pushmi::PUSHMI_TEMPLATE(), folly::pushmi::repeat(), and folly::pushmi::requires().

131  {
132  if (!is_owned()) {
133  abort();
134  }
135  repeat(*owner()) = true;
136  }
static bool & repeat(pending_type &p)
Definition: trampoline.h:117
static pending_type *& owner()
Definition: trampoline.h:104
template<class E >
requires not static Same<SingleReceiver, recurse_t> void folly::pushmi::detail::trampoline< E >::submit ( ownordelegate_t  ,
SingleReceiver  awhat 
)
inlinestatic

Definition at line 140 of file trampoline.h.

References folly::empty(), folly::gen::move, folly::pushmi::detail::ownornest, folly::pushmi::PUSHMI_TEMPLATE(), folly::pushmi::repeat(), folly::pushmi::requires(), folly::pushmi::set_done, folly::pushmi::set_error, folly::pushmi::set_value, and submit.

142  {
143  delegator<E> that;
144 
145  if (is_owned()) {
146  // thread already owned
147 
148  // poor mans scope guard
149  try {
150  if (++depth(*owner()) > 100) {
151  // defer work to owner
152  pending(*owner()).push_back(work_type{std::move(awhat)});
153  } else {
154  // dynamic recursion - optimization to balance queueing and
155  // stack usage and value interleaving on the same thread.
156  set_value(awhat, that);
157  set_done(awhat);
158  }
159  } catch (...) {
160  --depth(*owner());
161  throw;
162  }
163  --depth(*owner());
164  return;
165  }
166 
167  // take over the thread
168 
169  pending_type pending_store;
170  owner() = &pending_store;
171  depth(pending_store) = 0;
172  repeat(pending_store) = false;
173  // poor mans scope guard
174  try {
176  } catch (...) {
177  // ignore exceptions while delivering the exception
178  try {
179  set_error(awhat, std::current_exception());
180  for (auto& what : pending(pending_store)) {
181  set_error(what, std::current_exception());
182  }
183  } catch (...) {
184  }
185  pending(pending_store).clear();
186 
187  if (!is_owned()) {
188  std::abort();
189  }
190  if (!pending(pending_store).empty()) {
191  std::abort();
192  }
193  owner() = nullptr;
194  throw;
195  }
196  if (!is_owned()) {
197  std::abort();
198  }
199  if (!pending(pending_store).empty()) {
200  std::abort();
201  }
202  owner() = nullptr;
203  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PUSHMI_INLINE_VAR constexpr __adl::set_error_fn set_error
static void submit(Selector, Derived &, recurse_t)
Definition: trampoline.h:131
static int & depth(pending_type &p)
Definition: trampoline.h:109
any_receiver< error_type, any_executor_ref< error_type >> work_type
Definition: trampoline.h:100
static queue_type & pending(pending_type &p)
Definition: trampoline.h:113
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
static bool & repeat(pending_type &p)
Definition: trampoline.h:117
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::ownornest_t ownornest
PUSHMI_INLINE_VAR constexpr __adl::set_value_fn set_value
std::tuple< int, queue_type, bool > pending_type
Definition: trampoline.h:102
static pending_type *& owner()
Definition: trampoline.h:104
PUSHMI_INLINE_VAR constexpr __adl::set_done_fn set_done
template<class E >
requires not static Same<SingleReceiver, recurse_t> void folly::pushmi::detail::trampoline< E >::submit ( ownornest_t  ,
SingleReceiver  awhat 
)
inlinestatic

Definition at line 207 of file trampoline.h.

References folly::empty(), folly::gen::move, folly::pushmi::detail::ownordelegate, folly::pushmi::repeat(), folly::pushmi::set_done, folly::pushmi::set_value, and submit.

209  {
210  delegator<E> that;
211 
212  if (!is_owned()) {
214  return;
215  }
216 
217  auto& pending_store = *owner();
218 
219  // static recursion - tail call optimization
220  if (pending(pending_store).empty()) {
221  bool go = true;
222  while (go) {
223  repeat(pending_store) = false;
224  set_value(awhat, that);
225  set_done(awhat);
226  go = repeat(pending_store);
227  }
228  } else {
229  pending(pending_store).push_back(work_type{std::move(awhat)});
230  }
231 
232  if (pending(pending_store).empty()) {
233  return;
234  }
235 
236  while (!pending(pending_store).empty()) {
237  auto what = std::move(pending(pending_store).front());
238  pending(pending_store).pop_front();
239  set_value(what, any_executor_ref<error_type>{that});
240  set_done(what);
241  }
242  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static void submit(Selector, Derived &, recurse_t)
Definition: trampoline.h:131
any_receiver< error_type, any_executor_ref< error_type >> work_type
Definition: trampoline.h:100
static queue_type & pending(pending_type &p)
Definition: trampoline.h:113
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
static bool & repeat(pending_type &p)
Definition: trampoline.h:117
PUSHMI_INLINE_VAR constexpr __adl::set_value_fn set_value
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::ownordelegate_t ownordelegate
static pending_type *& owner()
Definition: trampoline.h:104
PUSHMI_INLINE_VAR constexpr __adl::set_done_fn set_done

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