X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ffutures%2Fhelpers.h;h=9b5e85ba1f0ac96ede79a336389f34faa9645e63;hb=98c488e23b44105d0e1a5b1d5cd7dea10b5843d5;hp=c39d6686f01f8e6360e9627b54133bc98944ca4b;hpb=7f413ec31d3c7ad9cde6f5fe4ec06d9ee6b758b2;p=folly.git diff --git a/folly/futures/helpers.h b/folly/futures/helpers.h index c39d6686..9b5e85ba 100644 --- a/folly/futures/helpers.h +++ b/folly/futures/helpers.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,60 @@ */ #pragma once +#include +#include +#include + +#include +#include #include +#include namespace folly { +namespace futures { +namespace detail { +template +struct CollectAllVariadicContext { + CollectAllVariadicContext() {} + template + inline void setPartialResult(Try& t) { + std::get(results) = std::move(t); + } + ~CollectAllVariadicContext() { + p.setValue(std::move(results)); + } + Promise...>> p; + std::tuple...> results; + typedef Future...>> type; +}; + +template +struct CollectVariadicContext { + CollectVariadicContext() {} + template + inline void setPartialResult(Try& t) { + if (t.hasException()) { + if (!threw.exchange(true)) { + p.setException(std::move(t.exception())); + } + } else if (!threw) { + std::get(results) = std::move(t); + } + } + ~CollectVariadicContext() noexcept { + if (!threw.exchange(true)) { + p.setValue(unwrapTryTuple(std::move(results))); + } + } + Promise> p; + std::tuple...> results; + std::atomic threw{false}; + typedef Future> type; +}; +} // namespace detail +} // namespace futures + /// This namespace is for utility functions that would usually be static /// members of Future, except they don't make sense there because they don't /// depend on the template type (rather, on the type of their arguments in @@ -37,29 +87,18 @@ namespace futures { /// The Timekeeper thread will be lazily created the first time it is /// needed. If your program never uses any timeouts or other time-based /// Futures you will pay no Timekeeper thread overhead. - Future sleep(Duration, Timekeeper* = nullptr); - - /// Create a Future chain from a sequence of callbacks. i.e. - /// - /// f.then(a).then(b).then(c); - /// - /// where f is a Future and the result of the chain is a Future - /// becomes - /// - /// f.then(chain(a, b, c)); - // If anyone figures how to get chain to deduce A and Z, I'll buy you a drink. - template - std::function(Try)> - chain(Callbacks... fns); + Future sleep(Duration, Timekeeper* = nullptr); /** * Set func as the callback for each input Future and return a vector of * Futures containing the results in the input order. */ - template ::value_type, - class Result - = typename decltype(std::declval().then(std::declval()))::value_type> + template < + class It, + class F, + class ItT = typename std::iterator_traits::value_type, + class Result = typename decltype( + std::declval().then(std::declval()))::value_type> std::vector> map(It first, It last, F func); // Sugar for the most common case @@ -69,7 +108,74 @@ namespace futures { return map(c.begin(), c.end(), std::forward(func)); } -} +} // namespace futures + +/** + Make a completed SemiFuture by moving in a value. e.g. + + string foo = "foo"; + auto f = makeSemiFuture(std::move(foo)); + + or + + auto f = makeSemiFuture("foo"); +*/ +template +SemiFuture::type> makeSemiFuture(T&& t); + +/** Make a completed void SemiFuture. */ +SemiFuture makeSemiFuture(); + +/** + Make a SemiFuture by executing a function. + + If the function returns a value of type T, makeSemiFutureWith + returns a completed SemiFuture, capturing the value returned + by the function. + + If the function returns a SemiFuture already, makeSemiFutureWith + returns just that. + + Either way, if the function throws, a failed Future is + returned that captures the exception. +*/ + +// makeSemiFutureWith(SemiFuture()) -> SemiFuture +template +typename std::enable_if::type>::value, + typename std::result_of::type>::type +makeSemiFutureWith(F&& func); + +// makeSemiFutureWith(T()) -> SemiFuture +// makeSemiFutureWith(void()) -> SemiFuture +template +typename std::enable_if< + !(isSemiFuture::type>::value), + SemiFuture::type>::type>>::type +makeSemiFutureWith(F&& func); + +/// Make a failed Future from an exception_ptr. +/// Because the Future's type cannot be inferred you have to specify it, e.g. +/// +/// auto f = makeSemiFuture(std::current_exception()); +template +FOLLY_DEPRECATED("use makeSemiFuture(exception_wrapper)") +SemiFuture makeSemiFuture(std::exception_ptr const& e); + +/// Make a failed SemiFuture from an exception_wrapper. +template +SemiFuture makeSemiFuture(exception_wrapper ew); + +/** Make a SemiFuture from an exception type E that can be passed to + std::make_exception_ptr(). */ +template +typename std::enable_if::value, + SemiFuture>::type +makeSemiFuture(E const& e); + +/** Make a Future out of a Try */ +template +SemiFuture makeSemiFuture(Try&& t); /** Make a completed Future by moving in a value. e.g. @@ -85,28 +191,46 @@ template Future::type> makeFuture(T&& t); /** Make a completed void Future. */ -Future makeFuture(); +Future makeFuture(); + +/** + Make a Future by executing a function. + + If the function returns a value of type T, makeFutureWith + returns a completed Future, capturing the value returned + by the function. + + If the function returns a Future already, makeFutureWith + returns just that. -/** Make a completed Future by executing a function. If the function throws - we capture the exception, otherwise we capture the result. */ + Either way, if the function throws, a failed Future is + returned that captures the exception. + + Calling makeFutureWith(func) is equivalent to calling + makeFuture().then(func). +*/ + +// makeFutureWith(Future()) -> Future template -auto makeFutureWith( - F&& func, - typename std::enable_if< - !std::is_reference::value, bool>::type sdf = false) - -> Future; +typename std::enable_if::type>::value, + typename std::result_of::type>::type +makeFutureWith(F&& func); +// makeFutureWith(T()) -> Future +// makeFutureWith(void()) -> Future template -auto makeFutureWith( - F const& func) - -> Future; +typename std::enable_if< + !(isFuture::type>::value), + Future::type>::type>>::type +makeFutureWith(F&& func); /// Make a failed Future from an exception_ptr. /// Because the Future's type cannot be inferred you have to specify it, e.g. /// /// auto f = makeFuture(std::current_exception()); template -Future makeFuture(std::exception_ptr const& e) DEPRECATED; +FOLLY_DEPRECATED("use makeFuture(exception_wrapper)") +Future makeFuture(std::exception_ptr const& e); /// Make a failed Future from an exception_wrapper. template @@ -128,11 +252,21 @@ Future makeFuture(Try&& t); * This is just syntactic sugar for makeFuture().via(executor) * * @param executor the Executor to call back on + * @param priority optionally, the priority to add with. Defaults to 0 which + * represents medium priority. * * @returns a void Future that will call back on the given executor */ -template -Future via(Executor* executor); +inline Future via( + Executor* executor, + int8_t priority = Executor::MID_PRI); + +/// Execute a function via the given executor and return a future. +/// This is semantically equivalent to via(executor).then(func), but +/// easier to read and slightly more efficient. +template +auto via(Executor*, Func&& func) + -> Future()())>::Inner>; /** When all the input Futures complete, the returned Future will complete. Errors do not cause early termination; this Future will always succeed @@ -165,17 +299,16 @@ auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) { /// is a Future, Try, ...>>. /// The Futures are moved in, so your copies are invalid. template -typename detail::VariadicContext< - typename std::decay::type::value_type...>::type +typename futures::detail::CollectAllVariadicContext< + typename std::decay::type::value_type...>::type collectAll(Fs&&... fs); /// Like collectAll, but will short circuit on the first exception. Thus, the /// type of the returned Future is std::vector instead of /// std::vector> template -Future::value_type::value_type ->::result_type> +Future::value_type::value_type>::result_type> collect(InputIterator first, InputIterator last); /// Sugar for the most common case @@ -184,6 +317,14 @@ auto collect(Collection&& c) -> decltype(collect(c.begin(), c.end())) { return collect(c.begin(), c.end()); } +/// Like collectAll, but will short circuit on the first exception. Thus, the +/// type of the returned Future is std::tuple instead of +/// std::tuple, Try, ...> +template +typename futures::detail::CollectVariadicContext< + typename std::decay::type::value_type...>::type +collect(Fs&&... fs); + /** The result is a pair of the index of the first Future to complete and the Try. If multiple Futures complete at the same time (or are already complete when passed in), the "winner" is chosen non-deterministically. @@ -202,6 +343,23 @@ auto collectAny(Collection&& c) -> decltype(collectAny(c.begin(), c.end())) { return collectAny(c.begin(), c.end()); } +/** Similar to collectAny, collectAnyWithoutException return the first Future to + * complete without exceptions. If none of the future complete without + * excpetions, the last exception will be returned as a result. + */ +template +Future::value_type::value_type>> +collectAnyWithoutException(InputIterator first, InputIterator last); + +/// Sugar for the most common case +template +auto collectAnyWithoutException(Collection&& c) + -> decltype(collectAnyWithoutException(c.begin(), c.end())) { + return collectAnyWithoutException(c.begin(), c.end()); +} + /** when n Futures have completed, the Future completes with a vector of the index and Try of those n Futures (the indices refer to the original order, but the result vector will be in an arbitrary order) @@ -221,11 +379,38 @@ auto collectN(Collection&& c, size_t n) return collectN(c.begin(), c.end(), n); } +/** window creates up to n Futures using the values + in the collection, and then another Future for each Future + that completes + + this is basically a sliding window of Futures of size n + + func must return a Future for each value in input + */ +template < + class Collection, + class F, + class ItT = typename std::iterator_traits< + typename Collection::iterator>::value_type, + class Result = typename futures::detail::resultOf::value_type> +std::vector> window(Collection input, F func, size_t n); + +template < + class Collection, + class F, + class ItT = typename std::iterator_traits< + typename Collection::iterator>::value_type, + class Result = typename futures::detail::resultOf::value_type> +std::vector> +window(Executor* executor, Collection input, F func, size_t n); + template using MaybeTryArg = typename std::conditional< - detail::callableWith&&>::value, Try, ItT>::type; + futures::detail::callableWith&&>::value, + Try, + ItT>::type; -template +template using isFutureResult = isFuture::type>; /** repeatedly calls func on every result, e.g. @@ -234,23 +419,18 @@ using isFutureResult = isFuture::type>; The type of the final result is a Future of the type of the initial value. Func can either return a T, or a Future + + func is called in order of the input, see unorderedReduce if that is not + a requirement */ -template ::value_type::value_type, - class Arg = MaybeTryArg> -typename std::enable_if::value, Future>::type -reduce(It first, It last, T initial, F func); - -template ::value_type::value_type, - class Arg = MaybeTryArg> -typename std::enable_if::value, Future>::type -reduce(It first, It last, T initial, F func); +template +Future reduce(It first, It last, T&& initial, F&& func); /// Sugar for the most common case template auto reduce(Collection&& c, T&& initial, F&& func) - -> decltype(reduce(c.begin(), c.end(), initial, func)) { + -> decltype(reduce(c.begin(), c.end(), std::forward(initial), + std::forward(func))) { return reduce( c.begin(), c.end(), @@ -258,4 +438,26 @@ auto reduce(Collection&& c, T&& initial, F&& func) std::forward(func)); } +/** like reduce, but calls func on finished futures as they complete + does NOT keep the order of the input + */ +template < + class It, + class T, + class F, + class ItT = typename std::iterator_traits::value_type::value_type, + class Arg = MaybeTryArg> +Future unorderedReduce(It first, It last, T initial, F func); + +/// Sugar for the most common case +template +auto unorderedReduce(Collection&& c, T&& initial, F&& func) + -> decltype(unorderedReduce(c.begin(), c.end(), std::forward(initial), + std::forward(func))) { + return unorderedReduce( + c.begin(), + c.end(), + std::forward(initial), + std::forward(func)); +} } // namespace folly