Shaka Player Embedded
Public Types | Public Member Functions | List of all members
shaka::TaskRunner Class Reference

#include <task_runner.h>

Public Types

using RunLoop = std::function< void()>
 

Public Member Functions

 TaskRunner (std::function< void(RunLoop)> wrapper, const util::Clock *clock, bool is_worker)
 
 ~TaskRunner ()
 
bool is_running () const
 
bool HasPendingWork () const
 
bool BelongsToCurrentThread () const
 
void Stop ()
 
void WaitUntilFinished ()
 
template<typename Func >
std::shared_future< impl::RetOf< Func > > InvokeOrSchedule (Func &&callback)
 
template<typename Func >
std::shared_ptr< ThreadEvent< impl::RetOf< Func > > > AddInternalTask (TaskPriority priority, const std::string &name, Func &&callback)
 
template<typename Func >
int AddTimer (uint64_t delay_ms, Func &&callback)
 
template<typename Func >
int AddRepeatedTimer (uint64_t delay_ms, Func &&callback)
 
void CancelTimer (int id)
 

Detailed Description

Schedules and manages tasks to be run on a worker thread. This manages a background thread to run the tasks. It is safe to call all these methods from any thread.

Definition at line 154 of file task_runner.h.

Member Typedef Documentation

§ RunLoop

using shaka::TaskRunner::RunLoop = std::function<void()>

Definition at line 156 of file task_runner.h.

Constructor & Destructor Documentation

§ TaskRunner()

shaka::TaskRunner::TaskRunner ( std::function< void(RunLoop)>  wrapper,
const util::Clock clock,
bool  is_worker 
)

Definition at line 42 of file task_runner.cc.

§ ~TaskRunner()

shaka::TaskRunner::~TaskRunner ( )

Definition at line 55 of file task_runner.cc.

Member Function Documentation

§ AddInternalTask()

template<typename Func >
std::shared_ptr<ThreadEvent<impl::RetOf<Func> > > shaka::TaskRunner::AddInternalTask ( TaskPriority  priority,
const std::string &  name,
Func &&  callback 
)
inline

Registers an internal task to be called on the worker thread. This callback will be given a higher priority than timers.

See also
AddTimer
Parameters
priorityThe priority of the task. Higher priority tasks will run before lower priority tasks even if the higher task is registered later.
nameThe name of the new task, used for debugging.
callbackThe callback object.
Returns
The task ID and a future that will hold the results.

Definition at line 219 of file task_runner.h.

§ AddRepeatedTimer()

template<typename Func >
int shaka::TaskRunner::AddRepeatedTimer ( uint64_t  delay_ms,
Func &&  callback 
)
inline

Calls the given callback every |delay_ms| milliseconds on the worker thread.

See also
AddTimer
Parameters
delay_msThe time to wait until the callback is called (in milliseconds).
callbackThe callback object.
Returns
The task ID.

Definition at line 267 of file task_runner.h.

§ AddTimer()

template<typename Func >
int shaka::TaskRunner::AddTimer ( uint64_t  delay_ms,
Func &&  callback 
)
inline

Calls the given callback after the given delay on the worker thread.

Parameters
delay_msThe time to wait until the callback is called (in milliseconds).
callbackThe callback object.
Returns
The task ID.

Definition at line 243 of file task_runner.h.

§ BelongsToCurrentThread()

bool shaka::TaskRunner::BelongsToCurrentThread ( ) const
Returns
Whether the calling code is running on the worker thread.

Definition at line 68 of file task_runner.cc.

§ CancelTimer()

void shaka::TaskRunner::CancelTimer ( int  id)

Cancels a pending timer with the given ID.

Definition at line 94 of file task_runner.cc.

§ HasPendingWork()

bool shaka::TaskRunner::HasPendingWork ( ) const
Returns
Whether there are pending tasks.

Definition at line 59 of file task_runner.cc.

§ InvokeOrSchedule()

template<typename Func >
std::shared_future<impl::RetOf<Func> > shaka::TaskRunner::InvokeOrSchedule ( Func &&  callback)
inline

If called from the thread this manages, the given callback is invoked synchronously; otherwise it is scheduled as an internal task.

Parameters
callbackThe callback object.
Returns
A future for when the task is completed.

Definition at line 192 of file task_runner.h.

§ is_running()

bool shaka::TaskRunner::is_running ( ) const
inline
Returns
Whether the background thread is running.

Definition at line 163 of file task_runner.h.

§ Stop()

void shaka::TaskRunner::Stop ( )

Stops the worker thread. Can only be called when running. Will stop any pending tasks and will block until the worker thread is stopped.

Definition at line 72 of file task_runner.cc.

§ WaitUntilFinished()

void shaka::TaskRunner::WaitUntilFinished ( )

Blocks the calling thread until the worker has no more work to do.

Definition at line 87 of file task_runner.cc.


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