From 75ec5ee87d04ff001492531f9b474c7447ffd731 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Tue, 1 Mar 2022 18:20:40 +0100 Subject: [PATCH] Terminate Peak file threads at exit --- libs/ardour/ardour/source_factory.h | 5 +++++ libs/ardour/globals.cc | 1 + libs/ardour/source_factory.cc | 33 +++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libs/ardour/ardour/source_factory.h b/libs/ardour/ardour/source_factory.h index 473b2c4851..5d4f69e648 100644 --- a/libs/ardour/ardour/source_factory.h +++ b/libs/ardour/ardour/source_factory.h @@ -24,6 +24,7 @@ #include #include +#include "pbd/pthread_utils.h" #include "ardour/source.h" class XMLNode; @@ -38,6 +39,7 @@ class LIBARDOUR_API SourceFactory { public: static void init (); + static void terminate (); static PBD::Signal1> SourceCreated; @@ -51,6 +53,9 @@ public: static Glib::Threads::Cond PeaksToBuild; static Glib::Threads::Mutex peak_building_lock; + static bool peak_thread_run; + static std::vector peak_thread_pool; + static std::list> files_with_peaks; static int peak_work_queue_length (); diff --git a/libs/ardour/globals.cc b/libs/ardour/globals.cc index 2a49c0b7a6..d3220876d4 100644 --- a/libs/ardour/globals.cc +++ b/libs/ardour/globals.cc @@ -739,6 +739,7 @@ ARDOUR::cleanup () delete TriggerBox::worker; Analyser::terminate (); + SourceFactory::terminate (); release_dma_latency (); config_connection.disconnect (); diff --git a/libs/ardour/source_factory.cc b/libs/ardour/source_factory.cc index 3944eda0c8..20eed4a516 100644 --- a/libs/ardour/source_factory.cc +++ b/libs/ardour/source_factory.cc @@ -26,7 +26,6 @@ #include "pbd/convert.h" #include "pbd/error.h" -#include "pbd/pthread_utils.h" #include "ardour/audio_playlist_source.h" #include "ardour/audioplaylist.h" @@ -55,6 +54,8 @@ PBD::Signal1> SourceFactory::SourceCreated; Glib::Threads::Cond SourceFactory::PeaksToBuild; Glib::Threads::Mutex SourceFactory::peak_building_lock; std::list> SourceFactory::files_with_peaks; +std::vector SourceFactory::peak_thread_pool; +bool SourceFactory::peak_thread_run = false; static int active_threads = 0; @@ -68,17 +69,24 @@ peak_thread_work () SourceFactory::peak_building_lock.lock (); wait: - if (SourceFactory::files_with_peaks.empty ()) { + if (SourceFactory::files_with_peaks.empty () && SourceFactory::peak_thread_run) { SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock); } + if (!SourceFactory::peak_thread_run) { + SourceFactory::peak_building_lock.unlock (); + return; + } + if (SourceFactory::files_with_peaks.empty ()) { goto wait; } boost::shared_ptr as (SourceFactory::files_with_peaks.front ().lock ()); SourceFactory::files_with_peaks.pop_front (); - ++active_threads; + if (as) { + ++active_threads; + } SourceFactory::peak_building_lock.unlock (); if (!as) { @@ -103,8 +111,25 @@ SourceFactory::peak_work_queue_length () void SourceFactory::init () { + if (peak_thread_run) { + return; + } + peak_thread_run = true; for (int n = 0; n < 2; ++n) { - PBD::Thread::create (&peak_thread_work); + peak_thread_pool.push_back (PBD::Thread::create (&peak_thread_work)); + } +} + +void +SourceFactory::terminate () +{ + if (!peak_thread_run) { + return; + } + peak_thread_run = false; + PeaksToBuild.broadcast (); + for (auto& t : peak_thread_pool) { + t->join (); } }