threading.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 THREADING_H
66 #define THREADING_H
67 
68 #include "xsens_mutex.h"
69 #include <functional>
70 
71 #ifndef __GNUC__
72  #pragma warning(disable: 4127)
73 #endif
74 
75 namespace xsens
76 {
84 {
85 private:
88 #ifdef _WIN32
89  mutable xsens::Mutex m_mux;
90 #endif
91 
92 protected:
93  volatile std::atomic_bool m_stop;
94  volatile std::atomic_bool m_yieldOnZeroSleep;
95 #ifdef _WIN32
96  HANDLE m_stopHandle;
97  HANDLE m_running;
98 private:
99  XsThreadId m_threadId;
100 #else
101  pthread_attr_t m_attr;
102  bool m_running;
103 private:
104 #endif
105  char* m_name;
106  static XSENS_THREAD_RETURN threadInit(void* obj);
107 #ifndef _WIN32
108  static void threadCleanup(void* obj);
109 #endif
110  void threadMain(void);
111 protected:
113  virtual void initFunction(void) { }
114 
116  virtual void exitFunction(void) { }
117 
119  virtual int32_t innerFunction(void)
120  {
121  return 0;
122  }
123 
125  inline XsThread threadHandle() const
126  {
127  return m_thread;
128  }
129 public:
130  StandardThread();
131  virtual ~StandardThread();
132 
133  bool startThread(const char* name = NULL);
134  virtual void signalStopThread(void);
135  void stopThread(void) noexcept;
136  bool isAlive(void) volatile const noexcept;
137  bool isRunning(void) volatile const noexcept;
138  bool setPriority(XsThreadPriority pri);
139  bool isTerminating() volatile const noexcept;
140 
143  {
144 #ifdef _WIN32
145  return m_threadId;
146 #else
147  return m_thread;
148 #endif
149  }
150 #ifdef _WIN32
151  void terminateThread();
152 #endif
153 };
154 #define XSENS_THREAD_CHECK() if (isTerminating()) return 0
155 
156 #ifdef ANDROID
157  #define CDECL_XS
158 #else
159  #define CDECL_XS __cdecl
160 #endif
161 
162 #ifndef SWIG
163 typedef void (CDECL_XS* WatchDogFunction)(void*);
164 
169 {
170 private:
172 #ifdef _WIN32
173  HANDLE m_stop;
174  HANDLE m_running;
175  HANDLE m_reset;
176 #else
177  pthread_attr_t m_attr;
179  bool m_running;
180  bool m_reset;
181  bool m_stop;
182 #endif
183  volatile std::atomic<std::uint32_t> m_timeout;
185  void* m_param;
186  char* m_name;
187  static XSENS_THREAD_RETURN threadInit(void* obj);
188 
190  void threadMain(void);
191  bool isAlive(void) volatile const noexcept;
192  bool isRunning(void) volatile const noexcept;
193 public:
194 
197  WatchDogThread(WatchDogFunction func, void* param = NULL);
198 
201  ~WatchDogThread();
202 
203  bool resetTimer(uint32_t timeout = 0);
204  bool startTimer(uint32_t timeout = 10000, const char* name = NULL);
205  bool stopTimer(void) noexcept;
206 
209  {
210  return m_threadId;
211  }
212 };
213 
220 {
221 public:
222  typedef void (CDECL_XS* TaskFunction)(void*);
223 private:
224  struct TaskType
225  {
227  void* m_param;
228  };
229 
230  std::deque<TaskType> m_queue;
232  bool m_inFunc;
233 protected:
234 
241  virtual int32_t innerFunction(void)
242  {
243  Lock safety(&m_safe);
244  if (m_queue.size() > 0)
245  {
246  TaskType& task = m_queue.front();
247  m_queue.pop_front();
248  m_inFunc = true;
249  safety.unlock();
250  task.m_function(task.m_param);
251  m_inFunc = false;
252  return 0; // no sleep
253  }
254  // notify thread to terminate when there are no more tasks
255  stopThread();
256  return 0;
257  }
258 public:
259  TaskThread() : m_inFunc(false) {}
261  {
262  while (isRunning() && getLength())
263  XsTime::msleep(100);
264  }
265 
270  void addTask(TaskFunction func, void* param)
271  {
272  Lock safety(&m_safe);
273  TaskType tmp = {func, param};
274  m_queue.push_back(tmp);
275  }
276 
278  int32_t getLength(void) noexcept
279  {
280  Lock safety(&m_safe);
281  return (int32_t) m_queue.size() + (m_inFunc ? 1 : 0);
282  }
283 
285  void clear(void)
286  {
287  Lock safety(&m_safe);
288  m_queue.clear();
289  }
290 };
291 #endif
292 #ifndef __APPLE__
293 // the std::function is not properly available in all iOS versions
298 {
299  std::function<void()> m_func;
300 public:
305  ThreadedFunction(std::function<void()> const& f, bool runNow = true)
306  : m_func(f)
307  {
308  if (runNow)
309  startThread();
310  }
312  {
313  stopThread();
314  }
315 
317  int32_t innerFunction(void) override final
318  {
319  m_func();
321  return 0;
322  }
323 };
324 #endif
325 } // namespace xsens
326 
327 #ifndef __GNUC__
328  #pragma warning(default: 4127)
329 #endif
330 
331 #endif
xsens::WatchDogThread::m_param
void * m_param
Definition: threading.h:185
xsens::ThreadedFunction
A class that will run a single (lambda) function in a separate thread.
Definition: threading.h:297
XsThreadId
pthread_t XsThreadId
Definition: xsthread.h:187
xsens::StandardThread::~StandardThread
virtual ~StandardThread()
Definition: threading.cpp:110
xsens::WatchDogThread::m_stop
bool m_stop
Definition: threading.h:181
xsens::TaskThread::m_queue
std::deque< TaskType > m_queue
Definition: threading.h:230
xsens::StandardThread::threadInit
static XSENS_THREAD_RETURN threadInit(void *obj)
Definition: threading.cpp:409
xsens::StandardThread::threadCleanup
static void threadCleanup(void *obj)
Cleanup the thread by calling the exit function.
Definition: threading.cpp:421
xsens::TaskThread::addTask
void addTask(TaskFunction func, void *param)
Adds a task to a queue.
Definition: threading.h:270
xsens::StandardThread::isRunning
bool isRunning(void) volatile const noexcept
Returns whether the thread is currently running.
Definition: threading.cpp:160
xsens::TaskThread::TaskType::m_function
TaskFunction m_function
Definition: threading.h:226
xsens::StandardThread::m_attr
pthread_attr_t m_attr
Duplicates m_stop functionality for external dependent classes such as Semaphore.
Definition: threading.h:101
xsens::WatchDogThread::m_attr
pthread_attr_t m_attr
Definition: threading.h:177
xsens::TaskThread::TaskThread
TaskThread()
Definition: threading.h:259
xsens::WatchDogThread::stopTimer
bool stopTimer(void) noexcept
Stops the timer.
Definition: threading.cpp:591
xsens::ThreadedFunction::~ThreadedFunction
~ThreadedFunction()
Definition: threading.h:311
xsens::WatchDogThread::resetTimer
bool resetTimer(uint32_t timeout=0)
Resets the timer and sets a timeout.
Definition: threading.cpp:739
xsens::WatchDogThread::getThreadId
XsThreadId getThreadId(void) const
Definition: threading.h:208
xsens::WatchDogThread::m_thread
XsThread m_thread
Definition: threading.h:171
xsens::StandardThread::innerFunction
virtual int32_t innerFunction(void)
Virtual inner function.
Definition: threading.h:119
xsens::TaskThread::m_safe
Mutex m_safe
Definition: threading.h:231
CDECL_XS
#define CDECL_XS
Definition: threading.h:159
xsens::TaskThread::TaskType
Definition: threading.h:224
xsens::StandardThread::m_name
char * m_name
Definition: threading.h:105
xsens::TaskThread::~TaskThread
~TaskThread()
Definition: threading.h:260
xsens::WatchDogThread::m_mutex
Mutex m_mutex
Definition: threading.h:178
xsens::WatchDogThread::m_threadId
XsThreadId m_threadId
Definition: threading.h:189
f
f
xsens_mutex.h
xsens::StandardThread::startThread
bool startThread(const char *name=NULL)
Starts the thread.
Definition: threading.cpp:281
xsens::StandardThread::m_yieldOnZeroSleep
volatile std::atomic_bool m_yieldOnZeroSleep
When true, a sleep value of 0 returned by innerFunction will trigger a thread yield operation....
Definition: threading.h:94
xsens::WatchDogThread::WatchDogThread
WatchDogThread(WatchDogFunction func, void *param=NULL)
Constructor.
Definition: threading.cpp:461
xsens::ThreadedFunction::ThreadedFunction
ThreadedFunction(std::function< void()> const &f, bool runNow=true)
Construct the object.
Definition: threading.h:305
XsThread
pthread_t XsThread
A handle for a thread.
Definition: xsthread.h:186
xsens::WatchDogThread::m_running
bool m_running
Definition: threading.h:179
xsens::TaskThread::getLength
int32_t getLength(void) noexcept
Definition: threading.h:278
xsens::WatchDogThread::m_reset
bool m_reset
Definition: threading.h:180
xsens::TaskThread::clear
void clear(void)
Clears a queue.
Definition: threading.h:285
xsens::WatchDogThread::~WatchDogThread
~WatchDogThread()
Destructor.
Definition: threading.cpp:483
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
xsens::TaskThread
Class for handling small tasks.
Definition: threading.h:219
xsens::TaskThread::TaskType::m_param
void * m_param
Definition: threading.h:227
xsens::TaskThread::m_inFunc
bool m_inFunc
Definition: threading.h:232
xsens::StandardThread::exitFunction
virtual void exitFunction(void)
Virtual exit function.
Definition: threading.h:116
xsens::StandardThread::isTerminating
bool isTerminating() volatile const noexcept
Returns whether the thread should (have) terminate(d)
Definition: threading.cpp:182
xsens::StandardThread::signalStopThread
virtual void signalStopThread(void)
Tells the thread to stop but does not wait for it to end.
Definition: threading.cpp:320
xsens::Mutex
A base mutex class.
Definition: xsens_mutex.h:132
xsens::StandardThread::threadHandle
XsThread threadHandle() const
Return the thread handle.
Definition: threading.h:125
xsens::StandardThread::m_running
bool m_running
Indicates that the thread is running.
Definition: threading.h:102
TaskFunction
boost::function< void(DiagnosticStatusWrapper &)> TaskFunction
xsens::StandardThread::StandardThread
StandardThread()
Definition: threading.cpp:89
xsens::StandardThread::m_thread
XsThread m_thread
Definition: threading.h:86
xsens::WatchDogThread::isAlive
bool isAlive(void) volatile const noexcept
Definition: threading.cpp:502
XSENS_THREAD_RETURN
#define XSENS_THREAD_RETURN
Definition: xsthread.h:175
xsens::ThreadedFunction::innerFunction
int32_t innerFunction(void) override final
The internal thread function, runs the supplied function.
Definition: threading.h:317
xsens::WatchDogThread::threadMain
void threadMain(void)
Definition: threading.cpp:671
xsens::StandardThread::threadMain
void threadMain(void)
The inner loop of the thread, calls innerFunction repeatedly and sleeps when necessary.
Definition: threading.cpp:429
xsens::Lock::unlock
bool unlock() noexcept
Unlocks the locked mutex.
Definition: xsens_mutex.h:1032
xsens::WatchDogThread::isRunning
bool isRunning(void) volatile const noexcept
Definition: threading.cpp:517
xsens::WatchDogThread::startTimer
bool startTimer(uint32_t timeout=10000, const char *name=NULL)
Starts a timer using some parameters.
Definition: threading.cpp:551
xsens::StandardThread
A class for a standard thread that has to perform the same action repeatedly.
Definition: threading.h:83
XsThreadPriority
XsThreadPriority
Thread priorities for xsSetThreadPriority() and xsGetThreadPriority()
Definition: xsthread.h:162
xsens::StandardThread::stopThread
void stopThread(void) noexcept
Tells the thread to stop and waits for it to end.
Definition: threading.cpp:334
xsens::WatchDogThread
A class that keeps an eye on a threads timer.
Definition: threading.h:168
xsens::Lock
A base class for a Lock.
Definition: xsens_mutex.h:947
int32_t
signed int int32_t
Definition: pstdint.h:515
param
T param(const std::string &param_name, const T &default_val)
xsens::StandardThread::m_stop
volatile std::atomic_bool m_stop
Indicates that the thread should stop. Derived classes should check isTerminating() instead of direct...
Definition: threading.h:93
xsens::WatchDogThread::threadInit
static XSENS_THREAD_RETURN threadInit(void *obj)
Definition: threading.cpp:661
xsens::StandardThread::isAlive
bool isAlive(void) volatile const noexcept
Definition: threading.cpp:140
xsens::StandardThread::initFunction
virtual void initFunction(void)
Virtual initialization function.
Definition: threading.h:113
xsens::TaskThread::innerFunction
virtual int32_t innerFunction(void)
The inner function of the task thread.
Definition: threading.h:241
xsens::StandardThread::getThreadId
XsThreadId getThreadId(void) const
Definition: threading.h:142
xsens::StandardThread::setPriority
bool setPriority(XsThreadPriority pri)
Sets the priority of the thread.
Definition: threading.cpp:191
xsens::WatchDogFunction
void(CDECL_XS * WatchDogFunction)(void *)
Definition: threading.h:163
xsens::ThreadedFunction::m_func
std::function< void()> m_func
Definition: threading.h:299
xsens
Definition: threading.cpp:78
xsens::StandardThread::m_priority
XsThreadPriority m_priority
Definition: threading.h:87
xsens::WatchDogThread::m_timeout
volatile std::atomic< std::uint32_t > m_timeout
Definition: threading.h:183
xsens::WatchDogThread::m_name
char * m_name
Definition: threading.h:186
xsens::WatchDogThread::m_func
WatchDogFunction m_func
Definition: threading.h:184


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