DISTRHO Plugin Framework
Thread.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DISTRHO_THREAD_HPP_INCLUDED
18 #define DISTRHO_THREAD_HPP_INCLUDED
19 
20 #include "Mutex.hpp"
21 #include "Sleep.hpp"
22 #include "String.hpp"
23 
24 #ifdef DISTRHO_OS_LINUX
25 # include <sys/prctl.h>
26 #endif
27 
28 #ifdef DISTRHO_OS_WASM
29 # error Threads do not work under wasm!
30 #endif
31 
33 
34 // -----------------------------------------------------------------------
35 // Thread class
36 
37 class Thread
38 {
39 protected:
40  /*
41  * Constructor.
42  */
43  Thread(const char* const threadName = nullptr) noexcept
44  : fLock(),
45  fSignal(),
46  fName(threadName),
47 #ifdef PTW32_DLLPORT
48  fHandle({nullptr, 0}),
49 #else
50  fHandle(0),
51 #endif
52  fShouldExit(false) {}
53 
54  /*
55  * Destructor.
56  */
57  virtual ~Thread() /*noexcept*/
58  {
59  DISTRHO_SAFE_ASSERT(! isThreadRunning());
60 
61  stopThread(-1);
62  }
63 
64  /*
65  * Virtual function to be implemented by the subclass.
66  */
67  virtual void run() = 0;
68 
69  // -------------------------------------------------------------------
70 
71 public:
72  /*
73  * Check if the thread is running.
74  */
75  bool isThreadRunning() const noexcept
76  {
77 #ifdef PTW32_DLLPORT
78  return (fHandle.p != nullptr);
79 #else
80  return (fHandle != 0);
81 #endif
82  }
83 
84  /*
85  * Check if the thread should exit.
86  */
87  bool shouldThreadExit() const noexcept
88  {
89  return fShouldExit;
90  }
91 
92  /*
93  * Start the thread.
94  */
95  bool startThread(const bool withRealtimePriority = false) noexcept
96  {
97  // check if already running
98  DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
99 
100  pthread_t handle;
101 
102  pthread_attr_t attr;
103  pthread_attr_init(&attr);
104 
105  struct sched_param sched_param;
106  std::memset(&sched_param, 0, sizeof(sched_param));
107 
108  if (withRealtimePriority)
109  {
110  sched_param.sched_priority = 80;
111 
112 #ifndef DISTRHO_OS_HAIKU
113  if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0 &&
114  pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0 &&
115 # ifndef DISTRHO_OS_WINDOWS
116  (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0 ||
117  pthread_attr_setschedpolicy(&attr, SCHED_RR) == 0) &&
118 # endif
119  pthread_attr_setschedparam(&attr, &sched_param) == 0)
120  {
121  d_stdout("Thread setup with realtime priority successful");
122  }
123  else
124 #endif
125  {
126  d_stdout("Thread setup with realtime priority failed, going with normal priority instead");
127  pthread_attr_destroy(&attr);
128  pthread_attr_init(&attr);
129  }
130  }
131 
132  const MutexLocker ml(fLock);
133 
134  fShouldExit = false;
135 
136  bool ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
137  pthread_attr_destroy(&attr);
138 
139  if (withRealtimePriority && !ok)
140  {
141  d_stdout("Thread with realtime priority failed on creation, going with normal priority instead");
142  pthread_attr_init(&attr);
143  ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
144  pthread_attr_destroy(&attr);
145  }
146 
147  DISTRHO_SAFE_ASSERT_RETURN(ok, false);
148 #ifdef PTW32_DLLPORT
149  DISTRHO_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
150 #else
151  DISTRHO_SAFE_ASSERT_RETURN(handle != 0, false);
152 #endif
153  pthread_detach(handle);
154  _copyFrom(handle);
155 
156  // wait for thread to start
157  fSignal.wait();
158  return true;
159  }
160 
161  /*
162  * Stop the thread.
163  * In the 'timeOutMilliseconds':
164  * = 0 -> no wait
165  * > 0 -> wait timeout value
166  * < 0 -> wait forever
167  */
168  bool stopThread(const int timeOutMilliseconds) noexcept
169  {
170  const MutexLocker ml(fLock);
171 
172  if (isThreadRunning())
173  {
174  signalThreadShouldExit();
175 
176  if (timeOutMilliseconds != 0)
177  {
178  // Wait for the thread to stop
179  int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
180 
181  for (; isThreadRunning();)
182  {
183  d_msleep(2);
184 
185  if (timeOutCheck < 0)
186  continue;
187 
188  if (timeOutCheck > 0)
189  timeOutCheck -= 1;
190  else
191  break;
192  }
193  }
194 
195  if (isThreadRunning())
196  {
197  // should never happen!
198  d_stderr2("assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
199 
200  // copy thread id so we can clear our one
201  pthread_t threadId;
202  _copyTo(threadId);
203  _init();
204 
205  pthread_detach(threadId);
206  return false;
207  }
208  }
209 
210  return true;
211  }
212 
213  /*
214  * Tell the thread to stop as soon as possible.
215  */
216  void signalThreadShouldExit() noexcept
217  {
218  fShouldExit = true;
219  }
220 
221  // -------------------------------------------------------------------
222 
223  /*
224  * Returns the name of the thread.
225  * This is the name that gets set in the constructor.
226  */
227  const String& getThreadName() const noexcept
228  {
229  return fName;
230  }
231 
232  /*
233  * Returns the Id/handle of the thread.
234  */
235  pthread_t getThreadId() const noexcept
236  {
237  return fHandle;
238  }
239 
240  /*
241  * Changes the name of the caller thread.
242  */
243  static void setCurrentThreadName(const char* const name) noexcept
244  {
245  DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
246 
247 #ifdef DISTRHO_OS_LINUX
248  prctl(PR_SET_NAME, name, 0, 0, 0);
249 #endif
250 #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 && !defined(DISTRHO_OS_GNU_HURD)
251  pthread_setname_np(pthread_self(), name);
252 #endif
253  }
254 
255  // -------------------------------------------------------------------
256 
257 private:
258  Mutex fLock; // Thread lock
259  Signal fSignal; // Thread start wait signal
260  const String fName; // Thread name
261  volatile pthread_t fHandle; // Handle for this thread
262  volatile bool fShouldExit; // true if thread should exit
263 
264  /*
265  * Init pthread type.
266  */
267  void _init() noexcept
268  {
269 #ifdef PTW32_DLLPORT
270  fHandle.p = nullptr;
271  fHandle.x = 0;
272 #else
273  fHandle = 0;
274 #endif
275  }
276 
277  /*
278  * Copy our pthread type from another var.
279  */
280  void _copyFrom(const pthread_t& handle) noexcept
281  {
282 #ifdef PTW32_DLLPORT
283  fHandle.p = handle.p;
284  fHandle.x = handle.x;
285 #else
286  fHandle = handle;
287 #endif
288  }
289 
290  /*
291  * Copy our pthread type to another var.
292  */
293  void _copyTo(volatile pthread_t& handle) const noexcept
294  {
295 #ifdef PTW32_DLLPORT
296  handle.p = fHandle.p;
297  handle.x = fHandle.x;
298 #else
299  handle = fHandle;
300 #endif
301  }
302 
303  /*
304  * Thread entry point.
305  */
306  void _runEntryPoint() noexcept
307  {
308  if (fName.isNotEmpty())
309  setCurrentThreadName(fName);
310 
311  // report ready
312  fSignal.signal();
313 
314  try {
315  run();
316  } catch(...) {}
317 
318  // done
319  _init();
320  }
321 
322  /*
323  * Thread entry point.
324  */
325  static void* _entryPoint(void* userData) noexcept
326  {
327  static_cast<Thread*>(userData)->_runEntryPoint();
328  return nullptr;
329  }
330 
331  DISTRHO_DECLARE_NON_COPYABLE(Thread)
332 };
333 
334 // -----------------------------------------------------------------------
335 
337 
338 #endif // DISTRHO_THREAD_HPP_INCLUDED
Definition: Mutex.hpp:40
Definition: Mutex.hpp:269
Definition: Mutex.hpp:187
Definition: String.hpp:31
Definition: Thread.hpp:38
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:834
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:828
static void d_stdout(const char *const fmt,...) noexcept
Definition: DistrhoUtils.hpp:131
static void d_stderr2(const char *const fmt,...) noexcept
Definition: DistrhoUtils.hpp:161