CppELib 1.7.0
Loading...
Searching...
No Matches
Thread.h
Go to the documentation of this file.
1#ifndef OS_WRAPPER_THREAD_H_INCLUDED
2#define OS_WRAPPER_THREAD_H_INCLUDED
3
4#include <cstddef>
5#include "Timeout.h"
6#include "OSWrapperError.h"
7
8namespace OSWrapper {
9
10class Runnable;
11class ThreadFactory;
12
19void registerThreadFactory(ThreadFactory* factory);
20
50class Thread {
51protected:
52 explicit Thread(Runnable* r)
53 : m_runnable(r), m_uncaughtExceptionHandler(0) {}
54 virtual ~Thread() {}
55
56 void threadMain();
57
58public:
67 public:
74 virtual void handle(Thread* t, const char* msg) = 0;
75 };
76
77 static const int INHERIT_PRIORITY;
78
84
90
102 static Thread* create(Runnable* r, int priority = INHERIT_PRIORITY, std::size_t stackSize = 0U, void* stackAddress = 0, const char* name = "");
103
110 static void destroy(Thread* t);
111
116 static void sleep(unsigned long millis);
117
121 static void yield();
122
129 static Thread* getCurrentThread();
130
137 static int getMaxPriority();
138
145 static int getMinPriority();
146
153 static int getNormalPriority();
154
161 static int getHighestPriority();
162
169 static int getLowestPriority();
170
181 static int getPriorityHigherThan(int basePriority, int d);
182
191 virtual void start() = 0;
192
200 virtual Error wait() = 0;
201
209 virtual Error tryWait() = 0;
210
221 virtual Error timedWait(Timeout tmout) = 0;
222
228 virtual bool isFinished() const = 0;
229
234 virtual void setName(const char* name) = 0;
235
240 virtual const char* getName() const = 0;
241
246 virtual void setPriority(int priority) = 0;
247
252 virtual int getPriority() const = 0;
253
258 virtual int getInitialPriority() const = 0;
259
264 virtual std::size_t getStackSize() const = 0;
265
270 virtual void* getNativeHandle() = 0;
271
280
286
287private:
288 Runnable* m_runnable;
289 UncaughtExceptionHandler* m_uncaughtExceptionHandler;
290 static UncaughtExceptionHandler* m_defaultUncaughtExceptionHandler;
291
292 void handleException(const char* msg);
293
294};
295
296}
297
298#endif // OS_WRAPPER_THREAD_H_INCLUDED
Interface for implementing an active class.
Definition Runnable.h:9
Interface for handling uncaught exception.
Definition Thread.h:66
virtual ~UncaughtExceptionHandler()
Definition Thread.h:68
virtual void handle(Thread *t, const char *msg)=0
Handle uncaught exception thrown in the Thread.
Abstract class that has functions of common RTOS's thread.
Definition Thread.h:50
virtual bool isFinished() const =0
Return true if this thread is in the FINISHED state.
void threadMain()
Common thread main method.
Definition Thread.cpp:125
static int getMaxPriority()
Get the maximum value of RTOS's thread priority.
Definition Thread.cpp:181
static int getPriorityHigherThan(int basePriority, int d)
Get the thread priority of RTOS higher than basePriority by d.
Definition Thread.cpp:211
virtual Error wait()=0
Block the current thread until this thread transits to the FINISHED state.
UncaughtExceptionHandler * getUncaughtExceptionHandler() const
Get the UncaughtExceptionHandler for this Thread.
Definition Thread.cpp:98
virtual void * getNativeHandle()=0
Get the native handle of RTOS's thread.
virtual void start()=0
Transit this thread to the READY state.
static void sleep(unsigned long millis)
Sleep the current thread while the time that is set by argument.
Definition Thread.cpp:163
static const int INHERIT_PRIORITY
Use when set the same priority as the caller thread.
Definition Thread.h:77
virtual int getPriority() const =0
Get this thread priority.
static int getHighestPriority()
Get the highest thread priority of RTOS.
Definition Thread.cpp:199
static void destroy(Thread *t)
Destroy a Thread object.
Definition Thread.cpp:156
static UncaughtExceptionHandler * getDefaultUncaughtExceptionHandler()
Get the default UncaughtExceptionHandler.
Definition Thread.cpp:88
static int getLowestPriority()
Get the lowest thread priority of RTOS.
Definition Thread.cpp:205
virtual std::size_t getStackSize() const =0
Get the stack size of this thread.
virtual ~Thread()
Definition Thread.h:54
virtual const char * getName() const =0
Get the object's name.
virtual void setName(const char *name)=0
Set the object's name.
static int getMinPriority()
Get the minimum value of RTOS's thread priority.
Definition Thread.cpp:187
static Thread * getCurrentThread()
Get the current running Thread object.
Definition Thread.cpp:175
static Thread * create(Runnable *r, int priority=INHERIT_PRIORITY, std::size_t stackSize=0U, void *stackAddress=0, const char *name="")
Create a Thread object.
Definition Thread.cpp:147
virtual Error tryWait()=0
Query without blocking whether this thread is in the FINISHED state.
static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler *handler)
Set the default UncaughtExceptionHandler for all the Thread.
Definition Thread.cpp:83
static int getNormalPriority()
Get the middle priority between the minimum and maximum value.
Definition Thread.cpp:193
virtual void setPriority(int priority)=0
Change this thread priority.
virtual int getInitialPriority() const =0
Get this thread priority when this thread was created.
void setUncaughtExceptionHandler(UncaughtExceptionHandler *handler)
Set the UncaughtExceptionHandler for this Thread.
Definition Thread.cpp:93
Thread(Runnable *r)
Definition Thread.h:52
static void yield()
Transit the current thread from the RUNNING state to the READY state to give other thread the RUNNING...
Definition Thread.cpp:169
virtual Error timedWait(Timeout tmout)=0
Block the current thread until this thread transits to the FINISHED state but only within the limited...
Value object for the timeout.
Definition Timeout.h:11
OSWrapper provides abstract C++ interface of common RTOS: thread, mutex, event flag,...
Definition EventFlag.cpp:5
Error
Kinds of errors of the OS objects.
Definition OSWrapperError.h:9
void registerThreadFactory(ThreadFactory *factory)
Register the ThreadFactory.
Definition Thread.cpp:75