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.

CarlaThread.hpp 8.7KB

10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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