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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Carla Thread
  3. * Copyright (C) 2013 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. if (pthread_create(const_cast<pthread_t*>(&fHandle), nullptr, _entryPoint, this) == 0)
  75. {
  76. #if (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
  77. if (fName.isNotEmpty())
  78. pthread_setname_np(fHandle, fName.getBuffer());
  79. #endif
  80. pthread_detach(fHandle);
  81. // wait for thread to start
  82. fLock.lock();
  83. }
  84. }
  85. /*
  86. * Stop the thread.
  87. * In the 'timeOutMilliseconds':
  88. * =0 -> no wait
  89. * >0 -> wait timeout value
  90. * <0 -> wait forever
  91. */
  92. bool stop(const int timeOutMilliseconds)
  93. {
  94. const CarlaMutex::ScopedLocker sl(fLock);
  95. if (isRunning())
  96. {
  97. signalShouldExit();
  98. if (timeOutMilliseconds != 0)
  99. {
  100. // Wait for the thread to stop
  101. int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
  102. while (isRunning())
  103. {
  104. carla_msleep(2);
  105. if (timeOutCheck < 0)
  106. continue;
  107. if (timeOutCheck > 0)
  108. timeOutCheck -= 1;
  109. else
  110. break;
  111. }
  112. }
  113. if (isRunning())
  114. {
  115. // should never happen!
  116. carla_stderr2("Carla assertion failure: \"! isRunning()\" in file %s, line %i", __FILE__, __LINE__);
  117. pthread_cancel(fHandle);
  118. _init();
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. /*
  125. * Tell the thread to stop as soon as possible.
  126. */
  127. void signalShouldExit() noexcept
  128. {
  129. fShouldExit = true;
  130. }
  131. private:
  132. CarlaMutex fLock; // Thread lock
  133. const CarlaString fName; // Thread name
  134. volatile pthread_t fHandle; // Handle for this thread
  135. volatile bool fShouldExit; // true if thread should exit
  136. void _init() noexcept
  137. {
  138. #ifdef CARLA_OS_WIN32
  139. fHandle.p = nullptr;
  140. fHandle.x = 0;
  141. #else
  142. fHandle = 0;
  143. #endif
  144. }
  145. void _runEntryPoint()
  146. {
  147. // report ready
  148. fLock.unlock();
  149. run();
  150. // done
  151. _init();
  152. }
  153. static void* _entryPoint(void* userData)
  154. {
  155. static_cast<CarlaThread*>(userData)->_runEntryPoint();
  156. return nullptr;
  157. }
  158. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  159. };
  160. // -----------------------------------------------------------------------
  161. #endif // CARLA_THREAD_HPP_INCLUDED