22namespace task_pool_util {
39 virtual void run() = 0;
45 template<
class Function>
56 _func(std::forward<Function>(f)) {
75 using __task = std::unique_ptr<_ImplBase>;
113 TaskPool() =
default;
121 _tasks {std::move(other._tasks)},
132 std::swap(
_tasks, other._tasks);
145 template<
class Function,
class... Args>
147 static_assert(std::is_invocable_v<Function, Args &&...>,
"arguments don't match the function");
149 using __return = std::invoke_result_t<Function, Args &&...>;
150 using task_t = std::packaged_task<__return()>;
152 auto bind = [task = std::forward<Function>(newTask), tuple_args = std::make_tuple(std::forward<Args>(
args)...)]()
mutable {
153 return std::apply(task, std::move(tuple_args));
156 task_t task(std::move(bind));
158 auto future = task.get_future();
161 _tasks.emplace_back(toRunnable(std::move(task)));
176 if (std::get<0>(*it) < task.first) {
181 _timer_tasks.emplace(it, task.first, std::move(task.second));
191 template<
class Function,
class X,
class Y,
class... Args>
192 auto pushDelayed(Function &&newTask, std::chrono::duration<X, Y> duration, Args &&...
args) {
193 static_assert(std::is_invocable_v<Function, Args &&...>,
"arguments don't match the function");
195 using __return = std::invoke_result_t<Function, Args &&...>;
196 using task_t = std::packaged_task<__return()>;
199 if constexpr (std::is_floating_point_v<X>) {
200 time_point = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
202 time_point = std::chrono::steady_clock::now() + duration;
205 auto bind = [task = std::forward<Function>(newTask), tuple_args = std::make_tuple(std::forward<Args>(
args)...)]()
mutable {
206 return std::apply(task, std::move(tuple_args));
209 task_t task(std::move(bind));
211 auto future = task.get_future();
212 auto runnable = toRunnable(std::move(task));
216 pushDelayed(std::pair {time_point, std::move(runnable)});
225 template<
class X,
class Y>
231 const __task &task = std::get<1>(*it);
233 if (&*task == task_id) {
234 std::get<0>(*it) = std::chrono::steady_clock::now() + duration;
247 if (std::get<0>(*it) > std::get<0>(*prev)) {
248 std::swap(*it, *prev);
267 const __task &task = std::get<1>(*it);
269 if (&*task == task_id) {
285 std::optional<std::pair<__time_point, __task>>
pop(
task_id_t task_id) {
288 auto pos = std::ranges::find_if(
_timer_tasks, [&task_id](
const auto &t) {
289 return t.second.get() == task_id;
296 return std::move(*pos);
304 std::optional<__task>
pop() {
338 std::optional<__time_point>
next() {
349 template<
class Function>
350 std::unique_ptr<_ImplBase> toRunnable(Function &&f) {
351 return std::make_unique<_Impl<Function>>(std::forward<Function &&>(f));
Queued delayed task identifier paired with its future result.
Definition task_pool.h:90
std::future< R > future
Future that resolves with the timer task result.
Definition task_pool.h:93
task_id_t task_id
Task ID.
Definition task_pool.h:92
timer_task_t(task_id_t task_id, std::future< R > &future)
Track a queued timer task and the future for its result.
Definition task_pool.h:101
bool cancel(task_id_t task_id)
Cancel a queued or delayed task before it runs.
Definition task_pool.h:262
std::optional< __task > pop()
Remove and return the next queued item, waiting when requested.
Definition task_pool.h:304
TaskPool & operator=(TaskPool &&other) noexcept
Assign state from another instance while preserving ownership semantics.
Definition task_pool.h:131
TaskPool(TaskPool &&other) noexcept
Move queued tasks and timers from another task pool.
Definition task_pool.h:120
std::chrono::steady_clock::time_point __time_point
Steady-clock timestamp used to schedule delayed tasks.
Definition task_pool.h:84
_ImplBase * task_id_t
Stable pointer used to identify a queued task.
Definition task_pool.h:79
void pushDelayed(std::pair< __time_point, __task > &&task)
Queue a task that becomes runnable after a delay.
Definition task_pool.h:171
std::optional< std::pair< __time_point, __task > > pop(task_id_t task_id)
Remove and return the next queued item, waiting when requested.
Definition task_pool.h:285
std::unique_ptr< _ImplBase > __task
Type-erased task owned by the task pool.
Definition task_pool.h:75
std::optional< __time_point > next()
Return the next timer task ready for execution.
Definition task_pool.h:338
std::vector< std::pair< __time_point, __task > > _timer_tasks
Delayed tasks sorted by their due time.
Definition task_pool.h:109
std::deque< __task > _tasks
Immediate tasks waiting for worker execution.
Definition task_pool.h:108
auto push(Function &&newTask, Args &&...args)
Queue work for asynchronous execution.
Definition task_pool.h:146
void delay(task_id_t task_id, std::chrono::duration< X, Y > duration)
Definition task_pool.h:226
auto pushDelayed(Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
Definition task_pool.h:192
std::mutex _task_mutex
Mutex protecting task queues and worker wakeups.
Definition task_pool.h:110
bool ready()
Check whether the task pool has active workers.
Definition task_pool.h:327
Type-erased task interface stored by the task pool.
Definition task_pool.h:27
virtual void run()=0
Execute the queued task body.
virtual ~_ImplBase()=default
Destroy a queued callable through its polymorphic base.
void run() override
Execute the queued task body.
Definition task_pool.h:62
_Impl(Function &&f)
Explicitly store a concrete callable behind the task type-erasure interface.
Definition task_pool.h:55
Declarations for the MoveByCopy utility class.
Functions for handling command line arguments.
Definition entry_handler.cpp:41
Declarations for utility functions.