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.

231 lines
6.1KB

  1. /*
  2. * Carla Runner
  3. * Copyright (C) 2022 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/emscripten.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. #else
  46. : fRunnerName(runnerName),
  47. fShouldStop(false),
  48. #endif
  49. fTimeInterval(0) {}
  50. /*
  51. * Destructor.
  52. */
  53. virtual ~CarlaRunner() /*noexcept*/
  54. {
  55. CARLA_SAFE_ASSERT(! isRunnerActive());
  56. stopRunner();
  57. }
  58. /*
  59. * Virtual function to be implemented by the subclass.
  60. * Return true to keep running, false to stop execution.
  61. */
  62. virtual bool run() = 0;
  63. /*
  64. * Check if the runner should stop.
  65. * To be called from inside the runner to know if a stop request has been made.
  66. */
  67. bool shouldRunnerStop() const noexcept
  68. {
  69. #ifndef CARLA_OS_WASM
  70. return fRunnerThread.shouldThreadExit();
  71. #else
  72. return fShouldStop;
  73. #endif
  74. }
  75. // ---------------------------------------------------------------------------------------------------------------
  76. public:
  77. /*
  78. * Check if the runner is active.
  79. */
  80. bool isRunnerActive() noexcept
  81. {
  82. #ifndef CARLA_OS_WASM
  83. return fRunnerThread.isThreadRunning();
  84. #else
  85. fShouldStop = false;
  86. return true;
  87. #endif
  88. }
  89. /*
  90. * Start the thread.
  91. */
  92. bool startRunner(const uint timeIntervalMilliseconds = 0) noexcept
  93. {
  94. fTimeInterval = timeIntervalMilliseconds;
  95. #ifndef CARLA_OS_WASM
  96. return fRunnerThread.startThread();
  97. #else
  98. fShouldStop = false;
  99. emscripten_async_call(_entryPoint, this, timeIntervalMilliseconds);
  100. return true;
  101. #endif
  102. }
  103. /*
  104. * Stop the runner.
  105. * This will signal the runner to stop if active, and wait until it finishes.
  106. */
  107. bool stopRunner() noexcept
  108. {
  109. #ifndef CARLA_OS_WASM
  110. return fRunnerThread.stopThread(-1);
  111. #else
  112. fShouldStop = true;
  113. return true;
  114. #endif
  115. }
  116. /*
  117. * Tell the runner to stop as soon as possible.
  118. */
  119. void signalRunnerShouldStop() noexcept
  120. {
  121. #ifndef CARLA_OS_WASM
  122. fRunnerThread.signalThreadShouldExit();
  123. #else
  124. fShouldStop = true;
  125. #endif
  126. }
  127. // ---------------------------------------------------------------------------------------------------------------
  128. /*
  129. * Returns the name of the runner.
  130. * This is the name that gets set in the constructor.
  131. */
  132. const CarlaString& getRunnerName() const noexcept
  133. {
  134. #ifndef CARLA_OS_WASM
  135. return fRunnerThread.getThreadName();
  136. #else
  137. return fRunnerName;
  138. #endif
  139. }
  140. // ---------------------------------------------------------------------------------------------------------------
  141. private:
  142. #ifndef CARLA_OS_WASM
  143. class RunnerThread : public CarlaThread
  144. {
  145. CarlaRunner* const runner;
  146. public:
  147. RunnerThread(CarlaRunner* const r, const char* const rn)
  148. : CarlaThread(rn),
  149. runner(r) {}
  150. protected:
  151. void run() override
  152. {
  153. const uint timeInterval = runner->fTimeInterval;
  154. while (!shouldThreadExit())
  155. {
  156. bool stillRunning = false;
  157. try {
  158. stillRunning = runner->run();
  159. } catch(...) {}
  160. if (stillRunning && !shouldThreadExit())
  161. {
  162. if (timeInterval != 0)
  163. carla_msleep(timeInterval);
  164. pthread_yield();
  165. continue;
  166. }
  167. break;
  168. }
  169. }
  170. } fRunnerThread;
  171. #else
  172. const CarlaString fRunnerName;
  173. volatile bool fShouldStop;
  174. void _runEntryPoint() noexcept
  175. {
  176. if (fShouldStop)
  177. return;
  178. bool stillRunning = false;
  179. try {
  180. stillRunning = run();
  181. } catch(...) {}
  182. if (stillRunning && !fShouldStop)
  183. emscripten_async_call(_entryPoint, this, fTimeInterval);
  184. }
  185. static void _entryPoint(void* const userData) noexcept
  186. {
  187. static_cast<CarlaRunner*>(userData)->_runEntryPoint();
  188. }
  189. #endif
  190. uint fTimeInterval;
  191. CARLA_DECLARE_NON_COPYABLE(CarlaRunner)
  192. };
  193. // -------------------------------------------------------------------------------------------------------------------
  194. #endif // CARLA_RUNNER_HPP_INCLUDED