Seastar
High performance C++ framework for concurrent servers
Classes | Typedefs | Enumerations | Functions
Future Utilities

These utilities are provided to help perform operations on futures. More...

Classes

struct  repeat_until_value_type_helper< future< std::experimental::optional< T > > >
 Type helper for repeat_until_value() More...
 
struct  reducer_with_get_traits< T, IsFuture >
 
struct  reducer_with_get_traits< T, false >
 
struct  reducer_with_get_traits< T, true >
 
struct  reducer_traits< T, V >
 
struct  reducer_traits< T, decltype(std::declval< T >().get(), void())>
 
class  adder< Result, Addend >
 
struct  is_future< T >
 Check whether a type is a future. More...
 
struct  futurize< T >
 Converts a type to a future type, if it isn't already. More...
 

Typedefs

template<typename AsyncAction >
using repeat_until_value_return_type = typename repeat_until_value_type_helper< std::result_of_t< AsyncAction()>>::future_type
 Return value of repeat_until_value()
 
template<typename T >
using futurize_t = typename futurize< T >::type
 

Enumerations

enum  stop_iteration { no, yes }
 

Functions

template<typename T , typename F >
auto do_with (T &&rvalue, F &&f)
 
template<typename Lock , typename Func >
auto with_lock (Lock &lock, Func &&func)
 
template<typename T1 , typename T2 , typename T3_or_F , typename... More>
auto do_with (T1 &&rv1, T2 &&rv2, T3_or_F &&rv3, More &&...more)
 
template<typename Iterator , typename Func >
future parallel_for_each (Iterator begin, Iterator end, Func &&func)
 
template<typename Range , typename Func >
future parallel_for_each (Range &&range, Func &&func)
 
template<typename AsyncAction >
static future repeat (AsyncAction &&action)
 
template<typename AsyncAction >
repeat_until_value_return_type< AsyncAction > repeat_until_value (AsyncAction &&action)
 
template<typename AsyncAction , typename StopCondition >
static future do_until (StopCondition &&stop_cond, AsyncAction &&action)
 
template<typename AsyncAction >
static future keep_doing (AsyncAction &&action)
 
template<typename Iterator , typename AsyncAction >
static future do_for_each (Iterator begin, Iterator end, AsyncAction &&action)
 
template<typename Container , typename AsyncAction >
static future do_for_each (Container &c, AsyncAction &&action)
 
template<typename... Futs>
future< std::tuple< Futs... > > when_all (Futs &&...futs)
 
template<typename FutureIterator >
future< std::vector< typename std::iterator_traits< FutureIterator >::value_type > > when_all (FutureIterator begin, FutureIterator end)
 
template<typename Iterator , typename Mapper , typename Reducer >
auto map_reduce (Iterator begin, Iterator end, Mapper &&mapper, Reducer &&r) -> typename reducer_traits< Reducer >::future_type
 
template<typename Iterator , typename Mapper , typename Initial , typename Reduce >
future< Initial > map_reduce (Iterator begin, Iterator end, Mapper &&mapper, Initial initial, Reduce reduce)
 
template<typename Range , typename Mapper , typename Initial , typename Reduce >
future< Initial > map_reduce (Range &&range, Mapper &&mapper, Initial initial, Reduce reduce)
 
static future now ()
 
future later ()
 

Detailed Description

These utilities are provided to help perform operations on futures.


Class Documentation

struct repeat_until_value_type_helper< future< std::experimental::optional< T > > >

template<typename T>
struct repeat_until_value_type_helper< future< std::experimental::optional< T > > >

Type helper for repeat_until_value()

Class Members
typedef future< optional_type > future_optional_type Return type of AsyncAction.
typedef future< value_type > future_type Return type of repeat_until_value()
typedef optional< T > optional_type Type used by AsyncAction while looping.
typedef T value_type The type of the value we are computing.
struct reducer_with_get_traits

template<typename T, bool IsFuture>
struct reducer_with_get_traits< T, IsFuture >

Function Documentation

template<typename Iterator , typename AsyncAction >
static future do_for_each ( Iterator  begin,
Iterator  end,
AsyncAction &&  action 
)
inlinestatic

Call a function for each item in a range, sequentially (iterator version).

For each item in a range, call a function, waiting for the previous invocation to complete before calling the next one.

