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.

238 lines
5.8KB

  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. carla_zeroStruct<pthread_t>(fHandle);
  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. if (pthread_create(&fHandle, nullptr, _entryPoint, this) == 0)
  82. {
  83. #ifdef CARLA_OS_WIN
  84. CARLA_SAFE_ASSERT_RETURN(fHandle.p != nullptr, false);
  85. #else
  86. CARLA_SAFE_ASSERT_RETURN(fHandle != 0, false);
  87. #endif
  88. pthread_detach(fHandle);
  89. // wait for thread to start
  90. fLock.lock();
  91. return true;
  92. }
  93. return false;
  94. }
  95. /*
  96. * Stop the thread.
  97. * In the 'timeOutMilliseconds':
  98. * = 0 -> no wait
  99. * > 0 -> wait timeout value
  100. * < 0 -> wait forever
  101. */
  102. bool stopThread(const int timeOutMilliseconds) noexcept
  103. {
  104. const CarlaMutexLocker cml(fLock);
  105. if (isThreadRunning())
  106. {
  107. signalThreadShouldExit();
  108. if (timeOutMilliseconds != 0)
  109. {
  110. // Wait for the thread to stop
  111. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  112. for (; isThreadRunning();)
  113. {
  114. carla_msleep(2);
  115. if (timeOutCheck < 0)
  116. continue;
  117. if (timeOutCheck > 0)
  118. timeOutCheck -= 1;
  119. else
  120. break;
  121. }
  122. }
  123. if (isThreadRunning())
  124. {
  125. // should never happen!
  126. carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
  127. // copy thread id so we can clear our one
  128. pthread_t threadId;
  129. carla_copyStruct<pthread_t>(threadId, fHandle);
  130. carla_zeroStruct<pthread_t>(fHandle);
  131. try {
  132. pthread_cancel(threadId);
  133. } catch(...) {}
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. /*
  140. * Tell the thread to stop as soon as possible.
  141. */
  142. void signalThreadShouldExit() noexcept
  143. {
  144. fShouldExit = true;
  145. }
  146. // -------------------------------------------------------------------
  147. /*
  148. * Returns the name of the thread.
  149. * This is the name that gets set in the constructor.
  150. */
  151. const CarlaString& getThreadName() const noexcept
  152. {
  153. return fName;
  154. }
  155. /*
  156. * Changes the name of the caller thread.
  157. */
  158. static void setCurrentThreadName(const char* const name) noexcept
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  161. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  162. pthread_setname_np(pthread_self(), name);
  163. #elif defined(CARLA_OS_LINUX)
  164. prctl(PR_SET_NAME, name, 0, 0, 0);
  165. #endif
  166. }
  167. // -------------------------------------------------------------------
  168. private:
  169. CarlaMutex fLock; // Thread lock
  170. const CarlaString fName; // Thread name
  171. pthread_t fHandle; // Handle for this thread
  172. volatile bool fShouldExit; // true if thread should exit
  173. void _runEntryPoint() noexcept
  174. {
  175. // report ready
  176. fLock.unlock();
  177. setCurrentThreadName(fName);
  178. try {
  179. run();
  180. } catch(...) {}
  181. // done
  182. const CarlaMutexLocker cml(fLock);
  183. carla_zeroStruct<pthread_t>(fHandle);
  184. }
  185. static void* _entryPoint(void* userData) noexcept
  186. {
  187. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  188. return nullptr;
  189. }
  190. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  191. };
  192. // -----------------------------------------------------------------------
  193. #endif // CARLA_THREAD_HPP_INCLUDED