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.

341 lines
8.7KB

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