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.

273 lines
5.3KB

  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 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. if (fStarted && ! fFinished)
  144. return true;
  145. // take the chance to clear data
  146. #ifdef CPP11_THREAD
  147. if (cthread != nullptr)
  148. {
  149. cthread->join();
  150. delete cthread;
  151. cthread = nullptr;
  152. }
  153. #else
  154. if (! _isNull())
  155. {
  156. //pthread_join(pthreadId, nullptr);
  157. //_zero();
  158. }
  159. #endif
  160. return false;
  161. }
  162. void waitForStarted(const unsigned int timeout = 0) // ms
  163. {
  164. if (fStarted)
  165. return;
  166. if (timeout == 0)
  167. {
  168. while (! fStarted)
  169. carla_msleep(1);
  170. }
  171. else
  172. {
  173. for (unsigned int i=0; i < timeout && ! fStarted; i++)
  174. carla_msleep(1);
  175. }
  176. }
  177. void waitForFinished()
  178. {
  179. waitForStarted();
  180. stop(0);
  181. }
  182. protected:
  183. virtual void run() = 0;
  184. private:
  185. bool fStarted;
  186. bool fFinished;
  187. void handleRoutine()
  188. {
  189. fStarted = true;
  190. run();
  191. fFinished = true;
  192. }
  193. #ifdef CPP11_THREAD
  194. std::thread* cthread;
  195. static void _cthreadRoutine(CarlaThread* const _this_)
  196. {
  197. _this_->handleRoutine();
  198. }
  199. #else
  200. pthread_t pthreadId;
  201. pthread_attr_t pthreadAttr;
  202. static void* _pthreadRoutine(void* const _this_)
  203. {
  204. ((CarlaThread*)_this_)->handleRoutine();
  205. pthread_exit(nullptr);
  206. return nullptr;
  207. }
  208. bool _isNull()
  209. {
  210. #ifdef CARLA_OS_WIN
  211. return (pthreadId.p == nullptr);
  212. #else
  213. return (pthreadId == 0);
  214. #endif
  215. }
  216. void _zero()
  217. {
  218. carla_zeroStruct<pthread_t>(pthreadId);
  219. }
  220. #endif
  221. //CARLA_PREVENT_HEAP_ALLOCATION
  222. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  223. };
  224. // -------------------------------------------------
  225. #endif // __CARLA_THREAD_HPP__