DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
8.3KB

  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. #ifndef DISTRHO_THREAD_HPP_INCLUDED
  17. #define DISTRHO_THREAD_HPP_INCLUDED
  18. #include "Mutex.hpp"
  19. #include "Sleep.hpp"
  20. #include "String.hpp"
  21. #ifdef DISTRHO_OS_LINUX
  22. # include <sys/prctl.h>
  23. #endif
  24. START_NAMESPACE_DISTRHO
  25. // -----------------------------------------------------------------------
  26. // Thread class
  27. class Thread
  28. {
  29. protected:
  30. /*
  31. * Constructor.
  32. */
  33. Thread(const char* const threadName = nullptr) noexcept
  34. : fLock(),
  35. fSignal(),
  36. fName(threadName),
  37. #ifdef PTW32_DLLPORT
  38. fHandle({nullptr, 0}),
  39. #else
  40. fHandle(0),
  41. #endif
  42. fShouldExit(false) {}
  43. /*
  44. * Destructor.
  45. */
  46. virtual ~Thread() /*noexcept*/
  47. {
  48. DISTRHO_SAFE_ASSERT(! isThreadRunning());
  49. stopThread(-1);
  50. }
  51. /*
  52. * Virtual function to be implemented by the subclass.
  53. */
  54. virtual void run() = 0;
  55. // -------------------------------------------------------------------
  56. public:
  57. /*
  58. * Check if the thread is running.
  59. */
  60. bool isThreadRunning() const noexcept
  61. {
  62. #ifdef PTW32_DLLPORT
  63. return (fHandle.p != nullptr);
  64. #else
  65. return (fHandle != 0);
  66. #endif
  67. }
  68. /*
  69. * Check if the thread should exit.
  70. */
  71. bool shouldThreadExit() const noexcept
  72. {
  73. return fShouldExit;
  74. }
  75. /*
  76. * Start the thread.
  77. */
  78. bool startThread(const bool withRealtimePriority = false) noexcept
  79. {
  80. // check if already running
  81. DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
  82. pthread_t handle;
  83. pthread_attr_t attr;
  84. pthread_attr_init(&attr);
  85. struct sched_param sched_param;
  86. std::memset(&sched_param, 0, sizeof(sched_param));
  87. if (withRealtimePriority)
  88. {
  89. sched_param.sched_priority = 80;
  90. #ifndef DISTRHO_OS_HAIKU
  91. if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0 &&
  92. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0 &&
  93. # ifndef DISTRHO_OS_WINDOWS
  94. (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0 ||
  95. pthread_attr_setschedpolicy(&attr, SCHED_RR) == 0) &&
  96. # endif
  97. pthread_attr_setschedparam(&attr, &sched_param) == 0)
  98. {
  99. d_stdout("Thread setup with realtime priority successful");
  100. }
  101. else
  102. #endif
  103. {
  104. d_stdout("Thread setup with realtime priority failed, going with normal priority instead");
  105. pthread_attr_destroy(&attr);
  106. pthread_attr_init(&attr);
  107. }
  108. }
  109. const MutexLocker ml(fLock);
  110. fShouldExit = false;
  111. bool ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
  112. pthread_attr_destroy(&attr);
  113. if (withRealtimePriority && !ok)
  114. {
  115. d_stdout("Thread with realtime priority failed on creation, going with normal priority instead");
  116. pthread_attr_init(&attr);
  117. ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
  118. pthread_attr_destroy(&attr);
  119. }
  120. DISTRHO_SAFE_ASSERT_RETURN(ok, false);
  121. #ifdef PTW32_DLLPORT
  122. DISTRHO_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
  123. #else
  124. DISTRHO_SAFE_ASSERT_RETURN(handle != 0, false);
  125. #endif
  126. pthread_detach(handle);
  127. _copyFrom(handle);
  128. // wait for thread to start
  129. fSignal.wait();
  130. return true;
  131. }
  132. /*
  133. * Stop the thread.
  134. * In the 'timeOutMilliseconds':
  135. * = 0 -> no wait
  136. * > 0 -> wait timeout value
  137. * < 0 -> wait forever
  138. */
  139. bool stopThread(const int timeOutMilliseconds) noexcept
  140. {
  141. const MutexLocker ml(fLock);
  142. if (isThreadRunning())
  143. {
  144. signalThreadShouldExit();
  145. if (timeOutMilliseconds != 0)
  146. {
  147. // Wait for the thread to stop
  148. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  149. for (; isThreadRunning();)
  150. {
  151. d_msleep(2);
  152. if (timeOutCheck < 0)
  153. continue;
  154. if (timeOutCheck > 0)
  155. timeOutCheck -= 1;
  156. else
  157. break;
  158. }
  159. }
  160. if (isThreadRunning())
  161. {
  162. // should never happen!
  163. d_stderr2("assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
  164. // copy thread id so we can clear our one
  165. pthread_t threadId;
  166. _copyTo(threadId);
  167. _init();
  168. pthread_detach(threadId);
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174. /*
  175. * Tell the thread to stop as soon as possible.
  176. */
  177. void signalThreadShouldExit() noexcept
  178. {
  179. fShouldExit = true;
  180. }
  181. // -------------------------------------------------------------------
  182. /*
  183. * Returns the name of the thread.
  184. * This is the name that gets set in the constructor.
  185. */
  186. const String& getThreadName() const noexcept
  187. {
  188. return fName;
  189. }
  190. /*
  191. * Returns the Id/handle of the thread.
  192. */
  193. pthread_t getThreadId() const noexcept
  194. {
  195. return fHandle;
  196. }
  197. /*
  198. * Changes the name of the caller thread.
  199. */
  200. static void setCurrentThreadName(const char* const name) noexcept
  201. {
  202. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  203. #ifdef DISTRHO_OS_LINUX
  204. prctl(PR_SET_NAME, name, 0, 0, 0);
  205. #endif
  206. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 && !defined(DISTRHO_OS_GNU_HURD)
  207. pthread_setname_np(pthread_self(), name);
  208. #endif
  209. }
  210. // -------------------------------------------------------------------
  211. private:
  212. Mutex fLock; // Thread lock
  213. Signal fSignal; // Thread start wait signal
  214. const String fName; // Thread name
  215. volatile pthread_t fHandle; // Handle for this thread
  216. volatile bool fShouldExit; // true if thread should exit
  217. /*
  218. * Init pthread type.
  219. */
  220. void _init() noexcept
  221. {
  222. #ifdef PTW32_DLLPORT
  223. fHandle.p = nullptr;
  224. fHandle.x = 0;
  225. #else
  226. fHandle = 0;
  227. #endif
  228. }
  229. /*
  230. * Copy our pthread type from another var.
  231. */
  232. void _copyFrom(const pthread_t& handle) noexcept
  233. {
  234. #ifdef PTW32_DLLPORT
  235. fHandle.p = handle.p;
  236. fHandle.x = handle.x;
  237. #else
  238. fHandle = handle;
  239. #endif
  240. }
  241. /*
  242. * Copy our pthread type to another var.
  243. */
  244. void _copyTo(volatile pthread_t& handle) const noexcept
  245. {
  246. #ifdef PTW32_DLLPORT
  247. handle.p = fHandle.p;
  248. handle.x = fHandle.x;
  249. #else
  250. handle = fHandle;
  251. #endif
  252. }
  253. /*
  254. * Thread entry point.
  255. */
  256. void _runEntryPoint() noexcept
  257. {
  258. if (fName.isNotEmpty())
  259. setCurrentThreadName(fName);
  260. // report ready
  261. fSignal.signal();
  262. try {
  263. run();
  264. } catch(...) {}
  265. // done
  266. _init();
  267. }
  268. /*
  269. * Thread entry point.
  270. */
  271. static void* _entryPoint(void* userData) noexcept
  272. {
  273. static_cast<Thread*>(userData)->_runEntryPoint();
  274. return nullptr;
  275. }
  276. DISTRHO_DECLARE_NON_COPYABLE(Thread)
  277. };
  278. // -----------------------------------------------------------------------
  279. END_NAMESPACE_DISTRHO
  280. #endif // DISTRHO_THREAD_HPP_INCLUDED