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.

288 lines
6.6KB

  1. /*
  2. * Carla Thread
  3. * Copyright (C) 2013-2014 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. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  22. // has pthread_setname_np
  23. #elif defined(CARLA_OS_LINUX)
  24. # include <sys/prctl.h>
  25. #endif
  26. // -----------------------------------------------------------------------
  27. // CarlaThread class
  28. class CarlaThread
  29. {
  30. protected:
  31. /*
  32. * Constructor.
  33. */
  34. CarlaThread(const char* const threadName = nullptr) noexcept
  35. : fLock(),
  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 ~CarlaThread() /*noexcept*/
  47. {
  48. CARLA_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() noexcept
  79. {
  80. // check if already running
  81. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
  82. const CarlaMutexLocker cml(fLock);
  83. fShouldExit = false;
  84. pthread_t handle;
  85. if (pthread_create(&handle, nullptr, _entryPoint, this) == 0)
  86. {
  87. #ifdef PTW32_DLLPORT
  88. CARLA_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
  89. #else
  90. CARLA_SAFE_ASSERT_RETURN(handle != 0, false);
  91. #endif
  92. pthread_detach(handle);
  93. _copyFrom(handle);
  94. // wait for thread to start
  95. fLock.lock();
  96. return true;
  97. }
  98. return false;
  99. }
  100. /*
  101. * Stop the thread.
  102. * In the 'timeOutMilliseconds':
  103. * = 0 -> no wait
  104. * > 0 -> wait timeout value
  105. * < 0 -> wait forever
  106. */
  107. bool stopThread(const int timeOutMilliseconds) noexcept
  108. {
  109. const CarlaMutexLocker cml(fLock);
  110. if (isThreadRunning())
  111. {
  112. signalThreadShouldExit();
  113. if (timeOutMilliseconds != 0)
  114. {
  115. // Wait for the thread to stop
  116. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  117. for (; isThreadRunning();)
  118. {
  119. carla_msleep(2);
  120. if (timeOutCheck < 0)
  121. continue;
  122. if (timeOutCheck > 0)
  123. timeOutCheck -= 1;
  124. else
  125. break;
  126. }
  127. }
  128. if (isThreadRunning())
  129. {
  130. // should never happen!
  131. carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
  132. // copy thread id so we can clear our one
  133. pthread_t threadId;
  134. _copyTo(threadId);
  135. _init();
  136. try {
  137. pthread_cancel(threadId);
  138. } CARLA_SAFE_EXCEPTION("pthread_cancel");
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. /*
  145. * Tell the thread to stop as soon as possible.
  146. */
  147. void signalThreadShouldExit() noexcept
  148. {
  149. fShouldExit = true;
  150. }
  151. // -------------------------------------------------------------------
  152. /*
  153. * Returns the name of the thread.
  154. * This is the name that gets set in the constructor.
  155. */
  156. const CarlaString& getThreadName() const noexcept
  157. {
  158. return fName;
  159. }
  160. /*
  161. * Changes the name of the caller thread.
  162. */
  163. static void setCurrentThreadName(const char* const name) noexcept
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  166. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  167. pthread_setname_np(pthread_self(), name);
  168. #elif defined(CARLA_OS_LINUX)
  169. prctl(PR_SET_NAME, name, 0, 0, 0);
  170. #endif
  171. }
  172. // -------------------------------------------------------------------
  173. private:
  174. CarlaMutex fLock; // Thread lock
  175. const CarlaString 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<CarlaThread*>(userData)->_runEntryPoint();
  234. return nullptr;
  235. }
  236. CARLA_DECLARE_NON_COPY_CLASS(CarlaThread)
  237. };
  238. // -----------------------------------------------------------------------
  239. #endif // CARLA_THREAD_HPP_INCLUDED