3#include "../../result.hpp"
4#include "../../scheduled_task.hpp"
5#include "../../shutdown_policy.hpp"
6#include "../../thread_config.hpp"
7#include "../../worker_count.hpp"
8#include "../../worker_registration.hpp"
9#include "../pool/shutdown.hpp"
10#include "../thread/control.hpp"
18#include <system_error>
25template <
typename Backend>
39 template <
typename Rep,
typename Period,
typename F>
43 if (stopped_.load(std::memory_order_acquire))
44 return unexpected(std::make_error_code(std::errc::operation_canceled));
48 auto native_delay = checked_duration_cast<typename Backend::duration>(delay);
51 auto handle = impl_->schedule_after(native_delay.value(), std::forward<F>(function));
52 if (handle.is_cancelled())
53 return unexpected(std::make_error_code(std::errc::operation_canceled));
54 return scheduled_task_access::make(std::move(handle));
62 if (stopped_.load(std::memory_order_acquire))
63 return unexpected(std::make_error_code(std::errc::operation_canceled));
67 auto handle = impl_->schedule_at(time, std::forward<F>(function));
68 if (handle.is_cancelled())
69 return unexpected(std::make_error_code(std::errc::operation_canceled));
70 return scheduled_task_access::make(std::move(handle));
74 template <
typename Rep,
typename Period,
typename F>
78 auto const native_interval = checked_duration_cast<typename Backend::duration>(interval);
81 if (native_interval.value() <= Backend::duration::zero())
82 return unexpected(std::make_error_code(std::errc::invalid_argument));
83 if (stopped_.load(std::memory_order_acquire))
84 return unexpected(std::make_error_code(std::errc::operation_canceled));
88 auto handle = impl_->schedule_periodic(native_interval.value(), std::forward<F>(function));
89 if (handle.is_cancelled())
90 return unexpected(std::make_error_code(std::errc::operation_canceled));
91 return scheduled_task_access::make(std::move(handle));
95 template <
typename InitialRep,
typename InitialPeriod,
typename IntervalRep,
typename IntervalPeriod,
typename F>
98 std::chrono::duration<IntervalRep, IntervalPeriod> interval, F&& function)
101 auto const native_interval = checked_duration_cast<typename Backend::duration>(interval);
102 if (!native_interval)
104 if (native_interval.value() <= Backend::duration::zero())
105 return unexpected(std::make_error_code(std::errc::invalid_argument));
106 auto const native_delay = checked_duration_cast<typename Backend::duration>(initial_delay);
109 if (stopped_.load(std::memory_order_acquire))
110 return unexpected(std::make_error_code(std::errc::operation_canceled));
114 auto handle = impl_->schedule_periodic_after(native_delay.value(), native_interval.value(),
115 std::forward<F>(function));
116 if (handle.is_cancelled())
117 return unexpected(std::make_error_code(std::errc::operation_canceled));
118 return scheduled_task_access::make(std::move(handle));
126 return unexpected(std::make_error_code(std::errc::operation_canceled));
134 return unexpected(std::make_error_code(std::errc::operation_canceled));
147 stopped_.store(
true, std::memory_order_release);
155 return impl_ ? impl_->scheduled_count() : 0;
166 dispose_impl() noexcept
171 if (!impl_->is_current_context())
184 auto* backend = impl_.release();
190 std::unique_ptr<Backend> owner(backend);
207 std::unique_ptr<Backend> impl_;
208 std::atomic<bool> stopped_{
false };
scheduled_pool_facade(scheduled_pool_facade &&)=delete
auto configure_workers(thread_config const &config) -> result< void >
auto shutdown(shutdown_policy policy=shutdown_policy::drain) -> result< void >
auto schedule_at(std::chrono::steady_clock::time_point time, F &&function) -> result< scheduled_task >
auto schedule_periodic_after(std::chrono::duration< InitialRep, InitialPeriod > initial_delay, std::chrono::duration< IntervalRep, IntervalPeriod > interval, F &&function) -> result< scheduled_task >
auto configure_scheduler(thread_config const &config) -> result< void >
scheduled_pool_facade(worker_count count, worker_registration registration)
auto operator=(scheduled_pool_facade const &) -> scheduled_pool_facade &=delete
auto schedule_periodic(std::chrono::duration< Rep, Period > interval, F &&function) -> result< scheduled_task >
auto operator=(scheduled_pool_facade &&) -> scheduled_pool_facade &=delete
auto scheduled_count() const -> std::size_t
auto schedule_after(std::chrono::duration< Rep, Period > delay, F &&function) -> result< scheduled_task >
scheduled_pool_facade(scheduled_pool_facade const &)=delete
Portable thread configuration bundle.
Value type for pool worker count.
Aggregates multiple thread_registry_backend instances into a single queryable view.
constexpr auto to_native(shutdown_policy policy) noexcept -> shutdown_policy_backend
auto try_result(Function &&function) -> decltype(std::forward< Function >(function)())
auto global_registry() -> thread_registry &
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.