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.

210 lines
4.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. // -----------------------------------------------------------------------
  22. // CarlaThread class
  23. class CarlaThread
  24. {
  25. protected:
  26. /*
  27. * Constructor.
  28. */
  29. CarlaThread(const char* const threadName = nullptr) /*noexcept*/
  30. : fName(threadName),
  31. fShouldExit(false)
  32. {
  33. _init();
  34. }
  35. /*
  36. * Destructor.
  37. */
  38. virtual ~CarlaThread() /*noexcept*/
  39. {
  40. CARLA_SAFE_ASSERT(! isThreadRunning());
  41. stopThread(-1);
  42. }
  43. /*
  44. * Virtual function to be implemented by the subclass.
  45. */
  46. virtual void run() = 0;
  47. public:
  48. /*
  49. * Check if the thread is running.
  50. */
  51. bool isThreadRunning() const noexcept
  52. {
  53. #ifdef CARLA_OS_WIN
  54. return (fHandle.p != nullptr);
  55. #else
  56. return (fHandle != 0);
  57. #endif
  58. }
  59. /*
  60. * Check if the thread should exit.
  61. */
  62. bool shouldThreadExit() const noexcept
  63. {
  64. return fShouldExit;
  65. }
  66. /*
  67. * Start the thread.
  68. */
  69. void startThread() noexcept
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(),);
  72. const CarlaMutex::ScopedLocker sl(fLock);
  73. fShouldExit = false;
  74. pthread_t threadId;
  75. if (pthread_create(&threadId, nullptr, _entryPoint, this) == 0)
  76. {
  77. #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  78. if (fName.isNotEmpty())
  79. pthread_setname_np(threadId, fName);
  80. #endif
  81. pthread_detach(threadId);
  82. #ifdef CARLA_OS_WIN
  83. *(const_cast<pthread_t*>(&fHandle)) = threadId;
  84. #else
  85. fHandle = threadId;
  86. #endif
  87. // wait for thread to start
  88. fLock.lock();
  89. }
  90. }
  91. /*
  92. * Stop the thread.
  93. * In the 'timeOutMilliseconds':
  94. * = 0 -> no wait
  95. * > 0 -> wait timeout value
  96. * < 0 -> wait forever
  97. */
  98. bool stopThread(const int timeOutMilliseconds) noexcept
  99. {
  100. const CarlaMutex::ScopedLocker sl(fLock);
  101. if (isThreadRunning())
  102. {
  103. signalThreadShouldExit();
  104. if (timeOutMilliseconds != 0)
  105. {
  106. // Wait for the thread to stop
  107. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  108. while (isThreadRunning())
  109. {
  110. carla_msleep(2);
  111. if (timeOutCheck < 0)
  112. continue;
  113. if (timeOutCheck > 0)
  114. timeOutCheck -= 1;
  115. else
  116. break;
  117. }
  118. }
  119. if (isThreadRunning())
  120. {
  121. // should never happen!
  122. carla_stderr2("Carla assertion failure: \"! isRunning()\" in file %s, line %i", __FILE__, __LINE__);
  123. pthread_t threadId = *(const_cast<pthread_t*>(&fHandle));
  124. _init();
  125. try {
  126. pthread_cancel(threadId);
  127. } catch(...) {}
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. /*
  134. * Tell the thread to stop as soon as possible.
  135. */
  136. void signalThreadShouldExit() noexcept
  137. {
  138. fShouldExit = true;
  139. }
  140. private:
  141. CarlaMutex fLock; // Thread lock
  142. const CarlaString fName; // Thread name
  143. volatile pthread_t fHandle; // Handle for this thread
  144. volatile bool fShouldExit; // true if thread should exit
  145. void _init() noexcept
  146. {
  147. #ifdef CARLA_OS_WIN
  148. fHandle.p = nullptr;
  149. fHandle.x = 0;
  150. #else
  151. fHandle = 0;
  152. #endif
  153. }
  154. void _runEntryPoint() noexcept
  155. {
  156. // report ready
  157. fLock.unlock();
  158. try {
  159. run();
  160. } catch(...) {}
  161. // done
  162. _init();
  163. }
  164. static void* _entryPoint(void* userData) noexcept
  165. {
  166. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  167. return nullptr;
  168. }
  169. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  170. };
  171. // -----------------------------------------------------------------------
  172. #endif // CARLA_THREAD_HPP_INCLUDED