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.

227 lines
5.7KB

  1. // SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #ifndef CARLA_RUNNER_HPP_INCLUDED
  4. #define CARLA_RUNNER_HPP_INCLUDED
  5. #include "CarlaUtils.hpp"
  6. #ifndef CARLA_OS_WASM
  7. # include "CarlaThread.hpp"
  8. #else
  9. # include "distrho/extra/String.hpp"
  10. # include <emscripten/html5.h>
  11. #endif
  12. // -------------------------------------------------------------------------------------------------------------------
  13. // CarlaRunner class
  14. /**
  15. This is a handy class that handles "idle" time in either background or main thread,
  16. whichever is more suitable to the target platform.
  17. Typically background threads on desktop platforms, main thread on web.
  18. A single function is expected to be implemented by subclasses,
  19. which directly allows it to stop the runner by returning false.
  20. You can use it for quick operations that do not need to be handled in the main thread if possible.
  21. The target is to spread out execution over many runs, instead of spending a lot of time on a single task.
  22. */
  23. class CarlaRunner
  24. {
  25. protected:
  26. /*
  27. * Constructor.
  28. */
  29. CarlaRunner(const char* const runnerName = nullptr) noexcept
  30. #ifndef CARLA_OS_WASM
  31. : fRunnerThread(this, runnerName),
  32. fTimeInterval(0)
  33. #else
  34. : fRunnerName(runnerName),
  35. fIntervalId(0)
  36. #endif
  37. {
  38. }
  39. /*
  40. * Destructor.
  41. */
  42. virtual ~CarlaRunner() /*noexcept*/
  43. {
  44. CARLA_SAFE_ASSERT(! isRunnerActive());
  45. stopRunner();
  46. }
  47. /*
  48. * Virtual function to be implemented by the subclass.
  49. * Return true to keep running, false to stop execution.
  50. */
  51. virtual bool run() = 0;
  52. /*
  53. * Check if the runner should stop.
  54. * To be called from inside the runner to know if a stop request has been made.
  55. */
  56. bool shouldRunnerStop() const noexcept
  57. {
  58. #ifndef CARLA_OS_WASM
  59. return fRunnerThread.shouldThreadExit();
  60. #else
  61. return fIntervalId == 0;
  62. #endif
  63. }
  64. // ---------------------------------------------------------------------------------------------------------------
  65. public:
  66. /*
  67. * Check if the runner is active.
  68. */
  69. bool isRunnerActive() noexcept
  70. {
  71. #ifndef CARLA_OS_WASM
  72. return fRunnerThread.isThreadRunning();
  73. #else
  74. return fIntervalId != 0;
  75. #endif
  76. }
  77. /*
  78. * Start the thread.
  79. */
  80. bool startRunner(const uint timeIntervalMilliseconds = 0) noexcept
  81. {
  82. #ifndef CARLA_OS_WASM
  83. CARLA_SAFE_ASSERT_RETURN(!fRunnerThread.isThreadRunning(), false);
  84. fTimeInterval = timeIntervalMilliseconds;
  85. return fRunnerThread.startThread();
  86. #else
  87. CARLA_SAFE_ASSERT_RETURN(fIntervalId == 0, false);
  88. fIntervalId = emscripten_set_interval(_entryPoint, timeIntervalMilliseconds, this);
  89. return true;
  90. #endif
  91. }
  92. /*
  93. * Stop the runner.
  94. * This will signal the runner to stop if active, and wait until it finishes.
  95. */
  96. bool stopRunner() noexcept
  97. {
  98. #ifndef CARLA_OS_WASM
  99. return fRunnerThread.stopThread(-1);
  100. #else
  101. signalRunnerShouldStop();
  102. return true;
  103. #endif
  104. }
  105. /*
  106. * Tell the runner to stop as soon as possible.
  107. */
  108. void signalRunnerShouldStop() noexcept
  109. {
  110. #ifndef CARLA_OS_WASM
  111. fRunnerThread.signalThreadShouldExit();
  112. #else
  113. if (fIntervalId != 0)
  114. {
  115. emscripten_clear_interval(fIntervalId);
  116. fIntervalId = 0;
  117. }
  118. #endif
  119. }
  120. // ---------------------------------------------------------------------------------------------------------------
  121. /*
  122. * Returns the name of the runner.
  123. * This is the name that gets set in the constructor.
  124. */
  125. const String& getRunnerName() const noexcept
  126. {
  127. #ifndef CARLA_OS_WASM
  128. return fRunnerThread.getThreadName();
  129. #else
  130. return fRunnerName;
  131. #endif
  132. }
  133. // ---------------------------------------------------------------------------------------------------------------
  134. private:
  135. #ifndef CARLA_OS_WASM
  136. class RunnerThread : public CarlaThread
  137. {
  138. CarlaRunner* const runner;
  139. public:
  140. RunnerThread(CarlaRunner* const r, const char* const rn)
  141. : CarlaThread(rn),
  142. runner(r) {}
  143. protected:
  144. void run() override
  145. {
  146. const uint timeInterval = runner->fTimeInterval;
  147. while (!shouldThreadExit())
  148. {
  149. bool stillRunning = false;
  150. try {
  151. stillRunning = runner->run();
  152. } catch(...) {}
  153. if (stillRunning && !shouldThreadExit())
  154. {
  155. if (timeInterval != 0)
  156. d_msleep(timeInterval);
  157. // FIXME
  158. // pthread_yield();
  159. continue;
  160. }
  161. break;
  162. }
  163. }
  164. CARLA_DECLARE_NON_COPYABLE(RunnerThread)
  165. } fRunnerThread;
  166. uint fTimeInterval;
  167. #else
  168. const String fRunnerName;
  169. long fIntervalId;
  170. void _runEntryPoint() noexcept
  171. {
  172. bool stillRunning = false;
  173. try {
  174. stillRunning = run();
  175. } catch(...) {}
  176. if (fIntervalId != 0 && !stillRunning)
  177. {
  178. emscripten_clear_interval(fIntervalId);
  179. fIntervalId = 0;
  180. }
  181. }
  182. static void _entryPoint(void* const userData) noexcept
  183. {
  184. static_cast<CarlaRunner*>(userData)->_runEntryPoint();
  185. }
  186. #endif
  187. CARLA_DECLARE_NON_COPYABLE(CarlaRunner)
  188. };
  189. // -------------------------------------------------------------------------------------------------------------------
  190. #endif // CARLA_RUNNER_HPP_INCLUDED