14#include "../../expected.hpp"
15#include "../callable/bind.hpp"
20#include <system_error>
50 template <
typename F,
typename... Args>
56 throw std::runtime_error(
"inline_pool_backend is shut down");
60 template <
typename F,
typename... Args>
65 auto bound =
bind_args(std::forward<F>(f), std::forward<Args>(args)...);
67 return unexpected(std::make_error_code(std::errc::operation_canceled));
69 std::promise<return_type> p;
70 auto future = p.get_future();
73 if constexpr (std::is_void_v<return_type>)
85 p.set_exception(std::current_exception());
90 template <
typename F,
typename... Args>
92 post(F&& f, Args&&... args)
94 auto r =
try_post(std::forward<F>(f), std::forward<Args>(args)...);
96 throw std::runtime_error(
"inline_pool_backend is shut down");
99 template <
typename F,
typename... Args>
103 auto bound =
bind_args(std::forward<F>(f), std::forward<Args>(args)...);
105 return unexpected(std::make_error_code(std::errc::operation_canceled));
116 template <
typename Iterator>
121 return unexpected(std::make_error_code(std::errc::operation_canceled));
123 std::vector<std::future<void>> futures;
124 for (
auto it = begin; it != end; ++it)
126 std::promise<void> p;
127 futures.push_back(p.get_future());
135 p.set_exception(std::current_exception());
141 template <
typename Iterator>
143 submit_batch(Iterator begin, Iterator end) -> std::vector<std::future<void>>
147 throw std::runtime_error(
"inline_pool_backend is shut down");
151 template <
typename Iterator,
typename F>
155 static_assert(detail::is_forward_iterator_v<Iterator>,
"parallel_for_each requires at least a forward iterator");
157 throw std::system_error(std::make_error_code(std::errc::operation_canceled),
158 "inline_pool_backend::parallel_for_each");
159 for (
auto it = begin; it != end; ++it)
164 size() const noexcept ->
size_t
A pool that executes every task inline on the calling thread.
void shutdown(shutdown_policy_backend=shutdown_policy_backend::drain)
void parallel_for_each(Iterator begin, Iterator end, F &&func)
auto try_submit_batch(Iterator begin, Iterator end) -> expected< std::vector< std::future< void > >, std::error_code >
auto pending_tasks() const noexcept -> size_t
void post(F &&f, Args &&... args)
auto submit(F &&f, Args &&... args) -> std::future< bind_result_t< F, Args... > >
auto submit_batch(Iterator begin, Iterator end) -> std::vector< std::future< void > >
auto try_submit(F &&f, Args &&... args) -> expected< std::future< bind_result_t< F, Args... > >, std::error_code >
auto try_post(F &&f, Args &&... args) -> expected< void, std::error_code >
auto size() const noexcept -> size_t
inline_pool_backend(size_t=0)
constexpr auto has_value() const noexcept -> bool
Aggregates multiple thread_registry_backend instances into a single queryable view.
std::invoke_result_t< decltype(bind_args(std::declval< F >(), std::declval< Args >()...))& > bind_result_t
Result of invoking the decayed callable and arguments stored by bind_args.
auto bind_args(F &&function, Args &&... args)
Worker identity, CPU selection, and registration helpers.