Parameters
beginan InputIterator designating the beginning of the range
endan InputIterator designating the endof the range
actiona callable, taking a reference to objects from the range as a parameter, and returning a future<> that resolves when it is acceptable to process the next item.
Returns
a ready future on success, or the first failed future if action failed.
template<typename Container , typename AsyncAction >
static future do_for_each ( Container &  c,
AsyncAction &&  action 
)
inlinestatic

Call a function for each item in a range, sequentially (range version).

For each item in a range, call a function, waiting for the previous invocation to complete before calling the next one.

Parameters
rangean Range object designating input values
actiona callable, taking a reference to objects from the range as a parameter, and returning a future<> that resolves when it is acceptable to process the next item.
Returns
a ready future on success, or the first failed future if action failed.
template<typename AsyncAction , typename StopCondition >
static future do_until ( StopCondition &&  stop_cond,
AsyncAction &&  action 
)
inlinestatic

Invokes given action until it fails or given condition evaluates to true.

Parameters
stop_conda callable taking no arguments, returning a boolean that evalutes to true when you don't want to call action any longer
actiona callable taking no arguments, returning a future<>. Will be called again as soon as the future resolves, unless the future fails, or stop_cond returns true.
Returns
a ready future if we stopped successfully, or a failed future if a call to to action failed.
template<typename T , typename F >
auto do_with ( T &&  rvalue,
F &&  f 
)
inline

do_with() holds an object alive for the duration until a future completes, and allow the code involved in making the future complete to have easy access to this object.

do_with() takes two arguments: The first is an temporary object (rvalue), the second is a function returning a future (a so-called "promise"). The function is given (a moved copy of) this temporary object, by reference, and it is ensured that the object will not be destructed until the completion of the future returned by the function.

do_with() returns a future which resolves to whatever value the given future (returned by the given function) resolves to. This returned value must not contain references to the temporary object, as at that point the temporary is destructed.

Parameters
rvaluea temporary value to protect while f is running
fa callable, accepting an lvalue reference of the same type as rvalue, that will be accessible while f runs
Returns
whatever f returns
template<typename T1 , typename T2 , typename T3_or_F , typename... More>
auto do_with ( T1 &&  rv1,
T2 &&  rv2,
T3_or_F &&  rv3,
More &&...  more 
)
inline

Multiple argument variant of do_with(T&& rvalue, F&& f).

This is the same as do_with(T&& tvalue, F&& f), but accepts two or more rvalue parameters, which are held in memory while f executes. f will be called with all arguments as reference parameters.

template<typename AsyncAction >
static future keep_doing ( AsyncAction &&  action)
inlinestatic

Invoke given action until it fails.

Calls action repeatedly until it returns a failed future.

Parameters
actiona callable taking no arguments, returning a future<> that becomes ready when you wish it to be called again.
Returns
a future<> that will resolve to the first failure of action
template<typename Iterator , typename Mapper , typename Initial , typename Reduce >
future<Initial> map_reduce ( Iterator  begin,
Iterator  end,
Mapper &&  mapper,
Initial  initial,
Reduce  reduce 
)
inline

Asynchronous map/reduce transformation.

Given a range of objects, an asynchronous unary function operating on these objects, an initial value, and a binary function for reducing, map_reduce() will transform each object in the range, then apply the the reducing function to the result.

Example:

Calculate the total size of several files:

1 map_reduce(files.begin(), files.end(),
2  std::mem_fn(file::size),
3  size_t(0),
4  std::plus<size_t>())

Requirements:

  • Iterator: an InputIterator.
  • Mapper: unary function taking Iterator::value_type and producing a future<...>.
  • Initial: any value type
  • Reduce: a binary function taking two Initial values and returning an Initial

Return type:

  • future<Initial>
Parameters
beginbeginning of object range to operate on
endend of object range to operate on
mappermap function to call on each object, returning a future
initialinitial input value to reduce function
reducebinary function for merging two result values from mapper
Returns
equivalent to reduce(reduce(initial, mapper(obj0)), mapper(obj1)) ...
template<typename Range , typename Mapper , typename Initial , typename Reduce >
future<Initial> map_reduce ( Range &&  range,
Mapper &&  mapper,
Initial  initial,
Reduce  reduce 
)
inline

Asynchronous map/reduce transformation (range version).

Given a range of objects, an asynchronous unary function operating on these objects, an initial value, and a binary function for reducing, map_reduce() will transform each object in the range, then apply the the reducing function to the result.

Example:

Calculate the total size of several files:

