Sunshine master
Self-hosted game stream host for Moonlight.
task_pool_util::TaskPool Class Reference

Queue of immediate and delayed tasks executed by worker threads. More...

#include <src/task_pool.h>

Inheritance diagram for task_pool_util::TaskPool:
[legend]

Classes

class  timer_task_t
 Queued delayed task identifier paired with its future result. More...
 

Public Types

typedef std::unique_ptr< _ImplBase__task
 Type-erased task owned by the task pool.
 
typedef std::chrono::steady_clock::time_point __time_point
 Steady-clock timestamp used to schedule delayed tasks.
 
typedef _ImplBasetask_id_t
 Stable pointer used to identify a queued task.
 

Public Member Functions

bool cancel (task_id_t task_id)
 Cancel a queued or delayed task before it runs.
 
template<class X , class Y >
void delay (task_id_t task_id, std::chrono::duration< X, Y > duration)
 
std::optional< __time_pointnext ()
 Return the next timer task ready for execution.
 
TaskPooloperator= (TaskPool &&other) noexcept
 Assign state from another instance while preserving ownership semantics.
 
std::optional< __taskpop ()
 Remove and return the next queued item, waiting when requested.
 
std::optional< std::pair< __time_point, __task > > pop (task_id_t task_id)
 Remove and return the next queued item, waiting when requested.
 
template<class Function , class... Args>
auto push (Function &&newTask, Args &&...args)
 Queue work for asynchronous execution.
 
template<class Function , class X , class Y , class... Args>
auto pushDelayed (Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
 
void pushDelayed (std::pair< __time_point, __task > &&task)
 Queue a task that becomes runnable after a delay.
 
bool ready ()
 Check whether the task pool has active workers.
 
 TaskPool (TaskPool &&other) noexcept
 Move queued tasks and timers from another task pool.
 

Protected Attributes

std::mutex _task_mutex
 Mutex protecting task queues and worker wakeups.
 
std::deque< __task_tasks
 Immediate tasks waiting for worker execution.
 
std::vector< std::pair< __time_point, __task > > _timer_tasks
 Delayed tasks sorted by their due time.
 

Detailed Description

Queue of immediate and delayed tasks executed by worker threads.

Constructor & Destructor Documentation

◆ TaskPool()

task_pool_util::TaskPool::TaskPool ( TaskPool && other)
inlinenoexcept

Move queued tasks and timers from another task pool.

Parameters
otherPool whose queues and worker state are moved into this pool.

Member Function Documentation

◆ cancel()

bool task_pool_util::TaskPool::cancel ( task_id_t task_id)
inline

Cancel a queued or delayed task before it runs.

Parameters
task_idIdentifier returned when the task was queued.
Returns
True when the task was found and cancelled.

◆ delay()

template<class X , class Y >
void task_pool_util::TaskPool::delay ( task_id_t task_id,
std::chrono::duration< X, Y > duration )
inline
Parameters
task_idThe id of the task to delay.
durationThe delay before executing the task.

◆ next()

std::optional< __time_point > task_pool_util::TaskPool::next ( )
inline

Return the next timer task ready for execution.

Returns
Time point for the next queued timer task, or std::nullopt when none exist.

◆ operator=()

TaskPool & task_pool_util::TaskPool::operator= ( TaskPool && other)
inlinenoexcept

Assign state from another instance while preserving ownership semantics.

Parameters
otherSource object whose state is copied or moved into this object.
Returns
Reference or value produced by the operator.

◆ pop() [1/2]

std::optional< __task > task_pool_util::TaskPool::pop ( )
inline

Remove and return the next queued item, waiting when requested.

Returns
Removed queue item, or empty result when the queue is stopped or empty.

◆ pop() [2/2]

std::optional< std::pair< __time_point, __task > > task_pool_util::TaskPool::pop ( task_id_t task_id)
inline

Remove and return the next queued item, waiting when requested.

Parameters
task_idIdentifier returned when the task was queued.
Returns
Removed queue item, or empty result when the queue is stopped or empty.

◆ push()

template<class Function , class... Args>
auto task_pool_util::TaskPool::push ( Function && newTask,
Args &&... args )
inline

Queue work for asynchronous execution.

Parameters
newTaskNew task.
argsArguments forwarded to the callable or parser.
Returns
Identifier assigned to the queued task.

◆ pushDelayed() [1/2]

template<class Function , class X , class Y , class... Args>
auto task_pool_util::TaskPool::pushDelayed ( Function && newTask,
std::chrono::duration< X, Y > duration,
Args &&... args )
inline
Returns
An id to potentially delay the task.
Parameters
newTaskNew task.
durationDelay before the timed task should run.
argsArguments forwarded to the callable or parser.

◆ pushDelayed() [2/2]

void task_pool_util::TaskPool::pushDelayed ( std::pair< __time_point, __task > && task)
inline

Queue a task that becomes runnable after a delay.

Parameters
taskTask object to enqueue or execute.

◆ ready()

bool task_pool_util::TaskPool::ready ( )
inline

Check whether the task pool has active workers.

Returns
True when the task was found and cancelled.

The documentation for this class was generated from the following file: