CppELib 1.7.0
Loading...
Searching...
No Matches
OSWrapper::Thread Class Referenceabstract

Abstract class that has functions of common RTOS's thread. More...

#include <Thread.h>

Classes

class  UncaughtExceptionHandler
 Interface for handling uncaught exception. More...
 

Public Member Functions

virtual void start ()=0
 Transit this thread to the READY state.
 
virtual Error wait ()=0
 Block the current thread until this thread transits to the FINISHED state.
 
virtual Error tryWait ()=0
 Query without blocking whether this thread is in the FINISHED state.
 
virtual Error timedWait (Timeout tmout)=0
 Block the current thread until this thread transits to the FINISHED state but only within the limited time.
 
virtual bool isFinished () const =0
 Return true if this thread is in the FINISHED state.
 
virtual void setName (const char *name)=0
 Set the object's name.
 
virtual const char * getName () const =0
 Get the object's name.
 
virtual void setPriority (int priority)=0
 Change this thread priority.
 
virtual int getPriority () const =0
 Get this thread priority.
 
virtual int getInitialPriority () const =0
 Get this thread priority when this thread was created.
 
virtual std::size_t getStackSize () const =0
 Get the stack size of this thread.
 
virtual void * getNativeHandle ()=0
 Get the native handle of RTOS's thread.
 
void setUncaughtExceptionHandler (UncaughtExceptionHandler *handler)
 Set the UncaughtExceptionHandler for this Thread.
 
UncaughtExceptionHandlergetUncaughtExceptionHandler () const
 Get the UncaughtExceptionHandler for this Thread.
 

Static Public Member Functions

static void setDefaultUncaughtExceptionHandler (UncaughtExceptionHandler *handler)
 Set the default UncaughtExceptionHandler for all the Thread.
 
static UncaughtExceptionHandlergetDefaultUncaughtExceptionHandler ()
 Get the default UncaughtExceptionHandler.
 
static Threadcreate (Runnable *r, int priority=INHERIT_PRIORITY, std::size_t stackSize=0U, void *stackAddress=0, const char *name="")
 Create a Thread object.
 
static void destroy (Thread *t)
 Destroy a Thread object.
 
static void sleep (unsigned long millis)
 Sleep the current thread while the time that is set by argument.
 
static void yield ()
 Transit the current thread from the RUNNING state to the READY state to give other thread the RUNNING state.
 
static ThreadgetCurrentThread ()
 Get the current running Thread object.
 
static int getMaxPriority ()
 Get the maximum value of RTOS's thread priority.
 
static int getMinPriority ()
 Get the minimum value of RTOS's thread priority.
 
static int getNormalPriority ()
 Get the middle priority between the minimum and maximum value.
 
static int getHighestPriority ()
 Get the highest thread priority of RTOS.
 
static int getLowestPriority ()
 Get the lowest thread priority of RTOS.
 
static int getPriorityHigherThan (int basePriority, int d)
 Get the thread priority of RTOS higher than basePriority by d.
 

Static Public Attributes

static const int INHERIT_PRIORITY = -1
 Use when set the same priority as the caller thread.
 

Protected Member Functions

 Thread (Runnable *r)
 
virtual ~Thread ()
 
void threadMain ()
 Common thread main method.
 

Detailed Description

Abstract class that has functions of common RTOS's thread.

This is the state machine diagram of Thread.

When the current thread calls some methods (for example Thread::wait(), Mutex::lock(), EventFlag::timedWait(), Thread::sleep(), etc), the state of current thread transits from RUNNING to WAITING. Then the waiting condition is satisfied or timed out, the state transits from WAITING to READY.

When the current thread runs through the end of Runnable::run(), the state of current thread transits from RUNNING to FINISHED.

Note
A Thread can not suspend, resume and stop other Thread.

Constructor & Destructor Documentation

◆ Thread()

OSWrapper::Thread::Thread ( Runnable r)
inlineexplicitprotected

◆ ~Thread()

virtual OSWrapper::Thread::~Thread ( )
inlineprotectedvirtual

