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.

233 lines
6.1KB

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