3#include "../../pool_statistics.hpp"
4#include "../../result.hpp"
5#include "../../shutdown_policy.hpp"
6#include "../../thread_config.hpp"
7#include "../../worker_count.hpp"
8#include "../../worker_registration.hpp"
9#include "../thread/control.hpp"
17#include <system_error>
26template <
typename Backend>
30 if constexpr (std::is_same_v<Backend, work_stealing_pool_backend>)
31 return std::make_unique<Backend>(count.resolve(),
38template <
typename T,
typename =
void>
44struct has_stolen_tasks<T,
std::void_t<decltype(std::declval<T const&>().stolen_tasks)>> : std::true_type
48template <
typename Backend>
55 if (!backend->is_current_worker())
70 auto* pending = backend.release();
76 std::unique_ptr<Backend> owner(pending);
95template <
typename Backend>
113 impl_ = std::move(
other.impl_);
118 template <
typename F,
typename... Args>
123 return unexpected(std::make_error_code(std::errc::operation_canceled));
125 {
return impl_->try_submit(std::forward<F>(function), std::forward<Args>(args)...); });
128 template <
typename F,
typename... Args>
132 auto submitted =
submit(std::forward<F>(function), std::forward<Args>(args)...);
134 throw std::system_error(submitted.error(),
"advanced pool submission");
135 return std::move(*submitted);
138 template <
typename F,
typename... Args>
143 return unexpected(std::make_error_code(std::errc::operation_canceled));
145 {
return impl_->try_post(std::forward<F>(function), std::forward<Args>(args)...); });
148 template <
typename F,
typename... Args>
152 auto posted =
post(std::forward<F>(function), std::forward<Args>(args)...);
154 throw std::system_error(posted.error(),
"advanced pool post");
157 template <
typename Iterator>
162 return unexpected(std::make_error_code(std::errc::operation_canceled));
163 return try_result([&]() ->
result<std::vector<std::future<void>>> {
return impl_->try_submit_batch(begin, end); });
166 template <
typename Iterator>
172 throw std::system_error(submitted.error(),
"advanced pool batch submission");
173 return std::move(*submitted);
176 template <
typename Iterator,
typename Function>
181 return unexpected(std::make_error_code(std::errc::operation_canceled));
185 impl_->parallel_for_each(begin, end, std::forward<Function>(function));
194 return unexpected(std::make_error_code(std::errc::operation_canceled));
198 impl_->wait_for_tasks();
207 return unexpected(std::make_error_code(std::errc::operation_canceled));
215 return unexpected(std::make_error_code(std::errc::operation_canceled));
243 return impl_ ? impl_->size() : 0;
249 return impl_ ? impl_->pending_tasks() : 0;
255 return impl_ && impl_->is_current_worker();
263 auto const source = impl_->get_statistics();
265 source.
total_threads, source.active_threads, source.pending_tasks, source.completed_tasks, 0,
266 source.tasks_per_second, source.avg_task_time
269 result_value.stolen_tasks = source.stolen_tasks;
280 std::unique_ptr<Backend> impl_;
283template <
typename Backend>
301 impl_ = std::move(
other.impl_);
306 template <
typename F,
typename... Args>
311 return unexpected(std::make_error_code(std::errc::operation_canceled));
313 {
return impl_->try_post(std::forward<F>(function), std::forward<Args>(args)...); });
316 template <
typename F,
typename... Args>
320 auto posted =
post(std::forward<F>(function), std::forward<Args>(args)...);
322 throw std::system_error(posted.error(),
"advanced lightweight pool post");
329 return unexpected(std::make_error_code(std::errc::operation_canceled));
337 return unexpected(std::make_error_code(std::errc::operation_canceled));
365 return impl_ ? impl_->size() : 0;
371 return impl_ && impl_->is_current_worker();
381 std::unique_ptr<Backend> impl_;
auto post(F &&function, Args &&... args) -> result< void >
auto distribute_workers() -> result< void >
lightweight_pool_facade(worker_count count, worker_registration registration)
auto configure_workers(thread_config const &config) -> result< void >
auto operator=(lightweight_pool_facade &&other) noexcept -> lightweight_pool_facade &
auto shutdown_for(std::chrono::milliseconds timeout) -> result< bool >
lightweight_pool_facade(lightweight_pool_facade const &)=delete
void post_or_throw(F &&function, Args &&... args)
auto operator=(lightweight_pool_facade const &) -> lightweight_pool_facade &=delete
~lightweight_pool_facade()
lightweight_pool_facade(lightweight_pool_facade &&other) noexcept
auto size() const noexcept -> std::size_t
auto is_current_worker() const noexcept -> bool
auto shutdown(shutdown_policy policy=shutdown_policy::drain) -> result< void >
auto submit_batch_or_throw(Iterator begin, Iterator end) -> std::vector< std::future< void > >
auto submit_or_throw(F &&function, Args &&... args) -> std::future< bind_result_t< F, Args... > >
submitting_pool_facade(submitting_pool_facade const &)=delete
auto is_current_worker() const noexcept -> bool
auto submit(F &&function, Args &&... args) -> result< std::future< bind_result_t< F, Args... > > >
submitting_pool_facade(worker_count count, worker_registration registration)
auto size() const noexcept -> std::size_t
auto get_statistics() const -> pool_statistics
auto pending_tasks() const -> std::size_t
~submitting_pool_facade()
auto wait() -> result< void >
auto operator=(submitting_pool_facade &&other) noexcept -> submitting_pool_facade &
auto submit_batch(Iterator begin, Iterator end) -> result< std::vector< std::future< void > > >
void post_or_throw(F &&function, Args &&... args)
auto parallel_for_each(Iterator begin, Iterator end, Function &&function) -> result< void >
auto shutdown(shutdown_policy policy=shutdown_policy::drain) -> result< void >
auto distribute_workers() -> result< void >
submitting_pool_facade(submitting_pool_facade &&other) noexcept
auto post(F &&function, Args &&... args) -> result< void >
auto shutdown_for(std::chrono::milliseconds timeout) -> result< bool >
auto operator=(submitting_pool_facade const &) -> submitting_pool_facade &=delete
auto configure_workers(thread_config const &config) -> result< void >
Portable thread configuration bundle.
Value type for pool worker count.
Aggregates multiple thread_registry_backend instances into a single queryable view.
void dispose_pool_backend(std::unique_ptr< Backend > &backend) noexcept
@ other
Standard round-robin time-sharing.
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 make_pool_backend(worker_count count, worker_registration registration) -> std::unique_ptr< Backend >
constexpr auto to_native(shutdown_policy policy) noexcept -> shutdown_policy_backend
auto try_result(Function &&function) -> decltype(std::forward< Function >(function)())
shutdown_policy
Defines how pools handle pending work during shutdown.
@ drain
Finish queued work before returning from shutdown.
worker_registration
Controls whether pool workers are registered in a thread registry.
@ global_registry
Register workers in the global registry for discovery/control.
Snapshot of pool activity metrics.
std::size_t total_threads
Number of worker threads owned by the pool.
Work-stealing deque and pool implementation.