proxygen
folly::fibers::TaskIterator< T > Class Template Reference

#include <AddTasks.h>

Classes

struct  Context
 

Public Types

typedef T value_type
 

Public Member Functions

 TaskIterator ()
 
 TaskIterator (const TaskIterator &other)=delete
 
TaskIteratoroperator= (const TaskIterator &other)=delete
 
 TaskIterator (TaskIterator &&other) noexcept
 
TaskIteratoroperator= (TaskIterator &&other)=delete
 
template<typename F >
void addTask (F &&func)
 
bool hasCompleted () const
 
bool hasPending () const
 
bool hasNext () const
 
T awaitNext ()
 
void reserve (size_t n)
 
size_t getTaskID () const
 
template<>
void awaitNext ()
 

Private Member Functions

folly::Try< TawaitNextResult ()
 

Private Attributes

std::shared_ptr< Contextcontext_ {std::make_shared<Context>()}
 
size_t id_ {std::numeric_limits<size_t>::max()}
 
FiberManagerfm_
 

Friends

template<class InputIterator >
TaskIterator< invoke_result_t< typename std::iterator_traits< InputIterator >::value_type > > addTasks (InputIterator first, InputIterator last)
 

Detailed Description

template<typename T>
class folly::fibers::TaskIterator< T >

Definition at line 30 of file AddTasks.h.

Member Typedef Documentation

template<typename T>
typedef T folly::fibers::TaskIterator< T >::value_type

Definition at line 51 of file AddTasks.h.

Constructor & Destructor Documentation

template<typename T>
folly::fibers::TaskIterator< T >::TaskIterator ( const TaskIterator< T > &  other)
delete
template<typename T >
folly::fibers::TaskIterator< T >::TaskIterator ( TaskIterator< T > &&  other)
noexcept

Definition at line 23 of file AddTasks-inl.h.

24  : context_(std::move(other.context_)), id_(other.id_), fm_(other.fm_) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< Context > context_
Definition: AddTasks.h:125

Member Function Documentation

template<typename T >
template<typename F >
void folly::fibers::TaskIterator< T >::addTask ( F &&  func)

Add one more task to the TaskIterator.

Parameters
functask to be added, will be scheduled on current FiberManager

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

References context, folly::makeTryWith(), folly::gen::move, folly::T, and folly::value().

Referenced by folly::fibers::addTasks(), folly::fibers::TaskIterator< T >::TaskIterator(), and TEST().

89  {
90  static_assert(
91  std::is_convertible<invoke_result_t<F>, T>::value,
92  "TaskIterator<T>: T must be convertible from func()'s return type");
93 
94  auto taskId = context_->totalTasks++;
95 
96  fm_.addTask(
97  [taskId, context = context_, func = std::forward<F>(func)]() mutable {
98  context->results.emplace_back(
99  taskId, folly::makeTryWith(std::move(func)));
100 
101  // Check for awaiting iterator.
102  if (context->promise.hasValue()) {
103  if (--context->tasksToFulfillPromise == 0) {
104  context->promise->setValue();
105  context->promise.clear();
106  }
107  }
108  });
109 }
context
Definition: CMakeCache.txt:563
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::std 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
std::shared_ptr< Context > context_
Definition: AddTasks.h:125
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
template<>
void folly::fibers::TaskIterator< void >::awaitNext ( )
inline

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

57  {
58  awaitNextResult().value();
59 }
folly::Try< T > awaitNextResult()
Definition: AddTasks-inl.h:42
template<typename T >
T folly::fibers::TaskIterator< T >::awaitNext ( )
inline

Await for another task to complete. Will not await if the result is already available.

Returns
result of the task completed.
Exceptions
exceptionthrown by the task.

Definition at line 52 of file AddTasks-inl.h.

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

Referenced by folly::fibers::TaskIterator< T >::TaskIterator(), and TEST().

52  {
53  return std::move(awaitNextResult().value());
54 }
folly::Try< T > awaitNextResult()
Definition: AddTasks-inl.h:42
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
template<typename T >
folly::Try< T > folly::fibers::TaskIterator< T >::awaitNextResult ( )
private

Definition at line 42 of file AddTasks-inl.h.

References i, folly::gen::move, and folly::reserve.

