| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#ifndef XSENS_THREADPOOL_H |
| 34 |
#define XSENS_THREADPOOL_H |
| 35 |
|
| 36 |
#include "xsens_mutex.h" |
| 37 |
|
| 38 |
#include <map> |
| 39 |
#include <set> |
| 40 |
#include <deque> |
| 41 |
#include <list> |
| 42 |
#include <memory> |
| 43 |
|
| 44 |
namespace xsens { |
| 45 |
|
| 46 |
int processorCount(); |
| 47 |
|
| 48 |
class PooledTask; |
| 49 |
class ThreadPoolTask { |
| 50 |
public: |
| 51 |
virtual bool exec() = 0; |
| 52 |
virtual unsigned int needToWaitFor(); |
| 53 |
ThreadPoolTask() : m_container(nullptr) {} |
| 54 |
virtual ~ThreadPoolTask() {} |
| 55 |
virtual void onError() {} |
| 56 |
bool isCanceling() const; |
| 57 |
unsigned int taskId() const; |
| 58 |
|
| 59 |
private: |
| 60 |
friend class ThreadPool; |
| 61 |
PooledTask* m_container; |
| 62 |
}; |
| 63 |
|
| 64 |
class PooledThread; |
| 65 |
class ThreadPool |
| 66 |
{ |
| 67 |
public: |
| 68 |
typedef unsigned int TaskId; |
| 69 |
|
| 70 |
private: |
| 71 |
void reportTaskComplete(std::shared_ptr<PooledTask>); |
| 72 |
void reportTaskPaused(std::shared_ptr<PooledTask>); |
| 73 |
std::shared_ptr<PooledTask> getNextTask(); |
| 74 |
friend class PooledThread; |
| 75 |
|
| 76 |
std::set<PooledThread*> m_threads; |
| 77 |
std::deque<std::shared_ptr<PooledTask>> m_tasks; |
| 78 |
std::map<TaskId, std::shared_ptr<PooledTask>> m_tasksSearch; |
| 79 |
std::map<TaskId, std::shared_ptr<PooledTask>> m_executing; |
| 80 |
std::map<TaskId, std::shared_ptr<PooledTask>> m_delaying; |
| 81 |
Mutex m_safe; |
| 82 |
TaskId m_nextId; |
| 83 |
bool m_suspended; |
| 84 |
volatile std::atomic_bool m_terminating; |
| 85 |
|
| 86 |
std::shared_ptr<PooledTask> findTask(TaskId id); |
| 87 |
|
| 88 |
protected: |
| 89 |
ThreadPool(); |
| 90 |
~ThreadPool(); |
| 91 |
|
| 92 |
public: |
| 93 |
TaskId addTask(ThreadPoolTask* task, TaskId afterId = 0); |
| 94 |
unsigned int count(); |
| 95 |
void setPoolSize(unsigned int poolsize); |
| 96 |
unsigned int poolSize() const; |
| 97 |
bool doesTaskExist(TaskId id); |
| 98 |
void cancelTask(TaskId id, bool wait = true); |
| 99 |
void waitForCompletion(TaskId id); |
| 100 |
void suspend(bool wait = false) noexcept; |
| 101 |
void resume(); |
| 102 |
unsigned int executedCount(unsigned int thread) const; |
| 103 |
unsigned int completedCount(unsigned int thread) const; |
| 104 |
unsigned int failedCount(unsigned int thread) const; |
| 105 |
XsThreadId taskThreadId(TaskId id); |
| 106 |
|
| 107 |
static ThreadPool* instance(); |
| 108 |
static void destroy(); |
| 109 |
static void setPool(ThreadPool* pool); |
| 110 |
}; |
| 111 |
|
| 112 |
class TaskCompletionWaiter : public ThreadPoolTask |
| 113 |
{ |
| 114 |
public: |
| 115 |
virtual bool exec(); |
| 116 |
virtual unsigned int needToWaitFor(); |
| 117 |
|
| 118 |
TaskCompletionWaiter(ThreadPool* pool = ThreadPool::instance()); |
| 119 |
TaskCompletionWaiter(const std::list<unsigned int>& waitlist, ThreadPool* pool = ThreadPool::instance()); |
| 120 |
virtual ~TaskCompletionWaiter(); |
| 121 |
void addWaitId(unsigned int id); |
| 122 |
|
| 123 |
private: |
| 124 |
ThreadPool* m_pool; |
| 125 |
std::list<unsigned int> m_waitList; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
template <typename T> |
| 134 |
class ThreadPoolObjectDeleter : public ThreadPoolTask |
| 135 |
{ |
| 136 |
T* m_object; |
| 137 |
ThreadPoolObjectDeleter(ThreadPoolObjectDeleter const&) = delete; |
| 138 |
ThreadPoolObjectDeleter const& operator = (ThreadPoolObjectDeleter const&) = delete; |
| 139 |
public: |
| 140 |
|
| 141 |
|
| 142 |
ThreadPoolObjectDeleter(T* obj) : m_object(obj) {} |
| 143 |
|
| 144 |
bool exec() override |
| 145 |
{ |
| 146 |
delete m_object; |
| 147 |
return true; |
| 148 |
} |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
template <typename T> |
| 159 |
inline static ThreadPool::TaskId deleteThreaded(T* obj) |
| 160 |
{ |
| 161 |
return ThreadPool::instance()->addTask(new ThreadPoolObjectDeleter<T>(obj)); |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
#endif |