14#include <condition_variable>
103template <
typename PoolType = thread_pool_backend>
111 using duration = std::chrono::steady_clock::duration;
124 std::shared_ptr<periodic_task_state> periodic_value = {})
131 if (
started.load(std::memory_order_acquire))
133 cancellation->pool_stopped.store(
true, std::memory_order_release);
135 periodic->running.store(
false, std::memory_order_release);
143 class scheduled_task_info
152 [[nodiscard]]
static auto
154 std::shared_ptr<detail::scheduled_cancellation_state>
cancellation) -> scheduled_task_info
156 return scheduled_task_info(
id, run_time, std::move(task), std::move(
cancellation));
159 [[nodiscard]]
static auto
161 std::shared_ptr<detail::scheduled_cancellation_state>
cancellation) -> scheduled_task_info
163 if (interval <= duration::zero())
164 throw std::invalid_argument(
"scheduled_pool periodic interval must be positive");
165 return scheduled_task_info(
id, run_time, interval, std::move(task), std::move(
cancellation));
176 return cancellation_;
181 return periodic_task_;
186 return std::move(one_shot_task_);
197 auto const interval = *interval_;
200 throw std::system_error(
advanced.error(),
"scheduled_pool periodic deadline");
202 if (next_run_ <= now)
204 auto const quotient = (now - next_run_) / interval;
205 auto const maximum = (std::numeric_limits<
decltype(quotient)>::max)();
206 if (quotient == maximum)
207 throw std::system_error(std::make_error_code(std::errc::value_too_large),
208 "scheduled_pool periodic deadline");
209 auto const missed = quotient + 1;
210 if (missed > maximum / interval.count())
211 throw std::system_error(std::make_error_code(std::errc::value_too_large),
212 "scheduled_pool periodic deadline");
215 throw std::system_error(
advanced.error(),
"scheduled_pool periodic deadline");
222 std::shared_ptr<detail::scheduled_cancellation_state>
cancellation)
223 : id_(id), next_run_(run_time), one_shot_task_(
std::move(task)), cancellation_(
std::move(
cancellation)),
229 std::shared_ptr<detail::scheduled_cancellation_state>
cancellation)
230 : id_(id), next_run_(run_time), interval_(interval),
231 periodic_task_(
std::make_shared<periodic_task_state>(
std::move(task))),
238 std::optional<duration> interval_;
240 std::shared_ptr<periodic_task_state> periodic_task_;
241 std::shared_ptr<detail::scheduled_cancellation_state> cancellation_;
251 : pool_(worker_threads), stop_(false), next_task_id_(1)
256 template <
typename T = PoolType, std::enable_if_t<std::is_constructible_v<T,
size_t,
bool>,
int> = 0>
258 : pool_(worker_threads, register_workers), stop_(false), next_task_id_(1)
282 throw std::system_error(run_time.error(),
"scheduled_pool delay");
286 template <
typename F, std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<F>, task_type>,
int> = 0>
292 throw std::system_error(run_time.error(),
"scheduled_pool delay");
308 template <
typename F, std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<F>, task_type>,
int> = 0>
330 template <
typename F, std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<F>, task_type>,
int> = 0>
347 if (interval <= duration::zero())
348 throw std::invalid_argument(
"scheduled_pool periodic interval must be positive");
351 throw std::system_error(run_time.error(),
"scheduled_pool initial delay");
354 throw std::system_error(next_run.error(),
"scheduled_pool periodic deadline");
358 template <
typename F, std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<F>, task_type>,
int> = 0>
362 if (interval <= duration::zero())
363 throw std::invalid_argument(
"scheduled_pool periodic interval must be positive");
366 throw std::system_error(run_time.error(),
"scheduled_pool initial delay");
369 throw std::system_error(next_run.error(),
"scheduled_pool periodic deadline");
370 return insert_periodic_task(run_time.value(), interval,
392 std::lock_guard<std::mutex> lock(mutex_);
393 return scheduled_tasks_.size();
399 return pool_.is_current_worker() || current_scheduler ==
this;
419 std::lock_guard<std::recursive_mutex> shutdown_lock(shutdown_mutex_);
420 std::multimap<time_point, scheduled_task_info> discarded;
422 std::lock_guard<std::mutex> lock(mutex_);
426 for (
auto const& task : scheduled_tasks_)
427 task.second.cancellation()->pool_stopped.store(
true, std::memory_order_release);
428 scheduled_tasks_.swap(discarded);
433 condition_.notify_one();
436 std::lock_guard<std::mutex> scheduler_lock(scheduler_mutex_);
437 if (scheduler_thread_.joinable())
439 scheduler_thread_.join();
444 pool_.shutdown(policy);
456 return pool_.configure_threads(name_prefix, policy, priority);
462 return pool_.configure_threads(config);
468 if (current_scheduler ==
this)
470 std::lock_guard<std::mutex> scheduler_lock(scheduler_mutex_);
482 if (!info.has_value())
483 return unexpected(std::make_error_code(std::errc::no_such_process));
491 if (!info.has_value())
492 return unexpected(std::make_error_code(std::errc::no_such_process));
493 return info->configure(config);
501 mutable std::mutex mutex_;
502 std::condition_variable condition_;
503 std::atomic<bool> stop_;
504 std::recursive_mutex shutdown_mutex_;
505 mutable std::mutex scheduler_mutex_;
507 std::multimap<time_point, scheduled_task_info> scheduled_tasks_;
508 std::atomic<uint64_t> next_task_id_;
515 std::promise<native_thread_id> scheduler_started;
516 auto scheduler_ready = scheduler_started.get_future();
519 [
this, started = std::move(scheduler_started)]()
mutable
521 current_scheduler =
this;
524 current_scheduler =
nullptr;
527 scheduler_tid_ = scheduler_ready.
get();
528 (void)thread_info(scheduler_tid_).set_name(
"ts_sched_pool");
534 std::lock_guard<std::mutex> lock(mutex_);
536 uint64_t
const task_id = next_task_id_++;
537 scheduled_task_backend handle(task_id);
545 scheduled_tasks_.insert(
547 condition_.notify_one();
555 std::lock_guard<std::mutex> lock(mutex_);
557 uint64_t
const task_id = next_task_id_++;
558 scheduled_task_backend handle(task_id);
567 handle.get_cancellation()) });
568 condition_.notify_one();
578 std::unique_lock<std::mutex> lock(mutex_);
584 if (scheduled_tasks_.empty())
586 condition_.wait(lock, [
this] {
return stop_ || !scheduled_tasks_.empty(); });
593 auto const now = std::chrono::steady_clock::now();
594 auto it = scheduled_tasks_.begin();
596 if (it == scheduled_tasks_.end())
607 auto const next_run = it->first;
608 condition_.wait_until(lock, next_run);
618 scheduled_tasks_.erase(it);
622 if (info.cancellation()->user_cancelled.load(std::memory_order_acquire)
623 || info.cancellation()->pool_stopped.load(std::memory_order_acquire))
631 auto cancellation = info.cancellation();
634 auto periodic_task = info.periodic_state();
635 bool const already_running = periodic_task->running.exchange(
true, std::memory_order_acq_rel);
636 if (!already_running)
638 auto dispatch = std::make_shared<scheduled_dispatch_state>(cancellation, periodic_task);
642 [periodic_task, cancellation, dispatch]()
644 dispatch->started.store(
true, std::memory_order_release);
647 if (!cancellation->user_cancelled.load(std::memory_order_acquire))
648 periodic_task->task();
652 periodic_task->running.store(
false, std::memory_order_release);
655 periodic_task->running.store(
false, std::memory_order_release);
660 periodic_task->running.store(
false, std::memory_order_release);
665 info.advance(std::chrono::steady_clock::now());
666 std::lock_guard<std::mutex> schedule_lock(mutex_);
667 if (!stop_ && !cancellation->user_cancelled.load(std::memory_order_acquire))
668 scheduled_tasks_.insert({ info.next_run(), std::move(info) });
672 auto one_shot_task = info.take_one_shot();
673 auto dispatch = std::make_shared<scheduled_dispatch_state>(cancellation);
675 [task = std::move(one_shot_task), cancellation, dispatch]()
mutable
677 dispatch->started.store(
true, std::memory_order_release);
678 if (!cancellation->user_cancelled.load(std::memory_order_acquire))
687 info.cancellation()->pool_stopped.store(
true, std::memory_order_release);
Value-semantic wrapper for a thread scheduling priority.
static constexpr auto normal() noexcept -> native_thread_priority
auto periodic_state() const noexcept -> std::shared_ptr< periodic_task_state > const &
auto next_run() const noexcept -> time_point
void advance(time_point now)
auto type() const noexcept -> kind
auto take_one_shot() noexcept -> one_shot_task_type
auto cancellation() const noexcept -> std::shared_ptr< detail::scheduled_cancellation_state > const &
static auto one_shot(std::uint64_t id, time_point run_time, one_shot_task_type task, std::shared_ptr< detail::scheduled_cancellation_state > cancellation) -> scheduled_task_info
static auto periodic(std::uint64_t id, time_point run_time, duration interval, periodic_task_type task, std::shared_ptr< detail::scheduled_cancellation_state > cancellation) -> scheduled_task_info
Thread pool augmented with delayed and periodic task scheduling.
auto is_current_context() const noexcept -> bool
auto operator=(scheduled_pool_backend_base const &) -> scheduled_pool_backend_base &=delete
detail::move_only_function< void()> periodic_task_type
auto schedule_at(time_point time_point, F &&task) -> scheduled_task_backend
scheduled_pool_backend_base(size_t worker_threads, bool register_workers)
auto configure_scheduler_thread(std::string const &name, native_scheduling_policy policy=native_scheduling_policy::other, native_thread_priority priority=native_thread_priority::normal()) -> expected< void, std::error_code >
auto scheduler_thread_info() const -> std::optional< thread_info >
std::chrono::steady_clock::time_point time_point
static void cancel(scheduled_task_backend &handle)
Cancel a scheduled task by handle.
auto schedule_periodic_after(duration initial_delay, duration interval, F &&task) -> scheduled_task_backend
detail::move_only_function< void()> one_shot_task_type
void shutdown(shutdown_policy_backend policy=shutdown_policy_backend::drain)
Shutdown the scheduler and wait for completion.
auto schedule_after(duration delay, F &&task) -> scheduled_task_backend
auto schedule_periodic(duration interval, F &&task) -> scheduled_task_backend
auto configure_scheduler_thread(native_thread_config const &config) -> expected< void, std::error_code >
auto configure_threads(native_thread_config const &config) -> expected< void, std::error_code >
auto thread_pool() -> PoolType &
Get the underlying thread pool for direct task submission.
~scheduled_pool_backend_base()
scheduled_pool_backend_base(scheduled_pool_backend_base const &)=delete
std::function< void()> task_type
auto scheduled_count() const -> size_t
Get number of scheduled tasks (including periodic).
std::chrono::steady_clock::duration duration
auto schedule_at(time_point time_point, task_type task) -> scheduled_task_backend
Schedule a task to run at a specific time point.
auto schedule_periodic_after(duration initial_delay, duration interval, task_type task) -> scheduled_task_backend
Schedule a task to run periodically after an initial delay.
auto schedule_after(duration delay, task_type task) -> scheduled_task_backend
Schedule a task to run after a delay.
scheduled_pool_backend_base(size_t worker_threads=default_worker_count())
Create a scheduled thread pool.
auto configure_threads(std::string const &name_prefix, native_scheduling_policy policy=native_scheduling_policy::other, native_thread_priority priority=native_thread_priority::normal())
Configure worker threads.
auto schedule_periodic(duration interval, task_type task) -> scheduled_task_backend
Schedule a task to run periodically at fixed intervals.
Copyable handle for a cancellable scheduled task.
Owning wrapper around std::thread with RAII join-on-destroy semantics.
auto get() noexcept -> std::thread &
Lightweight handle for querying and controlling a specific OS thread.
static auto get_thread_id() -> native_thread_id
Aggregates multiple thread_registry_backend instances into a single queryable view.
native_scheduling_policy
Enumeration of available thread scheduling policies.
@ other
Standard round-robin time-sharing.
scheduled_pool_backend_base< thread_pool_backend > scheduled_pool_backend
scheduled_pool_backend_base using the default thread_pool_backend backend.
auto configure_thread(ThreadLike &thread, std::string const &name, native_scheduling_policy policy, native_thread_priority priority) -> expected< void, std::error_code >
auto make_move_only_function(Callable &&callable) -> move_only_function< Signature, InlineSize >
auto default_worker_count() noexcept -> std::size_t
scheduled_pool_backend_base< lightweight_pool_backend > scheduled_lightweight_pool_backend
scheduled_pool_backend_base using lightweight_pool_backend as backend (minimal overhead).
scheduled_pool_backend_base< polling_pool_backend > scheduled_polling_pool_backend
scheduled_pool_backend_base using polling_pool_backend as backend.
void throw_worker_deadlock()
scheduled_pool_backend_base< work_stealing_pool_backend > scheduled_work_stealing_pool_backend
scheduled_pool_backend_base using work_stealing_pool_backend as backend.
auto checked_deadline_after(std::chrono::time_point< Clock, Duration > now, Duration delay) -> expected< std::chrono::time_point< Clock, Duration >, std::error_code >
unexpected(E) -> unexpected< E >
Internal cancellable scheduled-task handle.
std::atomic< bool > running
periodic_task_state(periodic_task_type task_value)
std::atomic< bool > started
scheduled_dispatch_state(std::shared_ptr< detail::scheduled_cancellation_state > cancellation_value, std::shared_ptr< periodic_task_state > periodic_value={})
std::shared_ptr< periodic_task_state > periodic
~scheduled_dispatch_state()
std::shared_ptr< detail::scheduled_cancellation_state > cancellation