42  {
43  assert(hasCompleted() || hasPending());
44  reserve(1);
45 
46  size_t i = context_->tasksConsumed++;
47  id_ = context_->results[i].first;
48  return std::move(context_->results[i].second);
49 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< Context > context_
Definition: AddTasks.h:125
template<typename T >
size_t folly::fibers::TaskIterator< T >::getTaskID ( ) const
inline
Returns
id of the last task that was processed by awaitNext().

Definition at line 82 of file AddTasks-inl.h.

Referenced by folly::fibers::TaskIterator< T >::TaskIterator().

82  {
83  assert(id_ != static_cast<size_t>(-1));
84  return id_;
85 }
template<typename T >
bool folly::fibers::TaskIterator< T >::hasCompleted ( ) const
inline
Returns
True if there are tasks immediately available to be consumed (no need to await on them).

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

Referenced by folly::fibers::TaskIterator< T >::TaskIterator().

27  {
28  return context_->tasksConsumed < context_->results.size();
29 }
std::shared_ptr< Context > context_
Definition: AddTasks.h:125
template<typename T >
bool folly::fibers::TaskIterator< T >::hasNext ( ) const
inline
Returns
True if there are any tasks (hasCompleted() || hasPending()).

Definition at line 37 of file AddTasks-inl.h.

Referenced by folly::fibers::TaskIterator< T >::TaskIterator().

37  {
38  return hasPending() || hasCompleted();
39 }
template<typename T >
bool folly::fibers::TaskIterator< T >::hasPending ( ) const
inline
Returns
True if there are tasks pending execution (need to awaited on).

Definition at line 32 of file AddTasks-inl.h.

Referenced by folly::fibers::TaskIterator< T >::TaskIterator().

32  {
33  return !context_.unique();
34 }
std::shared_ptr< Context > context_
Definition: AddTasks.h:125
template<typename T>
TaskIterator& folly::fibers::TaskIterator< T >::operator= ( const TaskIterator< T > &  other)
delete
template<typename T>
TaskIterator& folly::fibers::TaskIterator< T >::operator= ( TaskIterator< T > &&  other)
delete
template<typename T >
void folly::fibers::TaskIterator< T >::reserve ( size_t  n)
inline

Await until the specified number of tasks completes or there are no tasks left to await for. Note: Will not await if there are already the specified number of tasks available.

Parameters
nNumber of tasks to await for completition.

Definition at line 62 of file AddTasks-inl.h.

References folly::fibers::await(), min, and folly::gen::move.

Referenced by folly::fibers::TaskIterator< T >::TaskIterator().

62  {
63  size_t tasksReady = context_->results.size() - context_->tasksConsumed;
64 
65  // we don't need to do anything if there are already n or more tasks complete
66  // or if we have no tasks left to execute.
67  if (!hasPending() || tasksReady >= n) {
68  return;
69  }
70 
71  n -= tasksReady;
72  size_t tasksLeft = context_->totalTasks - context_->results.size();
73  n = std::min(n, tasksLeft);
74 
75  await([this, n](Promise<void> promise) {
76  context_->tasksToFulfillPromise = n;
77  context_->promise.assign(std::move(promise));
78  });
79 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
LogLevel min
Definition: LogLevel.cpp:30
std::shared_ptr< Context > context_
Definition: AddTasks.h:125
FirstArgOf< F >::type::value_type await(F &&func)

Friends And Related Function Documentation

template<typename T>
template<class InputIterator >
TaskIterator< invoke_result_t<typename std::iterator_traits<InputIterator>::value_type> > addTasks ( InputIterator  first,
InputIterator  last 
)
friend

Schedules several tasks and immediately returns an iterator, that allow one to traverse tasks in the order of their completion. All results and exceptions thrown are stored alongside with the task id and are accessible via iterator.

Parameters
firstRange of tasks to be scheduled
last
Returns
movable, non-copyable iterator

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

Referenced by folly::fibers::TaskIterator< T >::TaskIterator().

114  {
115  typedef invoke_result_t<
116  typename std::iterator_traits<InputIterator>::value_type>
117  ResultType;
118  typedef TaskIterator<ResultType> IteratorType;
119 
120  IteratorType iterator;
121 
122  for (; first != last; ++first) {
123  iterator.addTask(std::move(*first));
124  }
125 
126  iterator.context_->results.reserve(iterator.context_->totalTasks);
127 
128  return std::move(iterator);
129 }
typename invoke_result< F, Args... >::type invoke_result_t
Definition: Invoke.h:142
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr detail::First first
Definition: Base-inl.h:2553

Member Data Documentation

template<typename T>
std::shared_ptr<Context> folly::fibers::TaskIterator< T >::context_ {std::make_shared<Context>()}
private

Definition at line 125 of file AddTasks.h.

template<typename T>
FiberManager& folly::fibers::TaskIterator< T >::fm_
private

Definition at line 127 of file AddTasks.h.

template<typename T>
size_t folly::fibers::TaskIterator< T >::id_ {std::numeric_limits<size_t>::max()}
private

Definition at line 126 of file AddTasks.h.


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