CppELib 1.7.0
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1#ifndef OS_WRAPPER_THREAD_POOL_H_INCLUDED
2#define OS_WRAPPER_THREAD_POOL_H_INCLUDED
3
4#include <cstddef>
5#include "Runnable.h"
6#include "Thread.h"
8
9namespace OSWrapper {
10
11template <typename T> class MessageQueue;
12class EventFlag;
13class FixedMemoryPool;
14
31private:
32 class TaskRunner : public Runnable {
33 public:
34 void release();
35 Error timedWait(Timeout tmout);
36
37 private:
38 friend class ThreadPool;
39
40 TaskRunner(EventFlag* ev, ThreadPool* tp);
41 ~TaskRunner();
42 void startThread(Runnable* task, int priority, bool needsWaiting);
43 void run();
44 void invoke();
45 void afterInvoke();
46 void notifyFinished();
47 void setThread(Thread* t) { m_thread = t; }
48 Thread* getThread() const { return m_thread; }
49 EventFlag* getEventFlag() const { return m_ev; }
50
51 Runnable* m_task;
52 Thread* m_thread;
53 EventFlag* m_ev;
54 bool m_needsWaiting;
55 ThreadPool* m_tp;
56
57 TaskRunner(const TaskRunner&);
58 TaskRunner& operator=(const TaskRunner&);
59 };
60
61public:
73 class WaitGuard {
74 public:
79 WaitGuard();
80
86 ~WaitGuard();
87
94 void release();
95
104 Error wait();
105
114 Error tryWait();
115
127 Error timedWait(Timeout tmout);
128
134 bool isValid() const;
135
136 private:
137 friend class ThreadPool;
138 TaskRunner* m_runner;
139
140 WaitGuard(const WaitGuard&);
141 WaitGuard& operator=(const WaitGuard&);
142 };
143
152 static ThreadPool* create(std::size_t maxThreads, std::size_t stackSize = 0U, int defaultPriority = Thread::getNormalPriority(), const char* threadName = "");
153
163 static void destroy(ThreadPool* p);
164
176 Error start(Runnable* task, WaitGuard* waiter = 0, int priority = Thread::INHERIT_PRIORITY);
177
189 Error tryStart(Runnable* task, WaitGuard* waiter = 0, int priority = Thread::INHERIT_PRIORITY);
190
208 Error timedStart(Runnable* task, Timeout tmout, WaitGuard* waiter = 0, int priority = Thread::INHERIT_PRIORITY);
209
215
221
226 const char* getThreadName() const;
227
228private:
230 TaskRunnerMQ* m_freeRunnerQueue;
231
233 ThreadVector m_threads;
234 void* m_threadMemory;
235
236 FixedMemoryPool* m_threadMemoryPool;
237 FixedMemoryPool* m_runnerPool;
238 FixedMemoryPool* m_objPool;
239
240 Thread::UncaughtExceptionHandler* m_uncaughtExceptionHandler;
241 const int m_defaultPriority;
242 const char* m_threadName;
243
244 bool constructMembers(std::size_t maxThreads, std::size_t stackSize);
245 void destroyMembers();
246 void destroyRunners();
247 void destroyThreads();
248 void waitThreads();
249 void releaseRunner(TaskRunner* runner);
250 int getDefaultPriority() const { return m_defaultPriority; }
251
252 ThreadPool(FixedMemoryPool* objPool, int defaultPriority, const char* threadName);
253 ~ThreadPool();
254
255 ThreadPool(const ThreadPool&);
256 ThreadPool& operator=(const ThreadPool&);
257};
258
259}
260
261#endif // OS_WRAPPER_THREAD_POOL_H_INCLUDED
STL-like vector container using pre-allocated buffer.
Definition PreallocatedVector.h:26
Abstract class that has functions of common RTOS's event flag.
Definition EventFlag.h:24
Abstract class that has functions of common RTOS's fixed-size memory pool.
Definition FixedMemoryPool.h:23
Class template of message queue.
Definition MessageQueue.h:22
Interface for implementing an active class.
Definition Runnable.h:9
Class to wait for the asynchronous task finished.
Definition ThreadPool.h:73
~WaitGuard()
Destructor of WaitGuard.
Definition ThreadPool.cpp:307
Error wait()
Block the current thread until the asynchronous task finished.
Definition ThreadPool.cpp:321
WaitGuard()
Constructor of WaitGuard.
Definition ThreadPool.cpp:302
Error timedWait(Timeout tmout)
Block the current thread until the asynchronous task finished but only within the limited time.
Definition ThreadPool.cpp:331
Error tryWait()
Query without blocking whether the asynchronous task finished.
Definition ThreadPool.cpp:326
bool isValid() const
Return true if the validity state is valid.
Definition ThreadPool.cpp:339
void release()
Call wait(), release the resource, and turn the valid state to invalid.
Definition ThreadPool.cpp:312
Class of thread pool pattern.
Definition ThreadPool.h:30
Error tryStart(Runnable *task, WaitGuard *waiter=0, int priority=Thread::INHERIT_PRIORITY)
Try to start an asynchronous task without blocking.
Definition ThreadPool.cpp:177
Thread::UncaughtExceptionHandler * getUncaughtExceptionHandler() const
Get the UncaughtExceptionHandler for this ThreadPool.
Definition ThreadPool.cpp:220
Error timedStart(Runnable *task, Timeout tmout, WaitGuard *waiter=0, int priority=Thread::INHERIT_PRIORITY)
Start an asynchronous task with Timeout.
Definition ThreadPool.cpp:182
static void destroy(ThreadPool *p)
Destroy a ThreadPool object.
Definition ThreadPool.cpp:109
Error start(Runnable *task, WaitGuard *waiter=0, int priority=Thread::INHERIT_PRIORITY)
Start an asynchronous task.
Definition ThreadPool.cpp:172
static ThreadPool * create(std::size_t maxThreads, std::size_t stackSize=0U, int defaultPriority=Thread::getNormalPriority(), const char *threadName="")
Create a ThreadPool object.
Definition ThreadPool.cpp:25
const char * getThreadName() const
Get the name of threads.
Definition ThreadPool.cpp:225
void setUncaughtExceptionHandler(Thread::UncaughtExceptionHandler *handler)
Set the UncaughtExceptionHandler for this ThreadPool.
Definition ThreadPool.cpp:212
Interface for handling uncaught exception.
Definition Thread.h:66
Abstract class that has functions of common RTOS's thread.
Definition Thread.h:50
static const int INHERIT_PRIORITY
Use when set the same priority as the caller thread.
Definition Thread.h:77
static int getNormalPriority()
Get the middle priority between the minimum and maximum value.
Definition Thread.cpp:193
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