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.

296 lines
6.7KB

  1. /*
  2. * Carla Thread
  3. * Copyright (C) 2013-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_THREAD_HPP_INCLUDED
  18. #define CARLA_THREAD_HPP_INCLUDED
  19. #include "CarlaMutex.hpp"
  20. #include "CarlaString.hpp"
  21. #ifdef CARLA_OS_LINUX
  22. # include <sys/prctl.h>
  23. #endif
  24. // -----------------------------------------------------------------------
  25. // CarlaThread class
  26. class CarlaThread
  27. {
  28. protected:
  29. /*
  30. * Constructor.
  31. */
  32. CarlaThread(const char* const threadName = nullptr) noexcept
  33. : fLock(),
  34. fSignal(),
  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 ~CarlaThread() /*noexcept*/
  46. {
  47. CARLA_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. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
  81. const CarlaMutexLocker cml(fLock);
  82. fShouldExit = false;
  83. pthread_t handle;
  84. if (pthread_create(&handle, nullptr, _entryPoint, this) == 0)
  85. {
  86. #ifdef PTW32_DLLPORT
  87. CARLA_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
  88. #else
  89. CARLA_SAFE_ASSERT_RETURN(handle != 0, false);
  90. #endif
  91. pthread_detach(handle);
  92. _copyFrom(handle);
  93. // wait for thread to start
  94. fSignal.wait();
  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 CarlaMutexLocker 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. carla_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. carla_stderr2("Carla 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. } CARLA_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 CarlaString& getThreadName() const noexcept
  156. {
  157. return fName;
  158. }
  159. /*
  160. * Returns the Id/handle of the thread.
  161. */
  162. pthread_t getThreadId() const noexcept
  163. {
  164. return fHandle;
  165. }
  166. /*
  167. * Changes the name of the caller thread.
  168. */
  169. static void setCurrentThreadName(const char* const name) noexcept
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  172. #ifdef CARLA_OS_LINUX
  173. prctl(PR_SET_NAME, name, 0, 0, 0);
  174. #endif
  175. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  176. pthread_setname_np(pthread_self(), name);
  177. #endif
  178. }
  179. // -------------------------------------------------------------------
  180. private:
  181. CarlaMutex fLock; // Thread lock
  182. CarlaSignal fSignal; // Thread start wait signal
  183. const CarlaString fName; // Thread name
  184. volatile pthread_t fHandle; // Handle for this thread
  185. volatile bool fShouldExit; // true if thread should exit
  186. /*
  187. * Init pthread type.
  188. */
  189. void _init() noexcept
  190. {
  191. #ifdef PTW32_DLLPORT
  192. fHandle.p = nullptr;
  193. fHandle.x = 0;
  194. #else
  195. fHandle = 0;
  196. #endif
  197. }
  198. /*
  199. * Copy our pthread type from another var.
  200. */
  201. void _copyFrom(const pthread_t& handle) noexcept
  202. {
  203. #ifdef PTW32_DLLPORT
  204. fHandle.p = handle.p;
  205. fHandle.x = handle.x;
  206. #else
  207. fHandle = handle;
  208. #endif
  209. }
  210. /*
  211. * Copy our pthread type to another var.
  212. */
  213. void _copyTo(volatile pthread_t& handle) const noexcept
  214. {
  215. #ifdef PTW32_DLLPORT
  216. handle.p = fHandle.p;
  217. handle.x = fHandle.x;
  218. #else
  219. handle = fHandle;
  220. #endif
  221. }
  222. /*
  223. * Thread entry point.
  224. */
  225. void _runEntryPoint() noexcept
  226. {
  227. setCurrentThreadName(fName);
  228. // report ready
  229. fSignal.signal();
  230. try {
  231. run();
  232. } catch(...) {}
  233. // done
  234. _init();
  235. }
  236. /*
  237. * Thread entry point.
  238. */
  239. static void* _entryPoint(void* userData) noexcept
  240. {
  241. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  242. return nullptr;
  243. }
  244. CARLA_DECLARE_NON_COPY_CLASS(CarlaThread)
  245. };
  246. // -----------------------------------------------------------------------
  247. #endif // CARLA_THREAD_HPP_INCLUDED