Debian Codegraph @

3rdparty/xspublic/xscommon/threading.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 THREADING_H
34 #define THREADING_H
35
36 #include "xsens_mutex.h"
37
38 #ifndef __GNUC__
39 #pragma warning(disable: 4127)
40 #endif
41
42 namespace xsens {
43 /*! \class StandardThread
44 \brief A class for a standard thread that has to perform the same action repeatedly.
45 \details The class has three virtual functions, of which the innerFunction is the most important.
46 innerFunction gets called repeatedly and is expected to return so that StandardThread can
47 check for thread termination.
48 */
49 class StandardThread {
50 private:
51 XsThread m_thread;
52 XsThreadPriority m_priority;
53 #ifdef _WIN32
54 mutable xsens::Mutex m_mux;
55 #endif
56
57 protected:
58 volatile std::atomic_bool m_stop; //!< Indicates that the thread should stop. Derived classes should check isTerminating() instead of directly polling this value when checking if the thread should stop. However, there are some cases (tests, SignallingThread) where direct access from within the class is desired, which is why the vlaue is protected instead of private.
59 volatile std::atomic_bool m_yieldOnZeroSleep; //!< When true, a sleep value of 0 returned by innerFunction will trigger a thread yield operation. When false, the next cycle is started immediately.
60 #ifdef _WIN32
61 HANDLE m_stopHandle; //!< Duplicates m_stop functionality for external dependent classes such as Semaphore
62 HANDLE m_running; //!< Indicates that the thread is running
63 private:
64 XsThreadId m_threadId;
65 #else
66 pthread_attr_t m_attr; //!< Duplicates m_stop functionality for external dependent classes such as Semaphore
67 bool m_running; //!< Indicates that the thread is running
68 private:
69 #endif
70 char* m_name;
71 static XSENS_THREAD_RETURN threadInit(void *obj);
72 #ifndef _WIN32
73 static void threadCleanup(void *obj);
74 #endif
75 void threadMain(void);
76 protected:
77 //! Virtual initialization function
78 virtual void initFunction(void) { }
79
80 //! Virtual exit function
81 virtual void exitFunction(void) { }
82
83 //! Virtual inner function
84 virtual int32_t innerFunction(void) { return 0; }
85
86 //! Return the thread handle
87 inline XsThread threadHandle() const { return m_thread; }
88 public:
89 StandardThread();
90 virtual ~StandardThread();
91
92 bool startThread(const char* name=NULL);
93 virtual void signalStopThread(void);
94 void stopThread(void) noexcept;
95 bool isAlive(void) volatile const noexcept;
96 bool isRunning(void) volatile const noexcept;
97 bool setPriority(XsThreadPriority pri);
98 bool isTerminating() volatile const noexcept;
99
100 //! \returns The thread ID
101 XsThreadId getThreadId(void) const
102 {
103 #ifdef _WIN32
104 return m_threadId;
105 #else
106 return m_thread;
107 #endif
108 }
109 #ifdef _WIN32
110 void terminateThread();
111 #endif
112 };
113 #define XSENS_THREAD_CHECK if (isTerminating()) return 0;
114
115 #ifdef ANDROID
116 #define CDECL_XS
117 #else
118 #define CDECL_XS __cdecl
119 #endif
120
121 #ifndef SWIG
122 typedef void (CDECL_XS *WatchDogFunction)(void*);
123
124 /*! \class WatchDogThread
125 \brief A class that keeps an eye on a threads timer
126 */
127 class WatchDogThread {
128 private:
129 XsThread m_thread;
130 #ifdef _WIN32
131 HANDLE m_stop;
132 HANDLE m_running;
133 HANDLE m_reset;
134 #else
135 pthread_attr_t m_attr;
136 Mutex m_mutex;
137 bool m_running;
138 bool m_reset;
139 bool m_stop;
140 #endif
141 volatile std::atomic<std::uint32_t> m_timeout;
142 WatchDogFunction m_func;
143 void* m_param;
144 char* m_name;
145 static XSENS_THREAD_RETURN threadInit(void* obj);
146
147 XsThreadId m_threadId;
148 void threadMain(void);
149 bool isAlive(void) volatile const noexcept;
150 bool isRunning(void) volatile const noexcept;
151 public:
152
153 /*! \brief Constructor
154 */
155 WatchDogThread(WatchDogFunction func, void* param = NULL);
156
157 /*! \brief Destructor
158 */
159 ~WatchDogThread();
160
161 bool resetTimer(uint32_t timeout = 0);
162 bool startTimer(uint32_t timeout = 10000, const char* name=NULL);
163 bool stopTimer(void) noexcept;
164
165 //! \returns The thread ID
166 XsThreadId getThreadId(void) const { return m_threadId; }
167 };
168
169 /*! \class TaskThread
170 \brief Class for handling small tasks
171 \details Use this class if you have small tasks that need to be performed out of the main thread
172 The thread uses tasks supplied in a TaskType struct.
173 */
174 class TaskThread : public StandardThread {
175 public:
176 typedef void (CDECL_XS *TaskFunction)(void*); //!< A function prototype for a task
177 private:
178 struct TaskType {
179 TaskFunction m_function;
180 void* m_param;
181 };
182
183 std::deque<TaskType> m_queue;
184 Mutex m_safe;
185 bool m_inFunc;
186 protected:
187
188 /*! \brief The inner function of the task thread.
189 \details The function checks if there is a task in the queue and executes it.
190 Then it returns to the StandardThread main loop to check for termination.
191 If there are no tasks in the queue, the thread will terminate itself.
192 \returns Not 0 if successful
193 */
194 virtual int32_t innerFunction(void)
195 {
196 Lock safety(&m_safe);
197 if (m_queue.size() > 0)
198 {
199 TaskType& task = m_queue.front();
200 m_queue.pop_front();
201 m_inFunc = true;
202 safety.unlock();
203 task.m_function(task.m_param);
204 m_inFunc = false;
205 return 0; // no sleep
206 }
207 // notify thread to terminate when there are no more tasks
208 stopThread();
209 return 0;
210 }
211 public:
212 TaskThread() : m_inFunc(false) {}
213 ~TaskThread()
214 {
215 while(isRunning() && getLength())
216 XsTime::msleep(100);
217 }
218
219 /*! \brief Adds a task to a queue
220 \param func The function to add
221 \param param The parameters to add
222 */
223 void addTask(TaskFunction func, void* param)
224 {
225 Lock safety(&m_safe);
226 TaskType tmp = {func,param};
227 m_queue.push_back(tmp);
228 }
229
230 //! \returns The length of a queue
231 int32_t getLength(void) noexcept
232 {
233 Lock safety(&m_safe);
234 return (int32_t) m_queue.size() + (m_inFunc?1:0);
235 }
236
237 //! \brief Clears a queue
238 void clear(void)
239 {
240 Lock safety(&m_safe);
241 m_queue.clear();
242 }
243 };
244 #endif
245 } // namespace xsens
246
247 #ifndef __GNUC__
248 #pragma warning(default: 4127)
249 #endif
250
251 #endif