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.

325 lines
7.8KB

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