Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
util::AsyncTask< Clock > Class Template Referenceabstract

An abstract class that can be overridden to run long-running tasks and encapsulate task-related data. More...

#include <scheduler.h>

Inheritance diagram for util::AsyncTask< Clock >:
Collaboration diagram for util::AsyncTask< Clock >:

Public Member Functions

 AsyncTask (const std::optional< std::string > &name=std::nullopt)
 Construct a new task.
 
 AsyncTask (const AsyncTask &)=delete
 
AsyncTaskoperator= (const AsyncTask &)=delete
 
virtual void start ()
 Start the task.
 
virtual void stop ()
 Stop the task and wait for it to finish.
 
bool isRunning ()
 Check if the task is running.
 

Protected Member Functions

virtual void task (std::unique_lock< std::mutex > &lock)=0
 The long-running task, overridden by client code.
 
bool isRunningInternal ()
 Version of AsyncTask::isRunning() that does no synchronization.
 
bool wait_until (std::unique_lock< std::mutex > &lock, const std::chrono::time_point< Clock > &tp)
 Wait until the specified time point, or until the task has been stopped.
 
template<typename Rep, typename Period>
bool wait_for (std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &dur)
 Wait for a given duration, or until the task has been stopped.
 
void wait_until_done (std::unique_lock< std::mutex > &lock)
 Wait until the task has been stopped.
 
void notify () override
 Not for use by client code.
 

Friends

void util::impl::notifyScheduler (AsyncTask &)
 

Detailed Description

template<typename Clock = std::chrono::steady_clock>
class util::AsyncTask< Clock >

An abstract class that can be overridden to run long-running tasks and encapsulate task-related data.

Client code should use this class by deriving from it and overriding AsyncTask::task.

For simpler tasks that just require a function to be run periodically, consider PeriodicTask.

Template Parameters
ClockThe clock to use for timing.

Constructor & Destructor Documentation

◆ AsyncTask()

template<typename Clock = std::chrono::steady_clock>
util::AsyncTask< Clock >::AsyncTask ( const std::optional< std::string > & name = std::nullopt)
inline

Construct a new task.

Parameters
nameThe name of this task, for logging purposes.

Member Function Documentation

◆ isRunning()

template<typename Clock = std::chrono::steady_clock>
bool util::AsyncTask< Clock >::isRunning ( )
inline

Check if the task is running.

Returns
If the task is currently running.

◆ isRunningInternal()

template<typename Clock = std::chrono::steady_clock>
bool util::AsyncTask< Clock >::isRunningInternal ( )
inlineprotected

Version of AsyncTask::isRunning() that does no synchronization.

This is useful if called from AsyncTask::task(), to prevent deadlocks.

Returns
If the task is currently running.

◆ notify()

template<typename Clock = std::chrono::steady_clock>
void util::AsyncTask< Clock >::notify ( )
inlineoverrideprotectedvirtual

Not for use by client code.

Implements util::impl::Notifiable.

Reimplemented in util::PeriodicTask< Clock >.

◆ start()

template<typename Clock = std::chrono::steady_clock>
virtual void util::AsyncTask< Clock >::start ( )
inlinevirtual

Start the task.

If the task is already running, do nothing.

Reimplemented in net::mc::tasks::PowerRepeatTask.

◆ stop()

template<typename Clock = std::chrono::steady_clock>
virtual void util::AsyncTask< Clock >::stop ( )
inlinevirtual

Stop the task and wait for it to finish.

If the task is not running, do nothing.

Reimplemented in net::mc::tasks::PowerRepeatTask.

◆ task()

template<typename Clock = std::chrono::steady_clock>
virtual void util::AsyncTask< Clock >::task ( std::unique_lock< std::mutex > & lock)
protectedpure virtual

The long-running task, overridden by client code.

If a task wants to stop itself, it can just return.

Parameters
lockThe lock on the private internal state of the AsyncTask. Client code should generally not use this except for the wait_until_xxx methods.

Implemented in net::mc::tasks::CameraStreamTask, and util::PeriodicTask< Clock >.

◆ wait_for()

template<typename Clock = std::chrono::steady_clock>
template<typename Rep, typename Period>
bool util::AsyncTask< Clock >::wait_for ( std::unique_lock< std::mutex > & lock,
const std::chrono::duration< Rep, Period > & dur )
inlineprotected

Wait for a given duration, or until the task has been stopped.

Parameters
lockThe lock passed to AsyncTask::task.
tpThe duration of time to wait for.
Returns
true iff the task was stopped while waiting.

◆ wait_until()

template<typename Clock = std::chrono::steady_clock>
bool util::AsyncTask< Clock >::wait_until ( std::unique_lock< std::mutex > & lock,
const std::chrono::time_point< Clock > & tp )
inlineprotected

Wait until the specified time point, or until the task has been stopped.

Parameters
lockThe lock passed to AsyncTask::task.
tpThe time point to wait until.
Returns
true iff the task was stopped while waiting.

◆ wait_until_done()

template<typename Clock = std::chrono::steady_clock>
void util::AsyncTask< Clock >::wait_until_done ( std::unique_lock< std::mutex > & lock)
inlineprotected

Wait until the task has been stopped.

Parameters
lockThe lock passed to AsyncTask::task.

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