DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit 8a2eb6cdb64913e884332e351afd255c87872a80
parent a415fc875a3bc8587524957ec88091153db25795
Author: falkTX <falktx@falktx.com>
Date:   Sat,  9 Jul 2022 19:16:36 +0100

Simplify wasm runner, now using emscripten_set_interval

Signed-off-by: falkTX <falktx@falktx.com>

Diffstat:
Mdistrho/extra/Runner.hpp | 51++++++++++++++++++++++++++++-----------------------
1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/distrho/extra/Runner.hpp b/distrho/extra/Runner.hpp @@ -53,11 +53,14 @@ protected: Runner(const char* const runnerName = nullptr) noexcept #ifndef DISTRHO_OS_WASM : fRunnerThread(this, runnerName), + fTimeInterval(0) #else : fRunnerName(runnerName), - fShouldStop(false), + fIntervalId(0) #endif - fTimeInterval(0) {} + { + } + /* * Destructor. @@ -84,7 +87,7 @@ protected: #ifndef DISTRHO_OS_WASM return fRunnerThread.shouldThreadExit(); #else - return fShouldStop; + return fIntervalId == 0; #endif } @@ -99,8 +102,7 @@ public: #ifndef DISTRHO_OS_WASM return fRunnerThread.isThreadRunning(); #else - fShouldStop = false; - return true; + return fIntervalId != 0; #endif } @@ -109,12 +111,13 @@ public: */ bool startRunner(const uint timeIntervalMilliseconds = 0) noexcept { - fTimeInterval = timeIntervalMilliseconds; #ifndef DISTRHO_OS_WASM + DISTRHO_SAFE_ASSERT_RETURN(!fRunnerThread.isThreadRunning(), false); + fTimeInterval = timeIntervalMilliseconds; return fRunnerThread.startThread(); #else - fShouldStop = false; - emscripten_set_timeout_loop(_entryPoint, timeIntervalMilliseconds, this); + DISTRHO_SAFE_ASSERT_RETURN(fIntervalId == 0, false); + fIntervalId = emscripten_set_interval(_entryPoint, timeIntervalMilliseconds, this); return true; #endif } @@ -128,7 +131,7 @@ public: #ifndef DISTRHO_OS_WASM return fRunnerThread.stopThread(-1); #else - fShouldStop = true; + signalRunnerShouldStop(); return true; #endif } @@ -141,7 +144,11 @@ public: #ifndef DISTRHO_OS_WASM fRunnerThread.signalThreadShouldExit(); #else - fShouldStop = true; + if (fIntervalId != 0) + { + emscripten_clear_interval(fIntervalId); + fIntervalId = 0; + } #endif } @@ -200,35 +207,33 @@ private: } } } fRunnerThread; + + uint fTimeInterval; #else const String fRunnerName; - volatile bool fShouldStop; + long fIntervalId; - EM_BOOL _runEntryPoint() noexcept + void _runEntryPoint() noexcept { - if (fShouldStop) - return EM_FALSE; - bool stillRunning = false; try { stillRunning = run(); } catch(...) {} - if (stillRunning && !fShouldStop) - return EM_TRUE; - - return EM_FALSE; + if (fIntervalId != 0 && !stillRunning) + { + emscripten_clear_interval(fIntervalId); + fIntervalId = 0; + } } - static EM_BOOL _entryPoint(double, void* const userData) noexcept + static void _entryPoint(void* const userData) noexcept { - return static_cast<Runner*>(userData)->_runEntryPoint(); + static_cast<Runner*>(userData)->_runEntryPoint(); } #endif - uint fTimeInterval; - DISTRHO_DECLARE_NON_COPYABLE(Runner) };