PiImageStreamer
Loading...
Searching...
No Matches
SWE4211RPi::RunnableClass Class Referenceabstract

#include <RunnableClass.h>

Inheritance diagram for SWE4211RPi::RunnableClass:

Public Member Functions

 RunnableClass (std::string threadName, int priority=0)
 
virtual ~RunnableClass ()
 
virtual void resetThreadDiagnostics ()
 
virtual void printInformation ()
 
virtual double getCPUUsageInfo ()
 
virtual pid_t getThreadID ()
 
virtual void start () final
 
virtual void setPriority (int priority) final
 
virtual int getPriority () final
 
virtual void run ()=0
 
virtual bool isShutdown () final
 
virtual bool isStarted () final
 
virtual void stopThreadExecution ()
 
virtual void waitForShutdown ()
 

Static Public Member Functions

static void printThreads ()
 
static void resetAllThreadInformation ()
 

Protected Member Functions

virtual void startChildRunnables ()
 
virtual void stopChildRunnables ()
 

Protected Attributes

std::thread * myThread = NULL
 
pthread_t executingThread = 0
 
pid_t myOSThreadID = 0
 
std::string myName
 
std::atomic< bool > keepGoing {true}
 
std::atomic< bool > runCompleted {false}
 
std::atomic< bool > runStarted {false}
 
int priority = -1
 

Static Protected Attributes

static std::list< RunnableClass * > runningThreads
 

Private Member Functions

virtual void invokeRunMethod () final
 

Detailed Description

This is the runnable class, which mimics the runnable interface from Java. It is a virtual class which should not directly be instantiated.

Constructor & Destructor Documentation

◆ RunnableClass()

RunnableClass::RunnableClass ( std::string  threadName,
int  priority = 0 
)

This is the default constructor for the class.

Parameters
threadNameThis is the name of the thread in a human readable format.
priorityThis is the priority for the task. By default, will be 0 unless another priority is passed into the constructor.

This is the default constructor for the class. The thread name is padded or truncated to 23 characters so that the diagnostic table lines up, and the instance is added to the list of threads.

Parameters
threadNameThis is the name of the thread in a human readable format.
priorityThis is the priority for the task.

◆ ~RunnableClass()

RunnableClass::~RunnableClass ( )
virtual

This is the default destructor for the class. It deletes the thread object. The thread must have been joined (waitForShutdown()) or detached before the object is destroyed.

If the thread is not NULL, delete it.

Member Function Documentation

◆ getCPUUsageInfo()

double RunnableClass::getCPUUsageInfo ( )
virtual

This method will return the CPU usage for the given task. The usage will be as a percentage value.

Returns
The CPU usage as a percentage. A plain runnable class always returns 0.

This method will return the CPU usage for the given task. The usage will be as a percentage value.

Note that for a simple runnable class, we do not currently have this metric.

Reimplemented in SWE4211RPi::FreeRunningRepeatingTask, and SWE4211RPi::PeriodicTask.

Here is the caller graph for this function:

◆ getPriority()

int RunnableClass::getPriority ( )
finalvirtual

This method will obtain the priority set for this runnable class.

Returns
The return will be the set priority for the runnable class.
Here is the caller graph for this function:

◆ getThreadID()

pid_t RunnableClass::getThreadID ( )
virtual

This method will obtain the thread ID for this runnable.

Returns
The OS Thread ID.

◆ invokeRunMethod()

void RunnableClass::invokeRunMethod ( )
finalprivatevirtual

This private method initializes the runnable class. It is actually the method invoked when the thread starts. It applies the priority, records the thread IDs, and then calls the run method.

This private method initializes the runnable class. It is actually the method invoked when the thread starts. If a priority greater than 0 has been set, the thread is switched to the real time scheduler at that priority. The thread IDs are recorded, and then the run method is called.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isShutdown()

bool RunnableClass::isShutdown ( )
finalvirtual

This method will determine if the given thread is shutdown.

Returns
The return will be true if the thread is shutdown or false otherwise.

◆ isStarted()

bool RunnableClass::isStarted ( )
finalvirtual

This method will determine if the given runnable class has been started or not.

Returns
true if the task has been started. False otherwise.

◆ printInformation()

void RunnableClass::printInformation ( )
virtual

This method will print out information about the given thread. The info will be dependent upon the given thread.

Reimplemented in SWE4211RPi::FreeRunningRepeatingTask, and SWE4211RPi::PeriodicTask.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printThreads()

void RunnableClass::printThreads ( )
static

This method will print out to the console each of the registered threads, their thread ID's, and their diagnostics.

Here is the call graph for this function:

◆ resetAllThreadInformation()

void RunnableClass::resetAllThreadInformation ( )
static

This method will reset the thread information which is dynamic in nature and changes as the robot runs. This predominantly impacts threads which are not part of the Runnable class.

Here is the call graph for this function:

◆ resetThreadDiagnostics()

void RunnableClass::resetThreadDiagnostics ( )
virtual

This method will reset thread diagnostics back to their default values.

Reimplemented in SWE4211RPi::FreeRunningRepeatingTask, and SWE4211RPi::PeriodicTask.

Here is the caller graph for this function:

◆ run()

virtual void SWE4211RPi::RunnableClass::run ( )
pure virtual

This is the virtual run method. It will execute the given code that is to be executed by this class.

Implemented in SWE4211RPi::NetworkManager, SWE4211RPi::NetworkTransmissionManager, StatusDisplay, SWE4211RPi::FreeRunningRepeatingTask, SWE4211RPi::GenericThreadInfo, and SWE4211RPi::PeriodicTask.

