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

11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. if (fStarted && ! fFinished)
  144. return true;
  145. // take the change 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. }
  170. else
  171. {
  172. for (unsigned int i=0; i < timeout && ! fStarted; i++)
  173. carla_msleep(1);
  174. }
  175. }
  176. void waitForFinished()
  177. {
  178. waitForStarted();
  179. stop(0);
  180. }
  181. protected:
  182. virtual void run() = 0;
  183. private:
  184. bool fStarted;
  185. bool fFinished;
  186. void handleRoutine()
  187. {
  188. fStarted = true;
  189. run();
  190. fFinished = true;
  191. }
  192. #ifdef CPP11_THREAD
  193. std::thread* cthread;
  194. static void _cthreadRoutine(CarlaThread* const _this_)
  195. {
  196. _this_->handleRoutine();
  197. }
  198. #else
  199. pthread_t pthreadId;
  200. pthread_attr_t pthreadAttr;
  201. static void* _pthreadRoutine(void* const _this_)
  202. {
  203. ((CarlaThread*)_this_)->handleRoutine();
  204. pthread_exit(nullptr);
  205. return nullptr;
  206. }
  207. bool _isNull()
  208. {
  209. #ifdef CARLA_OS_WIN
  210. return (pthreadId.p == nullptr);
  211. #else
  212. return (pthreadId == 0);
  213. #endif
  214. }
  215. void _zero()
  216. {
  217. carla_zeroStruct<pthread_t>(pthreadId);
  218. }
  219. #endif
  220. CARLA_PREVENT_HEAP_ALLOCATION
  221. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
  222. };
  223. // -------------------------------------------------
  224. #endif // __CARLA_THREAD_HPP__