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.6KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Carla Thread
  3. * Copyright (C) 2013-2022 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_WASM
  23. # error Threads do not work under wasm!
  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. #ifndef CARLA_OS_WASM
  36. fSignal(),
  37. #endif
  38. fName(threadName),
  39. #ifdef PTW32_DLLPORT
  40. fHandle({nullptr, 0}),
  41. #else
  42. fHandle(0),
  43. #endif
  44. fShouldExit(false) {}
  45. /*
  46. * Destructor.
  47. */
  48. virtual ~CarlaThread() /*noexcept*/
  49. {
  50. CARLA_SAFE_ASSERT(! isThreadRunning());
  51. stopThread(-1);
  52. }
  53. /*
  54. * Virtual function to be implemented by the subclass.
  55. */
  56. virtual void run() = 0;
  57. // -------------------------------------------------------------------
  58. public:
  59. /*
  60. * Check if the thread is running.
  61. */
  62. bool isThreadRunning() const noexcept
  63. {
  64. #ifdef PTW32_DLLPORT
  65. return (fHandle.p != nullptr);
  66. #else
  67. return (fHandle != 0);
  68. #endif
  69. }
  70. /*
  71. * Check if the thread should exit.
  72. */
  73. bool shouldThreadExit() const noexcept
  74. {
  75. return fShouldExit;
  76. }
  77. /*
  78. * Start the thread.
  79. */
  80. bool startThread(bool withRealtimePriority = false) noexcept
  81. {
  82. // check if already running
  83. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), true);
  84. if (withRealtimePriority && std::getenv("CARLA_BRIDGE_DUMMY") != nullptr)
  85. withRealtimePriority = false;
  86. pthread_t handle;
  87. pthread_attr_t attr;
  88. pthread_attr_init(&attr);
  89. struct sched_param sched_param;
  90. carla_zeroStruct(sched_param);
  91. if (withRealtimePriority)
  92. {
  93. sched_param.sched_priority = 80;
  94. #if !defined(CARLA_OS_HAIKU) && !defined(CARLA_OS_WASM)
  95. if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0 &&
  96. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0 &&
  97. # ifndef CARLA_OS_WIN
  98. (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0 ||
  99. pthread_attr_setschedpolicy(&attr, SCHED_RR) == 0) &&
  100. # endif
  101. pthread_attr_setschedparam(&attr, &sched_param) == 0)
  102. {
  103. carla_stdout("CarlaThread setup with realtime priority successful");
  104. }
  105. else
  106. #endif
  107. {
  108. carla_stdout("CarlaThread setup with realtime priority failed, going with normal priority instead");
  109. pthread_attr_destroy(&attr);
  110. pthread_attr_init(&attr);
  111. }
  112. }
  113. pthread_attr_setdetachstate(&attr, 1);
  114. const CarlaMutexLocker cml(fLock);
  115. fShouldExit = false;
  116. bool ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
  117. pthread_attr_destroy(&attr);
  118. if (withRealtimePriority && !ok)
  119. {
  120. carla_stdout("CarlaThread with realtime priority failed on creation, going with normal priority instead");
  121. pthread_attr_init(&attr);
  122. pthread_attr_setdetachstate(&attr, 1);
  123. ok = pthread_create(&handle, &attr, _entryPoint, this) == 0;
  124. pthread_attr_destroy(&attr);
  125. }
  126. #ifndef CARLA_OS_WASM
  127. CARLA_SAFE_ASSERT_RETURN(ok, false);
  128. # ifdef PTW32_DLLPORT
  129. CARLA_SAFE_ASSERT_RETURN(handle.p != nullptr, false);
  130. # else
  131. CARLA_SAFE_ASSERT_RETURN(handle != 0, false);
  132. # endif
  133. #endif
  134. _copyFrom(handle);
  135. #ifndef CARLA_OS_WASM
  136. // wait for thread to start
  137. fSignal.wait();
  138. #endif
  139. return true;
  140. }
  141. /*
  142. * Stop the thread.
  143. * In the 'timeOutMilliseconds':
  144. * = 0 -> no wait
  145. * > 0 -> wait timeout value
  146. * < 0 -> wait forever
  147. */
  148. bool stopThread(const int timeOutMilliseconds) noexcept
  149. {
  150. const CarlaMutexLocker cml(fLock);
  151. if (isThreadRunning())
  152. {
  153. signalThreadShouldExit();
  154. if (timeOutMilliseconds != 0)
  155. {
  156. // Wait for the thread to stop
  157. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  158. for (; isThreadRunning();)
  159. {
  160. carla_msleep(2);
  161. if (timeOutCheck < 0)
  162. continue;
  163. if (timeOutCheck > 0)
  164. timeOutCheck -= 1;
  165. else
  166. break;
  167. }
  168. }
  169. if (isThreadRunning())
  170. {
  171. // should never happen!
  172. carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
  173. // copy thread id so we can clear our one
  174. pthread_t threadId;
  175. _copyTo(threadId);
  176. _init();
  177. pthread_detach(threadId);
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. /*
  184. * Tell the thread to stop as soon as possible.
  185. */
  186. void signalThreadShouldExit() noexcept
  187. {
  188. fShouldExit = true;
  189. }
  190. // -------------------------------------------------------------------
  191. /*
  192. * Returns the name of the thread.
  193. * This is the name that gets set in the constructor.
  194. */
  195. const CarlaString& getThreadName() const noexcept
  196. {
  197. return fName;
  198. }
  199. /*
  200. * Returns the Id/handle of the thread.
  201. */
  202. pthread_t getThreadId() const noexcept
  203. {
  204. return fHandle;
  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. #ifndef CARLA_OS_WASM
  221. CarlaSignal fSignal; // Thread start wait signal
  222. #endif
  223. const CarlaString fName; // Thread name
  224. volatile pthread_t fHandle; // Handle for this thread
  225. volatile bool fShouldExit; // true if thread should exit
  226. /*
  227. * Init pthread type.
  228. */
  229. void _init() noexcept
  230. {
  231. #ifdef PTW32_DLLPORT
  232. fHandle.p = nullptr;
  233. fHandle.x = 0;
  234. #else
  235. fHandle = 0;
  236. #endif
  237. }
  238. /*
  239. * Copy our pthread type from another var.
  240. */
  241. void _copyFrom(const pthread_t& handle) noexcept
  242. {
  243. #ifdef PTW32_DLLPORT
  244. fHandle.p = handle.p;
  245. fHandle.x = handle.x;
  246. #else
  247. fHandle = handle;
  248. #endif
  249. }
  250. /*
  251. * Copy our pthread type to another var.
  252. */
  253. void _copyTo(volatile pthread_t& handle) const noexcept
  254. {
  255. #ifdef PTW32_DLLPORT
  256. handle.p = fHandle.p;
  257. handle.x = fHandle.x;
  258. #else
  259. handle = fHandle;
  260. #endif
  261. }
  262. /*
  263. * Thread entry point.
  264. */
  265. void _runEntryPoint() noexcept
  266. {
  267. if (fName.isNotEmpty())
  268. setCurrentThreadName(fName);
  269. #ifndef CARLA_OS_WASM
  270. // report ready
  271. fSignal.signal();
  272. #endif
  273. try {
  274. run();
  275. } catch(...) {}
  276. // done
  277. _init();
  278. }
  279. /*
  280. * Thread entry point.
  281. */
  282. static void* _entryPoint(void* userData) noexcept
  283. {
  284. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  285. return nullptr;
  286. }
  287. CARLA_DECLARE_NON_COPYABLE(CarlaThread)
  288. };
  289. // -----------------------------------------------------------------------
  290. #endif // CARLA_THREAD_HPP_INCLUDED