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.

205 lines
4.6KB

  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)
  30. : fName(threadName),
  31. fShouldExit(false)
  32. {
  33. _init();
  34. }
  35. /*
  36. * Destructor.
  37. */
  38. virtual ~CarlaThread()
  39. {
  40. CARLA_SAFE_ASSERT(! isRunning());
  41. stop(-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 isRunning() const noexcept
  52. {
  53. #ifdef CARLA_OS_WIN32
  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 shouldExit() const noexcept
  63. {
  64. return fShouldExit;
  65. }
  66. /*
  67. * Start the thread.
  68. */
  69. void start()
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(! isRunning(),);
  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.getBuffer());
  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 stop(const int timeOutMilliseconds)
  99. {
  100. const CarlaMutex::ScopedLocker sl(fLock);
  101. if (isRunning())
  102. {
  103. signalShouldExit();
  104. if (timeOutMilliseconds != 0)
  105. {
  106. // Wait for the thread to stop
  107. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  108. while (isRunning())
  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 (isRunning())
  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. pthread_cancel(threadId);
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /*
  132. * Tell the thread to stop as soon as possible.
  133. */
  134. void signalShouldExit() noexcept
  135. {
  136. fShouldExit = true;
  137. }
  138. private:
  139. CarlaMutex fLock; // Thread lock
  140. const CarlaString fName; // Thread name
  141. volatile pthread_t fHandle; // Handle for this thread
  142. volatile bool fShouldExit; // true if thread should exit
  143. void _init() noexcept
  144. {
  145. #ifdef CARLA_OS_WIN32
  146. fHandle.p = nullptr;
  147. fHandle.x = 0;
  148. #else
  149. fHandle = 0;
  150. #endif
  151. }
  152. void _runEntryPoint()
  153. {
  154. // report ready
  155. fLock.unlock();
  156. run();
  157. // done
  158. _init();
  159. }
  160. static void* _entryPoint(void* userData)
  161. {
  162. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  163. return nullptr;
  164. }
  165. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  166. };
  167. // -----------------------------------------------------------------------
  168. #endif // CARLA_THREAD_HPP_INCLUDED