Member Function Documentation

◆ create()

Thread * OSWrapper::Thread::create ( Runnable r,
int  priority = INHERIT_PRIORITY,
std::size_t  stackSize = 0U,
void *  stackAddress = 0,
const char *  name = "" 
)
static

Create a Thread object.

Parameters
rPointer of Runnable object
priorityThread priority. If INHERIT_PRIORITY then sets the priority of the caller thread.
stackSizeStack size of thread. If zero then sets default stack size of RTOS.
stackAddressStack address of thread allocated by the caller. If null pointer then RTOS allocates the stack.
nameName of the object
Returns
If this method succeeds then returns a pointer of Thread object, else returns null pointer
Note
Created Thread object is in the FINISHED state. You need to call start() for the Thread object.

◆ destroy()

void OSWrapper::Thread::destroy ( Thread t)
static

Destroy a Thread object.

Parameters
tPointer of Thread object created by Thread::create()
Note
If t is null pointer, do nothing.

◆ getCurrentThread()

Thread * OSWrapper::Thread::getCurrentThread ( )
static

Get the current running Thread object.

Returns
Pointer of the current Thread object
Attention
If current running thread is not created by Thread::create(), returns null pointer.

◆ getDefaultUncaughtExceptionHandler()

Thread::UncaughtExceptionHandler * OSWrapper::Thread::getDefaultUncaughtExceptionHandler ( )
static

Get the default UncaughtExceptionHandler.

Returns
The default UncaughtExceptionHandler object

◆ getHighestPriority()

int OSWrapper::Thread::getHighestPriority ( )
static

Get the highest thread priority of RTOS.

Returns
The highest thread priority
Note
Return value may be the maximum or minimum value by kinds of RTOS.

◆ getInitialPriority()

virtual int OSWrapper::Thread::getInitialPriority ( ) const
pure virtual

Get this thread priority when this thread was created.

Returns
Initial thread priority

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ getLowestPriority()

int OSWrapper::Thread::getLowestPriority ( )
static

Get the lowest thread priority of RTOS.

Returns
The lowest thread priority
Note
Return value may be the maximum or minimum value by kinds of RTOS.

◆ getMaxPriority()

int OSWrapper::Thread::getMaxPriority ( )
static

Get the maximum value of RTOS's thread priority.

Returns
The maximum value of thread priority
Note
Thread::getMinPriority() < Thread::getNormalPriority() < Thread::getMaxPriority()

◆ getMinPriority()

int OSWrapper::Thread::getMinPriority ( )
static

Get the minimum value of RTOS's thread priority.

Returns
The minimum value of thread priority
Note
Thread::getMinPriority() < Thread::getNormalPriority() < Thread::getMaxPriority()

◆ getName()

virtual const char * OSWrapper::Thread::getName ( ) const
pure virtual

Get the object's name.

Returns
The object's name

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ getNativeHandle()

virtual void * OSWrapper::Thread::getNativeHandle ( )
pure virtual

Get the native handle of RTOS's thread.

Returns
native handle

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ getNormalPriority()

int OSWrapper::Thread::getNormalPriority ( )
static

Get the middle priority between the minimum and maximum value.

Returns
The middle priority between the minimum and maximum value
Note
Thread::getMinPriority() < Thread::getNormalPriority() < Thread::getMaxPriority()

◆ getPriority()

virtual int OSWrapper::Thread::getPriority ( ) const
pure virtual

Get this thread priority.

Returns
Thread priority

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ getPriorityHigherThan()

int OSWrapper::Thread::getPriorityHigherThan ( int  basePriority,
int  d 
)
static

Get the thread priority of RTOS higher than basePriority by d.

Parameters
basePriorityBase priority
dDifference
Returns
If d is positive number, returns the priority higher than basePriority by d.
If d is negative number, returns the priority lower than basePriority by d.
If d is zero, returns basePriority.
Note
The range of return value is between getLowestPriority() and getHighestPriority().

◆ getStackSize()