1 std::vector<file> files = ...;
2 map_reduce(files,
3  std::mem_fn(file::size),
4  size_t(0),
5  std::plus<size_t>())

Requirements:

  • Iterator: an InputIterator.
  • Mapper: unary function taking Iterator::value_type and producing a future<...>.
  • Initial: any value type
  • Reduce: a binary function taking two Initial values and returning an Initial

Return type:

  • future<Initial>
Parameters
rangeobject range to operate on
mappermap function to call on each object, returning a future
initialinitial input value to reduce function
reducebinary function for merging two result values from mapper
Returns
equivalent to reduce(reduce(initial, mapper(obj0)), mapper(obj1)) ...
template<typename Iterator , typename Func >
future parallel_for_each ( Iterator  begin,
Iterator  end,
Func &&  func 
)
inline

Run tasks in parallel (iterator version).

Given a range [begin, end) of objects, run func on each *i in the range, and return a future<> that resolves when all the functions complete. func should return a future<> that indicates when it is complete. All invocations are performed in parallel.

Parameters
beginan InputIterator designating the beginning of the range
endan InputIterator designating the end of the range
funcFunction to apply to each element in the range (returning a future<>)
Returns
a future<> that resolves when all the function invocations complete. If one or more return an exception, the return value contains one of the exceptions.
template<typename Range , typename Func >
future parallel_for_each ( Range &&  range,
Func &&  func 
)
inline

Run tasks in parallel (range version).

Given a range of objects, apply func to each object in the range, and return a future<> that resolves when all the functions complete. func should return a future<> that indicates when it is complete. All invocations are performed in parallel.

Parameters
rangeA range of objects to iterate run func on
funcA callable, accepting reference to the range's value_type, and returning a future<>.
Returns
a future<> that becomes ready when the entire range was processed. If one or more of the invocations of func returned an exceptional future, then the return value will contain one of those exceptions.
template<typename AsyncAction >
static future repeat ( AsyncAction &&  action)
inlinestatic

Invokes given action until it fails or the function requests iteration to stop by returning stop_iteration::yes.

Parameters
actiona callable taking no arguments, returning a future<stop_iteration>. Will be called again as soon as the future resolves, unless the future fails, action throws, or it resolves with stop_iteration::yes. If action is an r-value it can be moved in the middle of iteration.
Returns
a ready future if we stopped successfully, or a failed future if a call to to action failed.
template<typename AsyncAction >
repeat_until_value_return_type<AsyncAction> repeat_until_value ( AsyncAction &&  action)

Invokes given action until it fails or the function requests iteration to stop by returning an engaged future<std::experimental::optional<T>>. The value is extracted from the optional, and returned, as a future, from repeat_until_value().

Parameters
actiona callable taking no arguments, returning a future<std::experimental::optional<T>>. Will be called again as soon as the future resolves, unless the future fails, action throws, or it resolves with an engaged optional. If action is an r-value it can be moved in the middle of iteration.
Returns
a ready future if we stopped successfully, or a failed future if a call to to action failed. The optional's value is returned.
template<typename... Futs>
future<std::tuple<Futs...> > when_all ( Futs &&...  futs)
inline

Wait for many futures to complete, capturing possible errors (variadic version).

Given a variable number of futures as input, wait for all of them to resolve (either successfully or with an exception), and return them as a tuple so individual values or exceptions can be examined.

Parameters
futsfutures to wait for
Returns
an std::tuple<> of all the futures in the input; when ready, all contained futures will be ready as well.
template<typename FutureIterator >
future<std::vector<typename std::iterator_traits<FutureIterator>::value_type> > when_all ( FutureIterator  begin,
FutureIterator  end 
)
inline

Wait for many futures to complete, capturing possible errors (iterator version).

Given a range of futures as input, wait for all of them to resolve (either successfully or with an exception), and return them as a std::vector so individual values or exceptions can be examined.

Parameters
beginan InputIterator designating the beginning of the range of futures
endan InputIterator designating the end of the range of futures
Returns
an std::vector<> of all the futures in the input; when ready, all contained futures will be ready as well.
template<typename Lock , typename Func >
auto with_lock ( Lock &  lock,
Func &&  func 
)
inline

Executes the function func making sure the lock lock is taken, and later on properly released.

Parameters
lockthe lock, which is any object having providing a lock() / unlock() semantics. Caller must make sure that it outlives func.
funcfunction to be executed
Returns
whatever func returns