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