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.

d_thread.hpp 6.8KB

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