// Copyright (c) 2015 Amanieu d'Antras // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #ifndef ASYNCXX_H_ # error "Do not include this header directly, include instead." #endif namespace async { namespace detail { // Recursively split the arguments so tasks are spawned in parallel template struct parallel_invoke_internal { template static void run(Sched& sched, const Tuple& args) { auto&& t = async::local_spawn(sched, [&sched, &args] { parallel_invoke_internal::run(sched, args); }); parallel_invoke_internal::run(sched, args); t.get(); } }; template struct parallel_invoke_internal { template static void run(Sched&, const Tuple& args) { // Make sure to preserve the rvalue/lvalue-ness of the original parameter std::forward::type>(std::get(args))(); } }; template struct parallel_invoke_internal { template static void run(Sched&, const Tuple&) {} }; } // namespace detail // Run several functions in parallel, optionally using the specified scheduler. template typename std::enable_if::value>::type parallel_invoke(Sched& sched, Args&&... args) { detail::parallel_invoke_internal<0, sizeof...(Args)>::run(sched, std::forward_as_tuple(std::forward(args)...)); } template void parallel_invoke(Args&&... args) { async::parallel_invoke(::async::default_scheduler(), std::forward(args)...); } } // namespace async