RSC  0.17.1
rsc::threading::RepetitiveTask Class Referenceabstract

A task that already provides a base frame for common logic of interruptible tasks. More...

#include <RepetitiveTask.h>

Inheritance diagram for rsc::threading::RepetitiveTask:
Inheritance graph
Collaboration diagram for rsc::threading::RepetitiveTask:
Collaboration graph

Public Member Functions

 RepetitiveTask ()
 
virtual ~RepetitiveTask ()
 
virtual void pre ()
 A method called before each iteration of the task. More...
 
virtual void execute ()=0
 This method is invoked to perform the real work of the task. More...
 
virtual void post ()
 A method called after each iteration of the task. More...
 
virtual void cancel ()
 Interrupts the task. More...
 
virtual bool isCancelRequested ()
 Tells whether the task was interrupted by a call to cancel. More...
 
virtual bool continueExec ()
 Tells whether the #exec method shall be called another time. More...
 
void run ()
 Performs the real task work orchestrating the iterative loop with the different hook methods and so on. More...
 
virtual void waitDone ()
 Waits for the execution of this task to finish. More...
 
virtual bool isDone ()
 Indicates whether the task finished execution, either through being canceled or through successfully ending. More...
 
- Public Member Functions inherited from rsc::threading::Task
virtual ~Task ()
 

Protected Attributes

boost::recursive_mutex doneMutex
 
boost::condition doneCondition
 

Private Member Functions

void markDone ()
 
void timerBeforeCycle ()
 
void timerAfterCycle ()
 

Private Attributes

volatile bool cancelRequest
 
volatile bool done
 
rsc::logging::LoggerPtr logger
 
boost::timer timer
 

Friends

std::ostream & operator<< (std::ostream &out, const RepetitiveTask &t)
 

Detailed Description

A task that already provides a base frame for common logic of interruptible tasks.

This is a loop that executes at least once. More times are possible if continueExec returns true after a loop. This is a common pattern for interruptible tasks.

In each iteration for a task, first a hook is called before the real work (pre), then the real work in that iteration is executed with #exec, and afterwards a post hook (post) is called. If another iteration shall be performed, continueExec has to return true (defaults to false).

A default flag mechanism to cancel a task is provided and incorporated into continueExec.

Author
swrede
jwienke

Definition at line 59 of file RepetitiveTask.h.

Constructor & Destructor Documentation

rsc::threading::RepetitiveTask::RepetitiveTask ( )

Definition at line 34 of file RepetitiveTask.cpp.

rsc::threading::RepetitiveTask::~RepetitiveTask ( )
virtual

Definition at line 39 of file RepetitiveTask.cpp.

Member Function Documentation

void rsc::threading::RepetitiveTask::cancel ( )
virtual

Interrupts the task.

The default implementation sets a boolean flag that can be checked with isCancelRequested.

Implements rsc::threading::Task.

Definition at line 50 of file RepetitiveTask.cpp.

References cancelRequest.

bool rsc::threading::RepetitiveTask::continueExec ( )
virtual

Tells whether the #exec method shall be called another time.

Do not forget to incorporate the cancel mechanism while overriding this method if the #exec method does not do this itself.

Returns
true of iterations of #exec calls shall continue, else false, default implementation returns true unless the task is canceled.

Reimplemented in rsc::threading::PeriodicTask.

Definition at line 58 of file RepetitiveTask.cpp.

References isCancelRequested().

Referenced by run().

Here is the call graph for this function:

Here is the caller graph for this function:

virtual void rsc::threading::RepetitiveTask::execute ( )
pure virtual

This method is invoked to perform the real work of the task.

Referenced by run().

Here is the caller graph for this function:

bool rsc::threading::RepetitiveTask::isCancelRequested ( )
virtual

Tells whether the task was interrupted by a call to cancel.

The default implementation returns the value of a boolean flag. Override this for more complex logic but don't forget to also override cancel then.

Returns
true if cancel was called, else false

Implements rsc::threading::Task.

Definition at line 54 of file RepetitiveTask.cpp.

