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.

254 lines
4.9KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-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 GPL.txt file
  16. */
  17. #ifndef __CARLA_THREAD_HPP__
  18. #define __CARLA_THREAD_HPP__
  19. #include "CarlaJuceUtils.hpp"
  20. // #define CPP11_THREAD
  21. #ifdef CPP11_THREAD
  22. # include <thread>
  23. #else
  24. # include <pthread.h>
  25. #endif
  26. // -------------------------------------------------
  27. // CarlaThread class
  28. class CarlaThread
  29. {
  30. public:
  31. CarlaThread()
  32. : fStarted(false),
  33. fFinished(false)
  34. {
  35. #ifdef CPP11_THREAD
  36. cthread = nullptr;
  37. #else
  38. _zero();
  39. pthread_attr_init(&pthreadAttr);
  40. pthread_attr_setdetachstate(&pthreadAttr, PTHREAD_CREATE_JOINABLE);
  41. #endif
  42. }
  43. ~CarlaThread()
  44. {
  45. CARLA_ASSERT(! isRunning());
  46. if (isRunning())
  47. terminate();
  48. #ifdef CPP11_THREAD
  49. if (cthread != nullptr)
  50. {
  51. cthread->join();
  52. delete cthread;
  53. }
  54. #else
  55. if (! _isNull())
  56. pthread_join(pthreadId, nullptr);
  57. pthread_attr_destroy(&pthreadAttr);
  58. #endif
  59. }
  60. bool start()
  61. {
  62. CARLA_ASSERT(! isRunning());
  63. if (isRunning())
  64. return false;
  65. fStarted = false;
  66. fFinished = false;
  67. #ifdef CPP11_THREAD
  68. CARLA_ASSERT(cthread == nullptr);
  69. if (cthread != nullptr)
  70. return false;
  71. cthread = new std::thread(_cthreadRoutine, this);
  72. CARLA_ASSERT(cthread->joinable());
  73. return true;
  74. #else
  75. CARLA_ASSERT(_isNull());
  76. if (! _isNull())
  77. return false;
  78. return (pthread_create(&pthreadId, &pthreadAttr, _pthreadRoutine, this) == 0);
  79. #endif
  80. }
  81. bool stop(const unsigned int timeout = 0)
  82. {
  83. CARLA_ASSERT(isRunning());
  84. if (! isRunning())
  85. return true;
  86. #ifdef CPP11_THREAD
  87. if (cthread == nullptr)
  88. return true;
  89. #else
  90. if (_isNull())
  91. return true;
  92. #endif
  93. if (timeout == 0)
  94. {
  95. #ifdef CPP11_THREAD
  96. cthread->join();
  97. #else
  98. pthread_join(pthreadId, nullptr);
  99. #endif
  100. }
  101. else
  102. {
  103. for (unsigned int i=0; i < timeout && ! fFinished; i++)
  104. carla_msleep(1);
  105. }
  106. if (! fFinished)
  107. return false;
  108. #ifdef CPP11_THREAD
  109. delete cthread;
  110. cthread = nullptr;
  111. #else
  112. _zero();
  113. #endif
  114. return true;
  115. }
  116. void terminate()
  117. {
  118. CARLA_ASSERT(isRunning());
  119. if (fFinished)
  120. return;
  121. #ifdef CPP11_THREAD
  122. if (cthread == nullptr)
  123. return;
  124. #else
  125. if (_isNull())
  126. return;
  127. #endif
  128. #ifdef CPP11_THREAD
  129. cthread->detach();
  130. //cthread->join();
  131. delete cthread;
  132. cthread = nullptr;
  133. #else
  134. pthread_detach(pthreadId);
  135. //pthread_join(pthreadId, nullptr);
  136. pthread_cancel(pthreadId);
  137. _zero();
  138. #endif
  139. fFinished = true;
  140. }
  141. bool isRunning()
  142. {
  143. return (fStarted && ! fFinished);
  144. }
  145. void waitForStarted(const unsigned int timeout = 0) // ms
  146. {
  147. if (fStarted)
  148. return;
  149. if (timeout == 0)
  150. {
  151. while (! fStarted) {}
  152. }
  153. else
  154. {
  155. for (unsigned int i=0; i < timeout && ! fStarted; i++)
  156. carla_msleep(1);
  157. }
  158. }
  159. void waitForFinished()
  160. {
  161. waitForStarted();
  162. stop(0);
  163. }
  164. protected:
  165. virtual void run() = 0;
  166. private:
  167. bool fStarted;
  168. bool fFinished;
  169. void handleRoutine()
  170. {
  171. fStarted = true;
  172. run();
  173. fFinished = true;
  174. }
  175. #ifdef CPP11_THREAD
  176. std::thread* cthread;
  177. static void _cthreadRoutine(CarlaThread* const _this_)
  178. {
  179. _this_->handleRoutine();
  180. }
  181. #else
  182. pthread_t pthreadId;
  183. pthread_attr_t pthreadAttr;
  184. static void* _pthreadRoutine(void* const _this_)
  185. {
  186. ((CarlaThread*)_this_)->handleRoutine();
  187. pthread_exit(nullptr);
  188. return nullptr;
  189. }
  190. bool _isNull()
  191. {
  192. #ifdef CARLA_OS_WIN
  193. return (pthreadId.p == nullptr);
  194. #else
  195. return (pthreadId == 0);
  196. #endif
  197. }
  198. void _zero()
  199. {
  200. carla_zeroStruct<pthread_t>(pthreadId);
  201. }
  202. #endif
  203. CARLA_PREVENT_HEAP_ALLOCATION
  204. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  205. };
  206. // -------------------------------------------------
  207. #endif // __CARLA_THREAD_HPP__