22namespace task_pool_util {
36 virtual void run() = 0;
42 template<
class Function>
53 _func(std::forward<Function>(f)) {
72 typedef std::unique_ptr<_ImplBase>
__task;
118 _tasks {std::move(other._tasks)},
129 std::swap(
_tasks, other._tasks);
142 template<
class Function,
class... Args>
144 static_assert(std::is_invocable_v<Function, Args &&...>,
"arguments don't match the function");
146 using __return = std::invoke_result_t<Function, Args &&...>;
147 using task_t = std::packaged_task<__return()>;
149 auto bind = [task = std::forward<Function>(newTask), tuple_args = std::make_tuple(std::forward<Args>(
args)...)]()
mutable {
150 return std::apply(task, std::move(tuple_args));
153 task_t task(std::move(bind));
155 auto future = task.get_future();
158 _tasks.emplace_back(toRunnable(std::move(task)));
173 if (std::get<0>(*it) < task.first) {
178 _timer_tasks.emplace(it, task.first, std::move(task.second));
188 template<
class Function,
class X,
class Y,
class... Args>
189 auto pushDelayed(Function &&newTask, std::chrono::duration<X, Y> duration, Args &&...
args) {
190 static_assert(std::is_invocable_v<Function, Args &&...>,
"arguments don't match the function");
192 using __return = std::invoke_result_t<Function, Args &&...>;
193 using task_t = std::packaged_task<__return()>;
196 if constexpr (std::is_floating_point_v<X>) {
197 time_point = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
199 time_point = std::chrono::steady_clock::now() + duration;
202 auto bind = [task = std::forward<Function>(newTask), tuple_args = std::make_tuple(std::forward<Args>(
args)...)]()
mutable {
203 return std::apply(task, std::move(tuple_args));
206 task_t task(std::move(bind));
208 auto future = task.get_future();
209 auto runnable = toRunnable(std::move(task));
213 pushDelayed(std::pair {time_point, std::move(runnable)});
222 template<
class X,
class Y>
228 const __task &task = std::get<1>(*it);
230 if (&*task == task_id) {
231 std::get<0>(*it) = std::chrono::steady_clock::now() + duration;
244 if (std::get<0>(*it) > std::get<0>(*prev)) {
245 std::swap(*it, *prev);
264 const __task &task = std::get<1>(*it);
266 if (&*task == task_id) {
282 std::optional<std::pair<__time_point, __task>>
pop(
task_id_t task_id) {
286 return t.second.get() == task_id;
293 return std::move(*pos);
301 std::optional<__task>
pop() {
335 std::optional<__time_point>
next() {
346 template<
class Function>
347 std::unique_ptr<_ImplBase> toRunnable(Function &&f) {
348 return std::make_unique<_Impl<Function>>(std::forward<Function &&>(f));
Queued delayed task identifier paired with its future result.
Definition task_pool.h:87
std::future< R > future
Future that resolves with the timer task result.
Definition task_pool.h:90
task_id_t task_id
Task ID.
Definition task_pool.h:89
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:98
Queue of immediate and delayed tasks executed by worker threads.
Definition task_pool.h:67
bool cancel(task_id_t task_id)
Cancel a queued or delayed task before it runs.
Definition task_pool.h:259
std::optional< __task > pop()
Remove and return the next queued item, waiting when requested.
Definition task_pool.h:301
_ImplBase * task_id_t
Stable pointer used to identify a queued task.
Definition task_pool.h:76
TaskPool & operator=(TaskPool &&other) noexcept
Assign state from another instance while preserving ownership semantics.
Definition task_pool.h:128
TaskPool(TaskPool &&other) noexcept
Move queued tasks and timers from another task pool.
Definition task_pool.h:117
std::unique_ptr< _ImplBase > __task
Type-erased task owned by the task pool.
Definition task_pool.h:72
void pushDelayed(std::pair< __time_point, __task > &&task)
Queue a task that becomes runnable after a delay.
Definition task_pool.h:168
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:282
std::optional< __time_point > next()
Return the next timer task ready for execution.
Definition task_pool.h:335
std::vector< std::pair< __time_point, __task > > _timer_tasks
Delayed tasks sorted by their due time.
Definition task_pool.h:106
std::deque< __task > _tasks
Immediate tasks waiting for worker execution.
Definition task_pool.h:105
auto push(Function &&newTask, Args &&...args)
Queue work for asynchronous execution.
Definition task_pool.h:143
void delay(task_id_t task_id, std::chrono::duration< X, Y > duration)
Definition task_pool.h:223
auto pushDelayed(Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
Definition task_pool.h:189
std::chrono::steady_clock::time_point __time_point
Steady-clock timestamp used to schedule delayed tasks.
Definition task_pool.h:81
std::mutex _task_mutex
Mutex protecting task queues and worker wakeups.
Definition task_pool.h:107
bool ready()
Check whether the task pool has active workers.
Definition task_pool.h:324
Type-erased task interface stored by the task pool.
Definition task_pool.h:27
virtual void run()=0
Execute the queued task body.
Concrete task wrapper that stores and invokes a callable.
Definition task_pool.h:43
void run() override
Execute the queued task body.
Definition task_pool.h:59
_Impl(Function &&f)
Store a concrete callable behind the task type-erasure interface.
Definition task_pool.h:52
Declarations for the MoveByCopy utility class.
Functions for handling command line arguments.
Definition entry_handler.cpp:37
Declarations for utility functions.