2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/fibers/FiberManagerInternal.h>
23 template <class F, class G>
24 typename std::enable_if<
25 !std::is_same<typename std::result_of<F()>::type, void>::value,
26 void>::type inline callFuncs(F&& f, G&& g, size_t id) {
30 template <class F, class G>
31 typename std::enable_if<
32 std::is_same<typename std::result_of<F()>::type, void>::value,
33 void>::type inline callFuncs(F&& f, G&& g, size_t id) {
40 template <class InputIterator, class F>
41 inline void forEach(InputIterator first, InputIterator last, F&& f) {
46 typedef typename std::iterator_traits<InputIterator>::value_type FuncType;
52 auto taskFunc = [&tasksTodo, &e, &f, &baton](size_t id, FuncType&& func) {
59 func_ = std::forward<FuncType>(func)
62 callFuncs(std::forward<FuncType>(func_), f, id);
64 e = std::current_exception();
66 if (--tasksTodo == 0) {
72 auto firstTask = first;
75 for (size_t i = 1; first != last; ++i, ++first, ++tasksTodo) {
76 addTask(taskFunc(i, std::move(*first)));
79 taskFunc(0, std::move(*firstTask))();
82 if (e != std::exception_ptr()) {
83 std::rethrow_exception(e);