CppELib 1.7.0
Loading...
Searching...
No Matches
StdCppThreadFactory.h
Go to the documentation of this file.
1#ifndef STDCPP_OS_WRAPPER_STDCPP_THREAD_FACTORY_H_INCLUDED
2#define STDCPP_OS_WRAPPER_STDCPP_THREAD_FACTORY_H_INCLUDED
3
5#include "OSWrapper/Thread.h"
6#include <thread>
7#include <mutex>
8#include <condition_variable>
9#include <unordered_map>
10
11namespace StdCppOSWrapper {
12
14protected:
16 protected:
19 std::size_t m_stackSize;
20 const char* m_name;
21
22 std::thread m_thread;
23 mutable std::mutex m_mutex;
24 private:
25 std::condition_variable m_condStarted;
26 std::condition_variable m_condFinished;
27 bool m_isActive;
28 bool m_endThreadRequested;
29 std::thread::id m_threadId;
30
31 static void threadEntry(StdCppThread* t);
32 void threadLoop();
33
34 public:
35 StdCppThread(OSWrapper::Runnable* r, int priority, std::size_t stackSize, const char* name);
36 virtual ~StdCppThread() {}
37
38 void beginThread();
39 void endThread();
40
41 virtual void start();
42 virtual OSWrapper::Error wait();
43 virtual OSWrapper::Error tryWait();
45 virtual bool isFinished() const;
46 virtual void setName(const char* name);
47 virtual const char* getName() const;
48 virtual void setPriority(int priority);
49 virtual int getPriority() const;
50 virtual int getInitialPriority() const;
51 virtual std::size_t getStackSize() const;
52 virtual void* getNativeHandle();
53
54 std::thread::id getId() const { return m_threadId; }
55 };
56
57public:
60
61private:
62 virtual OSWrapper::Thread* create(OSWrapper::Runnable* r, int priority, std::size_t stackSize, void* stackAddress, const char* name);
63 virtual void destroy(OSWrapper::Thread* t);
64 virtual void sleep(unsigned long millis);
65 virtual void yield();
66 virtual OSWrapper::Thread* getCurrentThread();
67 virtual int getMaxPriority() const;
68 virtual int getMinPriority() const;
69 virtual int getHighestPriority() const;
70 virtual int getLowestPriority() const;
71
74
75 virtual StdCppThread* createStdCppThread(OSWrapper::Runnable* r, int priority, std::size_t stackSize, const char* name);
76
77 std::unordered_map<std::thread::id, OSWrapper::Thread*> m_threadIdMap;
78 std::recursive_mutex m_mutex;
79};
80
81}
82
83#endif // STDCPP_OS_WRAPPER_STDCPP_THREAD_FACTORY_H_INCLUDED
Interface for implementing an active class.
Definition Runnable.h:9
Definition ThreadFactory.h:11
Abstract class that has functions of common RTOS's thread.
Definition Thread.h:50
Value object for the timeout.
Definition Timeout.h:11
Definition StdCppThreadFactory.h:15
void beginThread()
Definition StdCppThreadFactory.cpp:41
virtual OSWrapper::Error timedWait(OSWrapper::Timeout tmout)
Block the current thread until this thread transits to the FINISHED state but only within the limited...
Definition StdCppThreadFactory.cpp:83
virtual bool isFinished() const
Return true if this thread is in the FINISHED state.
Definition StdCppThreadFactory.cpp:97
std::mutex m_mutex
Definition StdCppThreadFactory.h:23
virtual void start()
Transit this thread to the READY state.
Definition StdCppThreadFactory.cpp:63
const char * m_name
Definition StdCppThreadFactory.h:20
virtual void * getNativeHandle()
Get the native handle of RTOS's thread.
Definition StdCppThreadFactory.cpp:156
void endThread()
Definition StdCppThreadFactory.cpp:51
std::size_t m_stackSize
Definition StdCppThreadFactory.h:19
std::thread::id getId() const
Definition StdCppThreadFactory.h:54
virtual OSWrapper::Error tryWait()
Query without blocking whether this thread is in the FINISHED state.
Definition StdCppThreadFactory.cpp:78
virtual int getPriority() const
Get this thread priority.
Definition StdCppThreadFactory.cpp:135
virtual OSWrapper::Error wait()
Block the current thread until this thread transits to the FINISHED state.
Definition StdCppThreadFactory.cpp:73
virtual int getInitialPriority() const
Get this thread priority when this thread was created.
Definition StdCppThreadFactory.cpp:141
int m_priority
Definition StdCppThreadFactory.h:17
std::thread m_thread
Definition StdCppThreadFactory.h:22
virtual void setPriority(int priority)
Change this thread priority.
Definition StdCppThreadFactory.cpp:115
virtual const char * getName() const
Get the object's name.
Definition StdCppThreadFactory.cpp:109
virtual void setName(const char *name)
Set the object's name.
Definition StdCppThreadFactory.cpp:103
virtual std::size_t getStackSize() const
Get the stack size of this thread.
Definition StdCppThreadFactory.cpp:147
virtual ~StdCppThread()
Definition StdCppThreadFactory.h:36
int m_initialPriority
Definition StdCppThreadFactory.h:18
Definition StdCppThreadFactory.h:13
virtual ~StdCppThreadFactory()
Definition StdCppThreadFactory.h:59
StdCppThreadFactory()
Definition StdCppThreadFactory.cpp:163
Error
Kinds of errors of the OS objects.
Definition OSWrapperError.h:9
Implementation of OSWrapper for standard C++11 using std::thread.
Definition StdCppEventFlagFactory.cpp:6