References cancelRequest.

Referenced by rsc::threading::PeriodicTask::continueExec(), continueExec(), and run().

Here is the caller graph for this function:

bool rsc::threading::RepetitiveTask::isDone ( )
virtual

Indicates whether the task finished execution, either through being canceled or through successfully ending.

If true is returned, the run method of this task will not perform anything anymore.

Returns
true if run will not perform anything anymore

Implements rsc::threading::Task.

Definition at line 108 of file RepetitiveTask.cpp.

References done.

void rsc::threading::RepetitiveTask::markDone ( )
private

Definition at line 92 of file RepetitiveTask.cpp.

References done, doneCondition, and doneMutex.

Referenced by run().

Here is the caller graph for this function:

void rsc::threading::RepetitiveTask::post ( )
virtual

A method called after each iteration of the task.

The default methods logs timing information. Use it for comparable tasks only.

Definition at line 46 of file RepetitiveTask.cpp.

References timerAfterCycle().

Referenced by run().

Here is the call graph for this function:

Here is the caller graph for this function:

void rsc::threading::RepetitiveTask::pre ( )
virtual

A method called before each iteration of the task.

The default implementation starts a timer for performance measurements. Override this for comparable tasks.

Definition at line 42 of file RepetitiveTask.cpp.

References timerBeforeCycle().

Referenced by run().

Here is the call graph for this function:

Here is the caller graph for this function:

void rsc::threading::RepetitiveTask::run ( )
virtual

Performs the real task work orchestrating the iterative loop with the different hook methods and so on.

Implements rsc::threading::Task.

Definition at line 62 of file RepetitiveTask.cpp.

References continueExec(), execute(), isCancelRequested(), logger, markDone(), post(), pre(), RSCDEBUG, and RSCTRACE.

Here is the call graph for this function:

void rsc::threading::RepetitiveTask::timerAfterCycle ( )
private

Definition at line 116 of file RepetitiveTask.cpp.

References logger, RSCTRACE, and timer.

Referenced by post().

Here is the caller graph for this function:

void rsc::threading::RepetitiveTask::timerBeforeCycle ( )
private

Definition at line 112 of file RepetitiveTask.cpp.

References timer.

Referenced by pre().

Here is the caller graph for this function:

void rsc::threading::RepetitiveTask::waitDone ( )
virtual

Waits for the execution of this task to finish.

This may either be caused by a successful completion of the task or by being canceled. This method must block until run exits or it is sure that run will not be called anymore.

Undefined behavior might occur if this method is called before the task is scheduled. I.e. a calling thread might block infinitely.

Implements rsc::threading::Task.

Definition at line 98 of file RepetitiveTask.cpp.

References done, doneCondition, doneMutex, logger, and RSCDEBUG.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const RepetitiveTask t 
)
friend

Definition at line 121 of file RepetitiveTask.cpp.

Member Data Documentation

volatile bool rsc::threading::RepetitiveTask::cancelRequest
private

Definition at line 134 of file RepetitiveTask.h.

Referenced by cancel(), isCancelRequested(), and rsc::threading::operator<<().

volatile bool rsc::threading::RepetitiveTask::done
private

Definition at line 135 of file RepetitiveTask.h.

Referenced by isDone(), markDone(), and waitDone().

boost::condition rsc::threading::RepetitiveTask::doneCondition
protected

Definition at line 124 of file RepetitiveTask.h.

Referenced by markDone(), and waitDone().

boost::recursive_mutex rsc::threading::RepetitiveTask::doneMutex
mutableprotected

Definition at line 123 of file RepetitiveTask.h.

Referenced by markDone(), and waitDone().

rsc::logging::LoggerPtr rsc::threading::RepetitiveTask::logger
private

Definition at line 137 of file RepetitiveTask.h.

Referenced by run(), timerAfterCycle(), and waitDone().

boost::timer rsc::threading::RepetitiveTask::timer
private

Definition at line 139 of file RepetitiveTask.h.

Referenced by timerAfterCycle(), and timerBeforeCycle().


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