Debian Codegraph @

3rdparty/xspublic/xscommon/xsens_threadpool.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 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 #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; //!< \returns True if the task completed or false to reschedule the task
52 virtual unsigned int needToWaitFor();
53 ThreadPoolTask() : m_container(nullptr) {}
54 virtual ~ThreadPoolTask() {}
55 virtual void onError() {} //!< Callback for when an error occurred during task execution. Any implementation of this function should NEVER throw an exception.
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; //!< A type definition of a task ID
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 /*! \brief Task that will delete its object parameter
129 \details This class can be used to schedule a delete of a single object in the ThreadPool.
130 Normally you don't need to use this class directly, but can use the \ref deleteThreaded function.
131 \tparam T The type of the object to delete
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 /*! \brief Constructor, creates the task to delete object a obj, but doesn't schedule it.
141 */
142 ThreadPoolObjectDeleter(T* obj) : m_object(obj) {}
143 /*! \brief deletes the managed object */
144 bool exec() override
145 {
146 delete m_object;
147 return true;
148 }
149 };
150
151 /*! \brief Delete \a obj in the ThreadPool.
152 \details This function will schedule a task to be executed by the ThreadPool which will delete obj
153 \param obj The object to be deleted
154 \tparam T The type of the object to delete. Typically not required as the compiler should detect it automatically.
155 \return The task ID of the deletion task
156 \relates ThreadPoolObjectDeleter
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 } // namespace xsens
165
166 #endif