2 * Copyright 2016 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/FiberManager.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) {
38 } // anonymous namespace
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;
53 #pragma clang diagnostic push // ignore generalized lambda capture warning
54 #pragma clang diagnostic ignored "-Wc++1y-extensions"
56 auto taskFunc = [&tasksTodo, &e, &f, &baton](size_t id, FuncType&& func) {
63 func_ = std::forward<FuncType>(func)
66 callFuncs(std::forward<FuncType>(func_), f, id);
68 e = std::current_exception();
70 if (--tasksTodo == 0) {
76 #pragma clang diagnostic pop
79 auto firstTask = first;
82 for (size_t i = 1; first != last; ++i, ++first, ++tasksTodo) {
83 addTask(taskFunc(i, std::move(*first)));
86 taskFunc(0, std::move(*firstTask))();
89 if (e != std::exception_ptr()) {
90 std::rethrow_exception(e);