From 396f5edb07ecfa5d87ab29733bb03713696912c5 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 23 May 2021 22:46:19 +0100 Subject: [PATCH] Make Application::quit() thread-safe Signed-off-by: falkTX --- dgl/src/ApplicationPrivateData.cpp | 13 +++++++++++++ dgl/src/ApplicationPrivateData.hpp | 3 +++ 2 files changed, 16 insertions(+) diff --git a/dgl/src/ApplicationPrivateData.cpp b/dgl/src/ApplicationPrivateData.cpp index a23a8062..07cb21fb 100644 --- a/dgl/src/ApplicationPrivateData.cpp +++ b/dgl/src/ApplicationPrivateData.cpp @@ -32,6 +32,7 @@ Application::PrivateData::PrivateData(const bool standalone) standalone ? PUGL_WORLD_THREADS : 0x0)), isStandalone(standalone), isQuitting(false), + isQuittingInNextCycle(false), isStarting(true), visibleWindows(0), windows(), @@ -92,6 +93,12 @@ void Application::PrivateData::oneWindowClosed() noexcept void Application::PrivateData::idle(const uint timeoutInMs) { + if (isQuittingInNextCycle) + { + quit(); + isQuittingInNextCycle = false; + } + if (world != nullptr) { const double timeoutInSeconds = timeoutInMs != 0 @@ -112,6 +119,12 @@ void Application::PrivateData::quit() { DISTRHO_SAFE_ASSERT_RETURN(isStandalone,); + if (! isQuittingInNextCycle) + { + isQuittingInNextCycle = true; + return; + } + isQuitting = true; #ifndef DPF_TEST_APPLICATION_CPP diff --git a/dgl/src/ApplicationPrivateData.hpp b/dgl/src/ApplicationPrivateData.hpp index a636902c..4841c969 100644 --- a/dgl/src/ApplicationPrivateData.hpp +++ b/dgl/src/ApplicationPrivateData.hpp @@ -39,6 +39,9 @@ struct Application::PrivateData { /** Whether the applicating is about to quit, or already stopped. Defaults to false. */ bool isQuitting; + /** Helper for safely close everything from main thread. */ + bool isQuittingInNextCycle; + /** Whether the applicating is starting up, that is, no windows have been made visible yet. Defaults to true. */ bool isStarting;