DISTRHO Plugin Framework
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.

252 lines
6.7KB

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