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.

CarlaRunner.hpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Carla Runner
  3. * Copyright (C) 2022-2023 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef CARLA_RUNNER_HPP_INCLUDED
  17. #define CARLA_RUNNER_HPP_INCLUDED
  18. #include "CarlaUtils.hpp"
  19. #ifndef CARLA_OS_WASM
  20. # include "CarlaThread.hpp"
  21. #else
  22. # include "CarlaString.hpp"
  23. # include <emscripten/html5.h>
  24. #endif
  25. // -------------------------------------------------------------------------------------------------------------------
  26. // CarlaRunner class
  27. /**
  28. This is a handy class that handles "idle" time in either background or main thread,
  29. whichever is more suitable to the target platform.
  30. Typically background threads on desktop platforms, main thread on web.
  31. A single function is expected to be implemented by subclasses,
  32. which directly allows it to stop the runner by returning false.
  33. You can use it for quick operations that do not need to be handled in the main thread if possible.
  34. The target is to spread out execution over many runs, instead of spending a lot of time on a single task.
  35. */
  36. class CarlaRunner
  37. {
  38. protected:
  39. /*
  40. * Constructor.
  41. */
  42. CarlaRunner(const char* const runnerName = nullptr) noexcept
  43. #ifndef CARLA_OS_WASM
  44. : fRunnerThread(this, runnerName),
  45. fTimeInterval(0)
  46. #else
  47. : fRunnerName(runnerName),
  48. fIntervalId(0)
  49. #endif
  50. {
  51. }
  52. /*
  53. * Destructor.
  54. */
  55. virtual ~CarlaRunner() /*noexcept*/
  56. {
  57. CARLA_SAFE_ASSERT(! isRunnerActive());
  58. stopRunner();
  59. }
  60. /*
  61. * Virtual function to be implemented by the subclass.
  62. * Return true to keep running, false to stop execution.
  63. */
  64. virtual bool run() = 0;
  65. /*
  66. * Check if the runner should stop.
  67. * To be called from inside the runner to know if a stop request has been made.
  68. */
  69. bool shouldRunnerStop() const noexcept
  70. {
  71. #ifndef CARLA_OS_WASM
  72. return fRunnerThread.shouldThreadExit();
  73. #else
  74. return fIntervalId == 0;
  75. #endif
  76. }
  77. // ---------------------------------------------------------------------------------------------------------------
  78. public:
  79. /*
  80. * Check if the runner is active.
  81. */
  82. bool isRunnerActive() noexcept
  83. {
  84. #ifndef CARLA_OS_WASM
  85. return fRunnerThread.isThreadRunning();
  86. #else
  87. return fIntervalId != 0;
  88. #endif
  89. }
  90. /*
  91. * Start the thread.
  92. */
  93. bool startRunner(const uint timeIntervalMilliseconds = 0) noexcept
  94. {
  95. #ifndef CARLA_OS_WASM
  96. CARLA_SAFE_ASSERT_RETURN(!fRunnerThread.isThreadRunning(), false);
  97. fTimeInterval = timeIntervalMilliseconds;
  98. return fRunnerThread.startThread();
  99. #else
  100. CARLA_SAFE_ASSERT_RETURN(fIntervalId == 0, false);
  101. fIntervalId = emscripten_set_interval(_entryPoint, timeIntervalMilliseconds, this);
  102. return true;
  103. #endif
  104. }
  105. /*
  106. * Stop the runner.
  107. * This will signal the runner to stop if active, and wait until it finishes.
  108. */
  109. bool stopRunner() noexcept
  110. {
  111. #ifndef CARLA_OS_WASM
  112. return fRunnerThread.stopThread(-1);
  113. #else
  114. signalRunnerShouldStop();
  115. return true;
  116. #endif
  117. }
  118. /*
  119. * Tell the runner to stop as soon as possible.
  120. */
  121. void signalRunnerShouldStop() noexcept
  122. {
  123. #ifndef CARLA_OS_WASM
  124. fRunnerThread.signalThreadShouldExit();
  125. #else
  126. if (fIntervalId != 0)
  127. {
  128. emscripten_clear_interval(fIntervalId);
  129. fIntervalId = 0;
  130. }
  131. #endif
  132. }
  133. // ---------------------------------------------------------------------------------------------------------------
  134. /*
  135. * Returns the name of the runner.
  136. * This is the name that gets set in the constructor.
  137. */
  138. const CarlaString& getRunnerName() const noexcept
  139. {
  140. #ifndef CARLA_OS_WASM
  141. return fRunnerThread.getThreadName();
  142. #else
  143. return fRunnerName;
  144. #endif
  145. }
  146. // ---------------------------------------------------------------------------------------------------------------
  147. private:
  148. #ifndef CARLA_OS_WASM
  149. class RunnerThread : public CarlaThread
  150. {
  151. CarlaRunner* const runner;
  152. public:
  153. RunnerThread(CarlaRunner* const r, const char* const rn)
  154. : CarlaThread(rn),
  155. runner(r) {}
  156. protected:
  157. void run() override
  158. {
  159. const uint timeInterval = runner->fTimeInterval;
  160. while (!shouldThreadExit())
  161. {
  162. bool stillRunning = false;
  163. try {
  164. stillRunning = runner->run();
  165. } catch(...) {}
  166. if (stillRunning && !shouldThreadExit())
  167. {
  168. if (timeInterval != 0)
  169. carla_msleep(timeInterval);
  170. // FIXME
  171. // pthread_yield();
  172. continue;
  173. }
  174. break;
  175. }
  176. }
  177. CARLA_DECLARE_NON_COPYABLE(RunnerThread)
  178. } fRunnerThread;
  179. uint fTimeInterval;
  180. #else
  181. const CarlaString fRunnerName;
  182. long fIntervalId;
  183. void _runEntryPoint() noexcept
  184. {
  185. bool stillRunning = false;
  186. try {
  187. stillRunning = run();
  188. } catch(...) {}
  189. if (fIntervalId != 0 && !stillRunning)
  190. {
  191. emscripten_clear_interval(fIntervalId);
  192. fIntervalId = 0;
  193. }
  194. }
  195. static void _entryPoint(void* const userData) noexcept
  196. {
  197. static_cast<CarlaRunner*>(userData)->_runEntryPoint();
  198. }
  199. #endif
  200. CARLA_DECLARE_NON_COPYABLE(CarlaRunner)
  201. };
  202. // -------------------------------------------------------------------------------------------------------------------
  203. #endif // CARLA_RUNNER_HPP_INCLUDED