Here is the caller graph for this function:

◆ setPriority()

void RunnableClass::setPriority ( int  priority)
finalvirtual

This method will set the priority for the given task, using the real time FIFO scheduler as well as setting the priority. If the thread has already started, the scheduler is also changed immediately.

Parameters
priorityThis is the priority for the task. It must be between 0 and 99, with 99 being the highest priority. Values above the scheduler maximum are clamped to the maximum.

This method will set the priority for the given task. If the thread has already started, the scheduler is also changed immediately. Note that sched_setscheduler(0, ...) applies to the calling thread, so the change at run time is only applied to this runnable when setPriority() is called from its own thread.

Parameters
priorityThis is the priority for the task. It must be between 0 and 99, with 99 being the highest priority. Values above the scheduler maximum are clamped to the maximum.
Here is the caller graph for this function:

◆ start()

void RunnableClass::start ( )
finalvirtual

This method is the start method. It is final and is not overridden in child classes. The purpose of this method is to start any child runnables and then instantiate a new thread that invokes the run method. The thread runs at the priority set with setPriority() (or passed to the constructor).

This method is the start method. The purpose of this method is to start any child runnables and then instantiate a new thread that invokes the run method. It does nothing if the run method has already started.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startChildRunnables()

void RunnableClass::startChildRunnables ( )
protectedvirtual

This method will start up any runnable objects which are contained within a class that implements the Runnable interface. If there are no other objects that are runnable, there is no need to override this method. However, if a child class contains an instance of a runnable object, then this method must be overridden in the derived class.

This default implementation does nothing.

Reimplemented in SWE4211RPi::FreeRunningRepeatingTask, and SWE4211RPi::PeriodicTask.

Here is the caller graph for this function:

◆ stopChildRunnables()

void RunnableClass::stopChildRunnables ( )
protectedvirtual

This method will stop any runnable objects which are contained within a class that implements the Runnable interface. If there are no other objects that are runnable, there is no need to override this method. However, if a child class contains an instance of a runnable object, then this method must be overridden in the derived class.

This default implementation does nothing.

Reimplemented in SWE4211RPi::FreeRunningRepeatingTask, and SWE4211RPi::PeriodicTask.

Here is the caller graph for this function:

◆ stopThreadExecution()

void RunnableClass::stopThreadExecution ( )
virtual

This method will stop the execution of the given class. It sets keepGoing to false and stops any child runnables. It does not wait for the thread to finish; use waitForShutdown() for that.

This method will stop the execution of the given class.

Stop the thread by changing the value of keepGoing if the thread is running. If there is a child class that has a Runnable object inside of it, then stop is called on that object through stopChildRunnables(). The check and the change are done in one atomic exchange so that if two threads (for example, main and this thread handling a shutdown broadcast) call this method at the same time, only one of them stops the child runnables.

Reimplemented in SWE4211RPi::PulsedGPIOPin, SWE4211RPi::NetworkManager, and SWE4211RPi::NetworkTransmissionManager.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ waitForShutdown()

void RunnableClass::waitForShutdown ( )
virtual

This method will block waiting for the given thread to terminate before continuing. This should be overridden if any child class has it's own runnable objects encapsulated within it.

This method will block waiting for the given thread to terminate before continuing. If a class that is derived from this class has a child class with a Runnable object, this method must be overridden to wait for the child method to shutdown.

If thread is joinable

Join the thread and wait for shutdown.

Member Data Documentation

◆ executingThread

pthread_t SWE4211RPi::RunnableClass::executingThread = 0
protected

This attribute is the executing thread. It is set when the thread is started and then is cached afterwards.

◆ keepGoing

std::atomic<bool> SWE4211RPi::RunnableClass::keepGoing {true}
protected

This variable will determine whether or not the given thread is to continue executing. If this goes to false, then the thread is to shutdown in an appropriate manner. It is atomic because it is written by other threads (for example, main calling stopThreadExecution()) while this thread reads it.

◆ myName

std::string SWE4211RPi::RunnableClass::myName
protected

This is the name of the thread. It is shown as a string so that it can easily be viewed in human-readable format.

◆ myOSThreadID

pid_t SWE4211RPi::RunnableClass::myOSThreadID = 0
protected

This is the thread id (tid) for this task.

◆ myThread

std::thread* SWE4211RPi::RunnableClass::myThread = NULL
protected

This is the instance of the thread that is to be executed by this class. At instantiation it is NULL. It will be instantiated when the start method of the class is invoked.

◆ priority

int SWE4211RPi::RunnableClass::priority = -1
protected

This is the priority of the given thread. A value of 0 or less indicates no change in the priority and to use the default (non real time) scheduler. For a runnable class, this must be set before the class starts. For other derived classes, it may be changeable at run time.

◆ runCompleted

std::atomic<bool> SWE4211RPi::RunnableClass::runCompleted {false}
protected

This variable will determine if the current Runnable class has completed execution and the run method has returned. It is atomic because it is written by this thread and read by other threads.

◆ runningThreads

std::list< RunnableClass * > RunnableClass::runningThreads
staticprotected

This is a list of all of the running threads which have been started by this set of libraries.

This is a class scoped variable which holds a list of every runnable that has been constructed. Entries are added by the constructor and are not removed.

◆ runStarted

std::atomic<bool> SWE4211RPi::RunnableClass::runStarted {false}
protected

This variable will determine whether or not the current task has been started or not. It is atomic because it is written by this thread and read by other threads.


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