Audio plugin host https://kx.studio/carla
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.

291 lines
6.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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. fName(threadName),
  36. #ifdef PTW32_DLLPORT
  37. fHandle({nullptr, 0}),
  38. #else
  39. fHandle(0),
  40. #endif
  41. fShouldExit(false) {}
  42. /*
  43. * Destructor.
  44. */
  45. virtual ~Thread() /*noexcept*/
  46. {
  47. DISTRHO_SAFE_ASSERT(! isThreadRunning());
  48. stopThread(-1);
  49. }
  50. /*
  51. * Virtual function to be implemented by the subclass.
  52. */
  53. virtual void run() = 0;
  54. // -------------------------------------------------------------------
  55. public:
  56. /*
  57. * Check if the thread is running.
  58. */
  59. bool isThreadRunning() const noexcept
  60. {
  61. #ifdef PTW32_DLLPORT
  62. return (fHandle.p != nullptr);
  63. #else
  64. return (fHandle != 0);
  65. #endif
  66. }
  67. /*
  68. * Check if the thread should exit.
  69. */
  70. bool shouldThreadExit() const noexcept
  71. {
  72. return fShouldExit;
  73. }
  74. /*
  75. * Start the thread.
  76. */
  77. bool startThread() noexcept
  78. {
  79. // check if already running
  80. DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
  81. const MutexLocker cml(fLock);
  82. fShouldExit = false;
  83. pthread_t handle;
  84. if (pthread_create(&handle, nullptr, _entryPoint, this) == 0)
  85. {
  86. #ifdef PTW32_DLLPORT
  87. DISTRHO_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
  88. #else
  89. DISTRHO_SAFE_ASSERT_RETURN(handle != 0, false);
  90. #endif
  91. pthread_detach(handle);
  92. _copyFrom(handle);
  93. // wait for thread to start
  94. fLock.lock();
  95. return true;
  96. }
  97. return false;
  98. }
  99. /*
  100. * Stop the thread.
  101. * In the 'timeOutMilliseconds':
  102. * = 0 -> no wait
  103. * > 0 -> wait timeout value
  104. * < 0 -> wait forever
  105. */
  106. bool stopThread(const int timeOutMilliseconds) noexcept
  107. {
  108. const MutexLocker cml(fLock);
  109. if (isThreadRunning())
  110. {
  111. signalThreadShouldExit();
  112. if (timeOutMilliseconds != 0)
  113. {
  114. // Wait for the thread to stop
  115. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  116. for (; isThreadRunning();)
  117. {
  118. d_msleep(2);
  119. if (timeOutCheck < 0)
  120. continue;
  121. if (timeOutCheck > 0)
  122. timeOutCheck -= 1;
  123. else
  124. break;
  125. }
  126. }
  127. if (isThreadRunning())
  128. {
  129. // should never happen!
  130. d_stderr2("assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
  131. // copy thread id so we can clear our one
  132. pthread_t threadId;
  133. _copyTo(threadId);
  134. _init();
  135. try {
  136. pthread_cancel(threadId);
  137. } DISTRHO_SAFE_EXCEPTION("pthread_cancel");
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. /*
  144. * Tell the thread to stop as soon as possible.
  145. */
  146. void signalThreadShouldExit() noexcept
  147. {
  148. fShouldExit = true;
  149. }
  150. // -------------------------------------------------------------------
  151. /*
  152. * Returns the name of the thread.
  153. * This is the name that gets set in the constructor.
  154. */
  155. const String& getThreadName() const noexcept
  156. {
  157. return fName;
  158. }
  159. /*
  160. * Changes the name of the caller thread.
  161. */
  162. static void setCurrentThreadName(const char* const name) noexcept
  163. {
  164. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  165. #ifdef DISTRHO_OS_LINUX
  166. prctl(PR_SET_NAME, name, 0, 0, 0);
  167. #endif
  168. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  169. pthread_setname_np(pthread_self(), name);
  170. #endif
  171. }
  172. // -------------------------------------------------------------------
  173. private:
  174. Mutex fLock; // Thread lock
  175. const String fName; // Thread name
  176. volatile pthread_t fHandle; // Handle for this thread
  177. volatile bool fShouldExit; // true if thread should exit
  178. /*
  179. * Init pthread type.
  180. */
  181. void _init() noexcept
  182. {
  183. #ifdef PTW32_DLLPORT
  184. fHandle.p = nullptr;
  185. fHandle.x = 0;
  186. #else
  187. fHandle = 0;
  188. #endif
  189. }
  190. /*
  191. * Copy our pthread type from another var.
  192. */
  193. void _copyFrom(const pthread_t& handle) noexcept
  194. {
  195. #ifdef PTW32_DLLPORT
  196. fHandle.p = handle.p;
  197. fHandle.x = handle.x;
  198. #else
  199. fHandle = handle;
  200. #endif
  201. }
  202. /*
  203. * Copy our pthread type to another var.
  204. */
  205. void _copyTo(volatile pthread_t& handle) const noexcept
  206. {
  207. #ifdef PTW32_DLLPORT
  208. handle.p = fHandle.p;
  209. handle.x = fHandle.x;
  210. #else
  211. handle = fHandle;
  212. #endif
  213. }
  214. /*
  215. * Thread entry point.
  216. */
  217. void _runEntryPoint() noexcept
  218. {
  219. // report ready
  220. fLock.unlock();
  221. setCurrentThreadName(fName);
  222. try {
  223. run();
  224. } catch(...) {}
  225. // done
  226. _init();
  227. }
  228. /*
  229. * Thread entry point.
  230. */
  231. static void* _entryPoint(void* userData) noexcept
  232. {
  233. static_cast<Thread*>(userData)->_runEntryPoint();
  234. return nullptr;
  235. }
  236. DISTRHO_DECLARE_NON_COPY_CLASS(Thread)
  237. };
  238. // -----------------------------------------------------------------------
  239. END_NAMESPACE_DISTRHO
  240. #endif // DISTRHO_THREAD_HPP_INCLUDED