virtual std::size_t OSWrapper::Thread::getStackSize ( ) const
pure virtual

Get the stack size of this thread.

Returns
stack size

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ getUncaughtExceptionHandler()

Thread::UncaughtExceptionHandler * OSWrapper::Thread::getUncaughtExceptionHandler ( ) const

Get the UncaughtExceptionHandler for this Thread.

Returns
The UncaughtExceptionHandler object

◆ isFinished()

virtual bool OSWrapper::Thread::isFinished ( ) const
pure virtual

Return true if this thread is in the FINISHED state.

Return values
trueThis thread is in the FINISHED state
falseThis thread is not in the FINISHED state

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ setDefaultUncaughtExceptionHandler()

void OSWrapper::Thread::setDefaultUncaughtExceptionHandler ( Thread::UncaughtExceptionHandler handler)
static

Set the default UncaughtExceptionHandler for all the Thread.

Parameters
handlerPointer of UncaughtExceptionHandler object

◆ setName()

virtual void OSWrapper::Thread::setName ( const char *  name)
pure virtual

Set the object's name.

Parameters
nameThe object's name

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ setPriority()

virtual void OSWrapper::Thread::setPriority ( int  priority)
pure virtual

Change this thread priority.

Parameters
priorityThread priority

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ setUncaughtExceptionHandler()

void OSWrapper::Thread::setUncaughtExceptionHandler ( Thread::UncaughtExceptionHandler handler)

Set the UncaughtExceptionHandler for this Thread.

Parameters
handlerPointer of UncaughtExceptionHandler object
Note
If set UncaughtExceptionHandler by this method then the default UncaughtExceptionHandler is not called when an exception is not caught.

◆ sleep()

void OSWrapper::Thread::sleep ( unsigned long  millis)
static

Sleep the current thread while the time that is set by argument.

Parameters
millisTime in milliseconds

◆ start()

virtual void OSWrapper::Thread::start ( )
pure virtual

Transit this thread to the READY state.

After this method, when this thread transits to the RUNNING state, this thread calls Runnable::run() specified at create().

Note
If this thread is already in the READY state, do nothing.
If this thread is finished, you can start() again.

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ threadMain()

void OSWrapper::Thread::threadMain ( )
protected

Common thread main method.

Note
Called in the concrete class derived from Thread.

◆ timedWait()

virtual Error OSWrapper::Thread::timedWait ( Timeout  tmout)
pure virtual

Block the current thread until this thread transits to the FINISHED state but only within the limited time.

Parameters
tmoutThe limited time
Return values
OKSuccess. This thread is in the FINISHED state
TimedOutThe limited time was elapsed
CalledByNonThreadCalled from non thread context (interrupt handler, timer, etc)
Note
If tmout is Timeout::POLLING then this method queries the state without blocking.
If tmout is Timeout::FOREVER then this method waits forever until the condition is satisfied.

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ tryWait()

virtual Error OSWrapper::Thread::tryWait ( )
pure virtual

Query without blocking whether this thread is in the FINISHED state.

Return values
OKThis thread is already in the FINISHED state
TimedOutThis thread is not in the FINISHED state
Note
Same as timedWait(Timeout::POLLING)

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ wait()

virtual Error OSWrapper::Thread::wait ( )
pure virtual

Block the current thread until this thread transits to the FINISHED state.

Return values
OKSuccess. This thread is in the FINISHED state
CalledByNonThreadCalled from non thread context (interrupt handler, timer, etc)
Note
Same as timedWait(Timeout::FOREVER)

Implemented in StdCppOSWrapper::StdCppThreadFactory::StdCppThread, and TestDoubleOSWrapper::TestDoubleThread.

◆ yield()

void OSWrapper::Thread::yield ( )
static

Transit the current thread from the RUNNING state to the READY state to give other thread the RUNNING state.

Member Data Documentation

◆ INHERIT_PRIORITY

const int OSWrapper::Thread::INHERIT_PRIORITY = -1
static

Use when set the same priority as the caller thread.


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