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.

335 lines
8.3KB

  1. /*
  2. * Carla Thread
  3. * Copyright (C) 2013-2021 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. #include "CarlaProcessUtils.hpp"
  22. #ifdef CARLA_OS_LINUX
  23. # include <sys/prctl.h>
  24. #endif
  25. // -----------------------------------------------------------------------
  26. // CarlaThread class
  27. class CarlaThread
  28. {
  29. protected:
  30. /*
  31. * Constructor.
  32. */
  33. CarlaThread(const char* const threadName) noexcept
  34. : fLock(),
  35. fSignal(),
  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(bool withRealtimePriority = false) noexcept
  79. {
  80. // check if already running
  81. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
  82. if (withRealtimePriority && std::getenv("CARLA_BRIDGE_DUMMY") != nullptr)
  83. withRealtimePriority = false;
  84. pthread_t handle;
  85. pthread_attr_t attr;
  86. pthread_attr_init(&attr);
  87. struct sched_param sched_param;
  88. carla_zeroStruct(sched_param);
  89. if (withRealtimePriority)
  90. {
  91. sched_param.sched_priority = 80;
  92. #ifndef CARLA_OS_HAIKU
  93. if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0 &&
  94. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0 &&
  95. # ifndef CARLA_OS_WIN
  96. (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0 ||
  97. pthread_attr_setschedpolicy(&attr, SCHED_RR) == 0) &&
  98. # endif
  99. pthread_attr_setschedparam(&attr, &sched_param) == 0)
  100. {
  101. carla_stdout("CarlaThread setup with realtime priority successful");
  102. }
  103. else
  104. #endif
  105. {
  106. carla_stdout("CarlaThread setup with realtime priority failed, going with normal priority instead");
  107. pthread_attr_destroy(&attr);
  108. pthread_attr_init(&attr);
  109. }
  110. }
  111. pthread_attr_setdetachstate(&attr, 1);
  112. const CarlaMutexLocker cml(fLock);
  113. fShouldExit = false;
  114. bool ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
  115. pthread_attr_destroy(&attr);
  116. if (withRealtimePriority && !ok)
  117. {
  118. carla_stdout("CarlaThread with realtime priority failed on creation, going with normal priority instead");
  119. pthread_attr_init(&attr);
  120. pthread_attr_setdetachstate(&attr, 1);
  121. ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
  122. pthread_attr_destroy(&attr);
  123. }
  124. CARLA_SAFE_ASSERT_RETURN(ok, false);
  125. #ifdef PTW32_DLLPORT
  126. CARLA_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
  127. #else
  128. CARLA_SAFE_ASSERT_RETURN(handle != 0, false);
  129. #endif
  130. _copyFrom(handle);
  131. // wait for thread to start
  132. fSignal.wait();
  133. return true;
  134. }
  135. /*
  136. * Stop the thread.
  137. * In the 'timeOutMilliseconds':
  138. * = 0 -> no wait
  139. * > 0 -> wait timeout value
  140. * < 0 -> wait forever
  141. */
  142. bool stopThread(const int timeOutMilliseconds) noexcept
  143. {
  144. const CarlaMutexLocker cml(fLock);
  145. if (isThreadRunning())
  146. {
  147. signalThreadShouldExit();
  148. if (timeOutMilliseconds != 0)
  149. {
  150. // Wait for the thread to stop
  151. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  152. for (; isThreadRunning();)
  153. {
  154. carla_msleep(2);
  155. if (timeOutCheck < 0)
  156. continue;
  157. if (timeOutCheck > 0)
  158. timeOutCheck -= 1;
  159. else
  160. break;
  161. }
  162. }
  163. if (isThreadRunning())
  164. {
  165. // should never happen!
  166. carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
  167. // copy thread id so we can clear our one
  168. pthread_t threadId;
  169. _copyTo(threadId);
  170. _init();
  171. pthread_detach(threadId);
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. /*
  178. * Tell the thread to stop as soon as possible.
  179. */
  180. void signalThreadShouldExit() noexcept
  181. {
  182. fShouldExit = true;
  183. }
  184. // -------------------------------------------------------------------
  185. /*
  186. * Returns the name of the thread.
  187. * This is the name that gets set in the constructor.
  188. */
  189. const CarlaString& getThreadName() const noexcept
  190. {
  191. return fName;
  192. }
  193. /*
  194. * Returns the Id/handle of the thread.
  195. */
  196. pthread_t getThreadId() const noexcept
  197. {
  198. return fHandle;
  199. }
  200. /*
  201. * Changes the name of the caller thread.
  202. */
  203. static void setCurrentThreadName(const char* const name) noexcept
  204. {
  205. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  206. carla_setProcessName(name);
  207. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 && !defined(CARLA_OS_GNU_HURD)
  208. pthread_setname_np(pthread_self(), name);
  209. #endif
  210. }
  211. // -------------------------------------------------------------------
  212. private:
  213. CarlaMutex fLock; // Thread lock
  214. CarlaSignal fSignal; // Thread start wait signal
  215. const CarlaString fName; // Thread name
  216. volatile pthread_t fHandle; // Handle for this thread
  217. volatile bool fShouldExit; // true if thread should exit
  218. /*
  219. * Init pthread type.
  220. */
  221. void _init() noexcept
  222. {
  223. #ifdef PTW32_DLLPORT
  224. fHandle.p = nullptr;
  225. fHandle.x = 0;
  226. #else
  227. fHandle = 0;
  228. #endif
  229. }
  230. /*
  231. * Copy our pthread type from another var.
  232. */
  233. void _copyFrom(const pthread_t& handle) noexcept
  234. {
  235. #ifdef PTW32_DLLPORT
  236. fHandle.p = handle.p;
  237. fHandle.x = handle.x;
  238. #else
  239. fHandle = handle;
  240. #endif
  241. }
  242. /*
  243. * Copy our pthread type to another var.
  244. */
  245. void _copyTo(volatile pthread_t& handle) const noexcept
  246. {
  247. #ifdef PTW32_DLLPORT
  248. handle.p = fHandle.p;
  249. handle.x = fHandle.x;
  250. #else
  251. handle = fHandle;
  252. #endif
  253. }
  254. /*
  255. * Thread entry point.
  256. */
  257. void _runEntryPoint() noexcept
  258. {
  259. if (fName.isNotEmpty())
  260. setCurrentThreadName(fName);
  261. // report ready
  262. fSignal.signal();
  263. try {
  264. run();
  265. } catch(...) {}
  266. // done
  267. _init();
  268. }
  269. /*
  270. * Thread entry point.
  271. */
  272. static void* _entryPoint(void* userData) noexcept
  273. {
  274. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  275. return nullptr;
  276. }
  277. CARLA_DECLARE_NON_COPY_CLASS(CarlaThread)
  278. };
  279. // -----------------------------------------------------------------------
  280. #endif // CARLA_THREAD_HPP_INCLUDED