From 54573e11d44f4b9d5152f74be8bc380a8c4d63d9 Mon Sep 17 00:00:00 2001 From: Brian Heim Date: Wed, 18 Dec 2019 23:04:39 -0600 Subject: [PATCH] SC: limit number of running engines --- src/SuperColliderEngine.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/SuperColliderEngine.cpp b/src/SuperColliderEngine.cpp index b286a68..c0b6043 100644 --- a/src/SuperColliderEngine.cpp +++ b/src/SuperColliderEngine.cpp @@ -79,11 +79,23 @@ private: class SuperColliderEngine final : public ScriptEngine { public: - ~SuperColliderEngine() noexcept { _clientThread.join(); } + ~SuperColliderEngine() noexcept { + // Only join if client was started in the first place. + if (_clientThread.joinable()) + _clientThread.join(); + engineRunning = false; + } std::string getEngineName() override { return "SuperCollider"; } int run(const std::string& path, const std::string& script) override { + if (engineRunning) { + display("Only one SuperCollider engine may run at once"); + return 1; + } + + engineRunning = true; + if (!_clientThread.joinable()) { _clientThread = std::thread([this, script]() { _client.reset(new SC_VcvPrototypeClient(this)); @@ -110,6 +122,9 @@ public: } private: + // Used to limit the number of running SC instances to 1. + static bool engineRunning; + bool waitingOnClient() const noexcept { return !_clientRunning; } bool clientHasError() const noexcept { return !_client->isOk(); } void finishClientLoading() noexcept { _clientRunning = true; } @@ -119,6 +134,8 @@ private: std::atomic_bool _clientRunning{false}; // set to true when client is ready to process data }; +bool SuperColliderEngine::engineRunning = false; + SC_VcvPrototypeClient::SC_VcvPrototypeClient(SuperColliderEngine* engine) : SC_LanguageClient("SC VCV-Prototype client") , _engine(engine)