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 6.5KB

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