helpers.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <atomic>
  18. #include <tuple>
  19. #include <utility>
  20. #include <folly/Portability.h>
  21. #include <folly/Try.h>
  22. #include <folly/functional/Invoke.h>
  23. #include <folly/futures/Future.h>
  24. #include <folly/futures/Promise.h>
  25. namespace folly {
  26. /// This namespace is for utility functions that would usually be static
  27. /// members of Future, except they don't make sense there because they don't
  28. /// depend on the template type (rather, on the type of their arguments in
  29. /// some cases). This is the least-bad naming scheme we could think of. Some
  30. /// of the functions herein have really-likely-to-collide names, like "map"
  31. /// and "sleep".
  32. namespace futures {
  33. /// Returns a Future that will complete after the specified duration. The
  34. /// Duration typedef of a `std::chrono` duration type indicates the
  35. /// resolution you can expect to be meaningful (milliseconds at the time of
  36. /// writing). Normally you wouldn't need to specify a Timekeeper, we will
  37. /// use the global futures timekeeper (we run a thread whose job it is to
  38. /// keep time for futures timeouts) but we provide the option for power
  39. /// users.
  40. ///
  41. /// The Timekeeper thread will be lazily created the first time it is
  42. /// needed. If your program never uses any timeouts or other time-based
  43. /// Futures you will pay no Timekeeper thread overhead.
  44. Future<Unit> sleep(Duration, Timekeeper* = nullptr);
  45. /**
  46. * Set func as the callback for each input Future and return a vector of
  47. * Futures containing the results in the input order.
  48. */
  49. template <
  50. class It,
  51. class F,
  52. class ItT = typename std::iterator_traits<It>::value_type,
  53. class Result = typename decltype(
  54. std::declval<ItT>().then(std::declval<F>()))::value_type>
  55. std::vector<Future<Result>> map(It first, It last, F func);
  56. /**
  57. * Set func as the callback for each input Future and return a vector of
  58. * Futures containing the results in the input order and completing on
  59. * exec.
  60. */
  61. template <
  62. class It,
  63. class F,
  64. class ItT = typename std::iterator_traits<It>::value_type,
  65. class Result = typename decltype(std::move(std::declval<ItT>())
  66. .via(std::declval<Executor*>())
  67. .then(std::declval<F>()))::value_type>
  68. std::vector<Future<Result>> map(Executor& exec, It first, It last, F func);
  69. // Sugar for the most common case
  70. template <class Collection, class F>
  71. auto map(Collection&& c, F&& func) -> decltype(map(c.begin(), c.end(), func)) {
  72. return map(c.begin(), c.end(), std::forward<F>(func));
  73. }
  74. // Sugar for the most common case
  75. template <class Collection, class F>
  76. auto map(Executor& exec, Collection&& c, F&& func)
  77. -> decltype(map(exec, c.begin(), c.end(), func)) {
  78. return map(exec, c.begin(), c.end(), std::forward<F>(func));
  79. }
  80. } // namespace futures
  81. /**
  82. Make a completed SemiFuture by moving in a value. e.g.
  83. string foo = "foo";
  84. auto f = makeSemiFuture(std::move(foo));
  85. or
  86. auto f = makeSemiFuture<string>("foo");
  87. */
  88. template <class T>
  89. SemiFuture<typename std::decay<T>::type> makeSemiFuture(T&& t);
  90. /** Make a completed void SemiFuture. */
  91. SemiFuture<Unit> makeSemiFuture();
  92. /**
  93. Make a SemiFuture by executing a function.
  94. If the function returns a value of type T, makeSemiFutureWith
  95. returns a completed SemiFuture<T>, capturing the value returned
  96. by the function.
  97. If the function returns a SemiFuture<T> already, makeSemiFutureWith
  98. returns just that.
  99. Either way, if the function throws, a failed Future is
  100. returned that captures the exception.
  101. */
  102. // makeSemiFutureWith(SemiFuture<T>()) -> SemiFuture<T>
  103. template <class F>
  104. typename std::enable_if<
  105. isFutureOrSemiFuture<invoke_result_t<F>>::value,
  106. SemiFuture<typename invoke_result_t<F>::value_type>>::type
  107. makeSemiFutureWith(F&& func);
  108. // makeSemiFutureWith(T()) -> SemiFuture<T>
  109. // makeSemiFutureWith(void()) -> SemiFuture<Unit>
  110. template <class F>
  111. typename std::enable_if<
  112. !(isFutureOrSemiFuture<invoke_result_t<F>>::value),
  113. SemiFuture<typename lift_unit<invoke_result_t<F>>::type>>::type
  114. makeSemiFutureWith(F&& func);
  115. /// Make a failed Future from an exception_ptr.
  116. /// Because the Future's type cannot be inferred you have to specify it, e.g.
  117. ///
  118. /// auto f = makeSemiFuture<string>(std::current_exception());
  119. template <class T>
  120. [[deprecated("use makeSemiFuture(exception_wrapper)")]] SemiFuture<T>
  121. makeSemiFuture(std::exception_ptr const& e);
  122. /// Make a failed SemiFuture from an exception_wrapper.
  123. template <class T>
  124. SemiFuture<T> makeSemiFuture(exception_wrapper ew);
  125. /** Make a SemiFuture from an exception type E that can be passed to
  126. std::make_exception_ptr(). */
  127. template <class T, class E>
  128. typename std::
  129. enable_if<std::is_base_of<std::exception, E>::value, SemiFuture<T>>::type
  130. makeSemiFuture(E const& e);
  131. /** Make a Future out of a Try */
  132. template <class T>
  133. SemiFuture<T> makeSemiFuture(Try<T> t);
  134. /**
  135. Make a completed Future by moving in a value. e.g.
  136. string foo = "foo";
  137. auto f = makeFuture(std::move(foo));
  138. or
  139. auto f = makeFuture<string>("foo");
  140. NOTE: This function is deprecated. Please use makeSemiFuture and pass the
  141. appropriate executor to .via on the returned SemiFuture to get a
  142. valid Future where necessary.
  143. */
  144. template <class T>
  145. Future<typename std::decay<T>::type> makeFuture(T&& t);
  146. /**
  147. Make a completed void Future.
  148. NOTE: This function is deprecated. Please use makeSemiFuture and pass the
  149. appropriate executor to .via on the returned SemiFuture to get a
  150. valid Future where necessary.
  151. */
  152. Future<Unit> makeFuture();
  153. /**
  154. Make a Future by executing a function.
  155. If the function returns a value of type T, makeFutureWith
  156. returns a completed Future<T>, capturing the value returned
  157. by the function.
  158. If the function returns a Future<T> already, makeFutureWith
  159. returns just that.
  160. Either way, if the function throws, a failed Future is
  161. returned that captures the exception.
  162. Calling makeFutureWith(func) is equivalent to calling
  163. makeFuture().then(func).
  164. NOTE: This function is deprecated. Please use makeSemiFutureWith and pass the
  165. appropriate executor to .via on the returned SemiFuture to get a
  166. valid Future where necessary.
  167. */
  168. // makeFutureWith(Future<T>()) -> Future<T>
  169. template <class F>
  170. typename std::
  171. enable_if<isFuture<invoke_result_t<F>>::value, invoke_result_t<F>>::type
  172. makeFutureWith(F&& func);
  173. // makeFutureWith(T()) -> Future<T>
  174. // makeFutureWith(void()) -> Future<Unit>
  175. template <class F>
  176. typename std::enable_if<
  177. !(isFuture<invoke_result_t<F>>::value),
  178. Future<typename lift_unit<invoke_result_t<F>>::type>>::type
  179. makeFutureWith(F&& func);
  180. /// Make a failed Future from an exception_ptr.
  181. /// Because the Future's type cannot be inferred you have to specify it, e.g.
  182. ///
  183. /// auto f = makeFuture<string>(std::current_exception());
  184. template <class T>
  185. [[deprecated("use makeSemiFuture(exception_wrapper)")]] Future<T> makeFuture(
  186. std::exception_ptr const& e);
  187. /// Make a failed Future from an exception_wrapper.
  188. /// NOTE: This function is deprecated. Please use makeSemiFuture and pass the
  189. /// appropriate executor to .via on the returned SemiFuture to get a
  190. /// valid Future where necessary.
  191. template <class T>
  192. Future<T> makeFuture(exception_wrapper ew);
  193. /** Make a Future from an exception type E that can be passed to
  194. std::make_exception_ptr().
  195. NOTE: This function is deprecated. Please use makeSemiFuture and pass the
  196. appropriate executor to .via on the returned SemiFuture to get a
  197. valid Future where necessary.
  198. */
  199. template <class T, class E>
  200. typename std::enable_if<std::is_base_of<std::exception, E>::value, Future<T>>::
  201. type
  202. makeFuture(E const& e);
  203. /**
  204. Make a Future out of a Try
  205. NOTE: This function is deprecated. Please use makeSemiFuture and pass the
  206. appropriate executor to .via on the returned SemiFuture to get a
  207. valid Future where necessary.
  208. */
  209. template <class T>
  210. Future<T> makeFuture(Try<T> t);
  211. /*
  212. * Return a new Future that will call back on the given Executor.
  213. * This is just syntactic sugar for makeFuture().via(executor)
  214. *
  215. * @param executor the Executor to call back on
  216. * @param priority optionally, the priority to add with. Defaults to 0 which
  217. * represents medium priority.
  218. *
  219. * @returns a void Future that will call back on the given executor
  220. */
  221. inline Future<Unit> via(
  222. Executor* executor,
  223. int8_t priority = Executor::MID_PRI);
  224. inline Future<Unit> via(
  225. Executor::KeepAlive<> executor,
  226. int8_t priority = Executor::MID_PRI);
  227. /// Execute a function via the given executor and return a future.
  228. /// This is semantically equivalent to via(executor).then(func), but
  229. /// easier to read and slightly more efficient.
  230. template <class Func>
  231. auto via(Executor*, Func&& func) -> Future<
  232. typename isFutureOrSemiFuture<decltype(std::declval<Func>()())>::Inner>;
  233. template <class Func>
  234. auto via(Executor::KeepAlive<>, Func&& func) -> Future<
  235. typename isFutureOrSemiFuture<decltype(std::declval<Func>()())>::Inner>;
  236. /** When all the input Futures complete, the returned Future will complete.
  237. Errors do not cause early termination; this Future will always succeed
  238. after all its Futures have finished (whether successfully or with an
  239. error).
  240. The Futures are moved in, so your copies are invalid. If you need to
  241. chain further from these Futures, use the variant with an output iterator.
  242. This function is thread-safe for Futures running on different threads. But
  243. if you are doing anything non-trivial after, you will probably want to
  244. follow with `via(executor)` because it will complete in whichever thread the
  245. last Future completes in.
  246. The return type for Future<T> input is a Future<std::vector<Try<T>>>
  247. */
  248. template <class InputIterator>
  249. SemiFuture<std::vector<
  250. Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>
  251. collectAllSemiFuture(InputIterator first, InputIterator last);
  252. /// Sugar for the most common case
  253. template <class Collection>
  254. auto collectAllSemiFuture(Collection&& c)
  255. -> decltype(collectAllSemiFuture(c.begin(), c.end())) {
  256. return collectAllSemiFuture(c.begin(), c.end());
  257. }
  258. template <class InputIterator>
  259. Future<std::vector<
  260. Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>
  261. collectAll(InputIterator first, InputIterator last);
  262. template <class Collection>
  263. auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) {
  264. return collectAll(c.begin(), c.end());
  265. }
  266. /// This version takes a varying number of Futures instead of an iterator.
  267. /// The return type for (Future<T1>, Future<T2>, ...) input
  268. /// is a Future<std::tuple<Try<T1>, Try<T2>, ...>>.
  269. /// The Futures are moved in, so your copies are invalid.
  270. template <typename... Fs>
  271. SemiFuture<std::tuple<Try<typename remove_cvref_t<Fs>::value_type>...>>
  272. collectAllSemiFuture(Fs&&... fs);
  273. template <typename... Fs>
  274. Future<std::tuple<Try<typename remove_cvref_t<Fs>::value_type>...>> collectAll(
  275. Fs&&... fs);
  276. /// Like collectAll, but will short circuit on the first exception. Thus, the
  277. /// type of the returned Future is std::vector<T> instead of
  278. /// std::vector<Try<T>>
  279. template <class InputIterator>
  280. Future<std::vector<
  281. typename std::iterator_traits<InputIterator>::value_type::value_type>>
  282. collect(InputIterator first, InputIterator last);
  283. /// Sugar for the most common case
  284. template <class Collection>
  285. auto collect(Collection&& c) -> decltype(collect(c.begin(), c.end())) {
  286. return collect(c.begin(), c.end());
  287. }
  288. /// Like collectAll, but will short circuit on the first exception. Thus, the
  289. /// type of the returned Future is std::tuple<T1, T2, ...> instead of
  290. /// std::tuple<Try<T1>, Try<T2>, ...>
  291. template <typename... Fs>
  292. Future<std::tuple<typename remove_cvref_t<Fs>::value_type...>> collect(
  293. Fs&&... fs);
  294. /** The result is a pair of the index of the first Future to complete and
  295. the Try. If multiple Futures complete at the same time (or are already
  296. complete when passed in), the "winner" is chosen non-deterministically.
  297. This function is thread-safe for Futures running on different threads.
  298. */
  299. template <class InputIterator>
  300. Future<std::pair<
  301. size_t,
  302. Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>
  303. collectAny(InputIterator first, InputIterator last);
  304. /// Sugar for the most common case
  305. template <class Collection>
  306. auto collectAny(Collection&& c) -> decltype(collectAny(c.begin(), c.end())) {
  307. return collectAny(c.begin(), c.end());
  308. }
  309. /** Similar to collectAny, collectAnyWithoutException return the first Future to
  310. * complete without exceptions. If none of the future complete without
  311. * excpetions, the last exception will be returned as a result.
  312. */
  313. template <class InputIterator>
  314. Future<std::pair<
  315. size_t,
  316. typename std::iterator_traits<InputIterator>::value_type::value_type>>
  317. collectAnyWithoutException(InputIterator first, InputIterator last);
  318. /// Sugar for the most common case
  319. template <class Collection>
  320. auto collectAnyWithoutException(Collection&& c)
  321. -> decltype(collectAnyWithoutException(c.begin(), c.end())) {
  322. return collectAnyWithoutException(c.begin(), c.end());
  323. }
  324. /** when n Futures have completed, the Future completes with a vector of
  325. the index and Try of those n Futures (the indices refer to the original
  326. order, but the result vector will be in an arbitrary order)
  327. Not thread safe.
  328. */
  329. template <class InputIterator>
  330. SemiFuture<std::vector<std::pair<
  331. size_t,
  332. Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>>
  333. collectN(InputIterator first, InputIterator last, size_t n);
  334. /// Sugar for the most common case
  335. template <class Collection>
  336. auto collectN(Collection&& c, size_t n)
  337. -> decltype(collectN(c.begin(), c.end(), n)) {
  338. return collectN(c.begin(), c.end(), n);
  339. }
  340. /** window creates up to n Futures using the values
  341. in the collection, and then another Future for each Future
  342. that completes
  343. this is basically a sliding window of Futures of size n
  344. func must return a Future for each value in input
  345. */
  346. template <
  347. class Collection,
  348. class F,
  349. class ItT = typename std::iterator_traits<
  350. typename Collection::iterator>::value_type,
  351. class Result = typename invoke_result_t<F, ItT&&>::value_type>
  352. std::vector<Future<Result>> window(Collection input, F func, size_t n);
  353. template <
  354. class Collection,
  355. class F,
  356. class ItT = typename std::iterator_traits<
  357. typename Collection::iterator>::value_type,
  358. class Result = typename invoke_result_t<F, ItT&&>::value_type>
  359. std::vector<Future<Result>>
  360. window(Executor* executor, Collection input, F func, size_t n);
  361. template <
  362. class Collection,
  363. class F,
  364. class ItT = typename std::iterator_traits<
  365. typename Collection::iterator>::value_type,
  366. class Result = typename invoke_result_t<F, ItT&&>::value_type>
  367. std::vector<Future<Result>>
  368. window(Executor::KeepAlive<> executor, Collection input, F func, size_t n);
  369. template <typename F, typename T, typename ItT>
  370. using MaybeTryArg = typename std::
  371. conditional<is_invocable<F, T&&, Try<ItT>&&>::value, Try<ItT>, ItT>::type;
  372. /** repeatedly calls func on every result, e.g.
  373. reduce(reduce(reduce(T initial, result of first), result of second), ...)
  374. The type of the final result is a Future of the type of the initial value.
  375. Func can either return a T, or a Future<T>
  376. func is called in order of the input, see unorderedReduce if that is not
  377. a requirement
  378. */
  379. template <class It, class T, class F>
  380. Future<T> reduce(It first, It last, T&& initial, F&& func);
  381. /// Sugar for the most common case
  382. template <class Collection, class T, class F>
  383. auto reduce(Collection&& c, T&& initial, F&& func) -> decltype(reduce(
  384. c.begin(),
  385. c.end(),
  386. std::forward<T>(initial),
  387. std::forward<F>(func))) {
  388. return reduce(
  389. c.begin(), c.end(), std::forward<T>(initial), std::forward<F>(func));
  390. }
  391. /** like reduce, but calls func on finished futures as they complete
  392. does NOT keep the order of the input
  393. */
  394. template <class It, class T, class F>
  395. Future<T> unorderedReduce(It first, It last, T initial, F func);
  396. /// Sugar for the most common case
  397. template <class Collection, class T, class F>
  398. auto unorderedReduce(Collection&& c, T&& initial, F&& func)
  399. -> decltype(unorderedReduce(
  400. c.begin(),
  401. c.end(),
  402. std::forward<T>(initial),
  403. std::forward<F>(func))) {
  404. return unorderedReduce(
  405. c.begin(), c.end(), std::forward<T>(initial), std::forward<F>(func));
  406. }
  407. } // namespace folly