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.

345 lines
8.8KB

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