xsens_threadpool.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #ifndef XSENS_THREADPOOL_H
66 #define XSENS_THREADPOOL_H
67 
68 #include "xsens_mutex.h"
69 
70 #include <map>
71 #include <set>
72 #include <deque>
73 #include <list>
74 #include <memory>
75 
76 namespace xsens
77 {
78 
79 int processorCount();
80 
81 class PooledTask;
83 {
84 public:
85  virtual bool exec() = 0;
86  virtual unsigned int needToWaitFor();
87  ThreadPoolTask() : m_container(nullptr) {}
88  virtual ~ThreadPoolTask() {}
89  virtual void onError() {}
90  bool isCanceling() const;
91  unsigned int taskId() const;
92 
93 private:
94  friend class ThreadPool;
96 };
97 
98 class PooledThread;
100 {
101 public:
102  typedef unsigned int TaskId;
103 
104 private:
105  void reportTaskComplete(std::shared_ptr<PooledTask>);
106  void reportTaskPaused(std::shared_ptr<PooledTask>);
107  std::shared_ptr<PooledTask> getNextTask();
108  friend class PooledThread;
109 
110  std::set<PooledThread*> m_threads;
111  std::deque<std::shared_ptr<PooledTask>> m_tasks;
112  std::map<TaskId, std::shared_ptr<PooledTask>> m_tasksSearch;
113  std::map<TaskId, std::shared_ptr<PooledTask>> m_executing;
114  std::map<TaskId, std::shared_ptr<PooledTask>> m_delaying;
118  volatile std::atomic_bool m_terminating;
119 
120  std::shared_ptr<PooledTask> findTask(TaskId id);
121 
122 protected:
123  ThreadPool();
124  ~ThreadPool();
125 
126 public:
127  TaskId addTask(ThreadPoolTask* task, TaskId afterId = 0);
128  unsigned int count();
129  void setPoolSize(unsigned int poolsize);
130  unsigned int poolSize() const;
131  bool doesTaskExist(TaskId id);
132  void cancelTask(TaskId id, bool wait = true) noexcept;
133  void waitForCompletion(TaskId id);
134  void suspend(bool wait = false) noexcept;
135  void resume();
136  unsigned int executedCount(unsigned int thread) const;
137  unsigned int completedCount(unsigned int thread) const;
138  unsigned int failedCount(unsigned int thread) const;
140 
141  static ThreadPool* instance() noexcept;
142  static void destroy();
143  static void setPool(ThreadPool* pool);
144 };
145 
147 {
148 public:
149  virtual bool exec();
150  virtual unsigned int needToWaitFor();
151 
153  TaskCompletionWaiter(const std::list<unsigned int>& waitlist, ThreadPool* pool = ThreadPool::instance());
154  virtual ~TaskCompletionWaiter();
155  void addWaitId(unsigned int id);
156 
157 private:
159  std::list<unsigned int> m_waitList;
160 };
161 
167 template <typename T>
169 {
172  ThreadPoolObjectDeleter const& operator = (ThreadPoolObjectDeleter const&) = delete;
173 public:
176  ThreadPoolObjectDeleter(T* obj) : m_object(obj) {}
178  bool exec() override
179  {
180  delete m_object;
181  return true;
182  }
183 };
184 
192 template <typename T>
193 inline static ThreadPool::TaskId deleteThreaded(T* obj)
194 {
196 }
197 
198 } // namespace xsens
199 
200 #endif
xsens::ThreadPool::m_suspended
bool m_suspended
Definition: xsens_threadpool.h:117
xsens::ThreadPoolTask
A generic task implementation for the thread pool.
Definition: xsens_threadpool.h:82
XsThreadId
pthread_t XsThreadId
Definition: xsthread.h:187
xsens::ThreadPoolObjectDeleter::m_object
T * m_object
Definition: xsens_threadpool.h:170
xsens::ThreadPoolTask::isCanceling
bool isCanceling() const
Returns true if the task has been told to cancel itself.
Definition: xsens_threadpool.cpp:188
xsens::ThreadPoolTask::~ThreadPoolTask
virtual ~ThreadPoolTask()
Definition: xsens_threadpool.h:88
xsens::ThreadPool::suspend
void suspend(bool wait=false) noexcept
Suspend execution of tasks, any currently executing tasks will run to completion, but queued tasks wi...
Definition: xsens_threadpool.cpp:676
xsens::ThreadPool::ThreadPool
ThreadPool()
Construct a threadpool with a number of threads equal to the number of cores on the PC.
Definition: xsens_threadpool.cpp:361
xsens::ThreadPool::completedCount
unsigned int completedCount(unsigned int thread) const
Return the number of tasks completed by the given thread.
Definition: xsens_threadpool.cpp:713
xsens::ThreadPool::taskThreadId
XsThreadId taskThreadId(TaskId id)
Find an XsThread with the specified id.
Definition: xsens_threadpool.cpp:528
xsens::ThreadPool::doesTaskExist
bool doesTaskExist(TaskId id)
Check if a task with the supplied id exists.
Definition: xsens_threadpool.cpp:539
xsens::ThreadPool::m_tasksSearch
std::map< TaskId, std::shared_ptr< PooledTask > > m_tasksSearch
Definition: xsens_threadpool.h:112
xsens::ThreadPoolObjectDeleter::exec
bool exec() override
deletes the managed object
Definition: xsens_threadpool.h:178
xsens::PooledThread
A class that contains a thread that runs in a ThreadPool to execute tasks.
Definition: xsens_threadpool.cpp:210
xsens::ThreadPool::m_threads
std::set< PooledThread * > m_threads
Definition: xsens_threadpool.h:110
xsens::ThreadPoolTask::needToWaitFor
virtual unsigned int needToWaitFor()
This function gets called by PooledThread when the exec() function returns false to determine if we s...
Definition: xsens_threadpool.cpp:119
xsens::ThreadPool::setPoolSize
void setPoolSize(unsigned int poolsize)
Set the number of threads in the ThreadPool.
Definition: xsens_threadpool.cpp:445
xsens::TaskCompletionWaiter
A class that allows multiple-dependency waiting.
Definition: xsens_threadpool.h:146
xsens::ThreadPool::m_terminating
volatile std::atomic_bool m_terminating
Definition: xsens_threadpool.h:118
xsens::ThreadPoolObjectDeleter
Task that will delete its object parameter.
Definition: xsens_threadpool.h:168
xsens::ThreadPoolObjectDeleter::deleteThreaded
static ThreadPool::TaskId deleteThreaded(T *obj)
Delete obj in the ThreadPool.
Definition: xsens_threadpool.h:193
xsens_mutex.h
xsens::ThreadPool::cancelTask
void cancelTask(TaskId id, bool wait=true) noexcept
Remove the task with the supplied id if it exists, waits for the task to be finished.
Definition: xsens_threadpool.cpp:546
xsens::ThreadPool::reportTaskPaused
void reportTaskPaused(std::shared_ptr< PooledTask >)
Called by PooledThread to notify the ThreadPool that its running task has to wait for something.
Definition: xsens_threadpool.cpp:644
xsens::ThreadPool::TaskId
unsigned int TaskId
A type definition of a task ID.
Definition: xsens_threadpool.h:102
xsens::TaskCompletionWaiter::m_pool
ThreadPool * m_pool
Definition: xsens_threadpool.h:158
xsens::ThreadPool::m_executing
std::map< TaskId, std::shared_ptr< PooledTask > > m_executing
Definition: xsens_threadpool.h:113
xsens::ThreadPool::count
unsigned int count()
Return the number of tasks that are currently in the queue or being executed.
Definition: xsens_threadpool.cpp:436
xsens::ThreadPool::getNextTask
std::shared_ptr< PooledTask > getNextTask()
Return the next task that should be run and mark it as executing.
Definition: xsens_threadpool.cpp:621
xsens::ThreadPool::m_safe
Mutex m_safe
Definition: xsens_threadpool.h:115
xsens::processorCount
int processorCount()
Returns the number of processor cores in the current system.
Definition: xsens_threadpool.cpp:94
xsens::ThreadPool::destroy
static void destroy()
Destroy the global thread pool object.
Definition: xsens_threadpool.cpp:760
xsens::TaskCompletionWaiter::m_waitList
std::list< unsigned int > m_waitList
Definition: xsens_threadpool.h:159
xsens::Mutex
A base mutex class.
Definition: xsens_mutex.h:132
xsens::ThreadPoolObjectDeleter::ThreadPoolObjectDeleter
ThreadPoolObjectDeleter(T *obj)
Constructor, creates the task to delete object a obj, but doesn't schedule it.
Definition: xsens_threadpool.h:176
xsens::ThreadPool::addTask
TaskId addTask(ThreadPoolTask *task, TaskId afterId=0)
Add a task to be executed by the threadpool.
Definition: xsens_threadpool.cpp:399
xsens::ThreadPoolTask::ThreadPoolTask
ThreadPoolTask()
Definition: xsens_threadpool.h:87
xsens::ThreadPool::m_tasks
std::deque< std::shared_ptr< PooledTask > > m_tasks
Definition: xsens_threadpool.h:111
xsens::ThreadPool::resume
void resume()
Resume execution of tasks.
Definition: xsens_threadpool.cpp:694
xsens::ThreadPoolTask::taskId
unsigned int taskId() const
Returns the task ID of the task or 0 if it doesn't have a proper ID (yet)
Definition: xsens_threadpool.cpp:196
xsens::ThreadPool::waitForCompletion
void waitForCompletion(TaskId id)
Wait for the task with the given ID to complete.
Definition: xsens_threadpool.cpp:586
xsens::ThreadPool::failedCount
unsigned int failedCount(unsigned int thread) const
Return the number of tasks that failed to execute in the given thread.
Definition: xsens_threadpool.cpp:724
xsens::ThreadPool::instance
static ThreadPool * instance() noexcept
Return the global thread pool object, it will be created if it did not yet exist.
Definition: xsens_threadpool.cpp:737
xsens::ThreadPoolTask::m_container
PooledTask * m_container
Definition: xsens_threadpool.h:95
xsens::ThreadPool::setPool
static void setPool(ThreadPool *pool)
Set the threadpool to use.
Definition: xsens_threadpool.cpp:776
xsens::ThreadPool::findTask
std::shared_ptr< PooledTask > findTask(TaskId id)
Find a task with the supplied id.
Definition: xsens_threadpool.cpp:504
xsens::ThreadPool
This class creates and maintains a number of threads that can execute finite-length tasks.
Definition: xsens_threadpool.h:99
xsens::ThreadPool::reportTaskComplete
void reportTaskComplete(std::shared_ptr< PooledTask >)
Called by PooledThread to notify the ThreadPool that a task was completed.
Definition: xsens_threadpool.cpp:596
xsens::PooledTask
A class that contains a task and some administrative stuff.
Definition: xsens_threadpool.cpp:130
xsens::ThreadPoolTask::exec
virtual bool exec()=0
xsens
Definition: threading.cpp:78
xsens::ThreadPool::poolSize
unsigned int poolSize() const
Return the number of threads in the pool.
Definition: xsens_threadpool.cpp:497
xsens::ThreadPool::~ThreadPool
~ThreadPool()
Destructor, clears any pending tasks and destroys the threads.
Definition: xsens_threadpool.cpp:371
xsens::ThreadPool::executedCount
unsigned int executedCount(unsigned int thread) const
Return the number of tasks executed (including paused) by the given thread.
Definition: xsens_threadpool.cpp:702
xsens::ThreadPool::m_delaying
std::map< TaskId, std::shared_ptr< PooledTask > > m_delaying
Definition: xsens_threadpool.h:114
xsens::ThreadPool::m_nextId
TaskId m_nextId
Definition: xsens_threadpool.h:116


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20