From dc787ba51b2aa450c98d4e873de6817959af4be2 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 13 Nov 2020 14:04:42 +0100 Subject: [PATCH] qt: medialib: define a medialib thread pool The database queries, initiated by list models, must be executed asynchronously (on a thread different from the UI thread). When a list model is destroyed (from the UI thread), some database queries might still be executing. It would be incorrect for the list model to wait for them (it would block the UI thread), so they must be executed on an external thread pool. Since the queries themselves require the medialib to be executed, they must not outlive it (or the asynchronous code might crash). Therefore, the right scope for executing asynchronous queries is the MediaLib instance. Signed-off-by: Pierre Lamot --- modules/gui/qt/medialibrary/medialib.cpp | 3 +++ modules/gui/qt/medialibrary/medialib.hpp | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/modules/gui/qt/medialibrary/medialib.cpp b/modules/gui/qt/medialibrary/medialib.cpp index c0b06339a9..cdccf4b1b9 100644 --- a/modules/gui/qt/medialibrary/medialib.cpp +++ b/modules/gui/qt/medialibrary/medialib.cpp @@ -35,6 +35,9 @@ MediaLib::MediaLib(intf_thread_t *_intf, QObject *_parent) { m_event_cb.reset( vlc_ml_event_register_callback( m_ml, MediaLib::onMediaLibraryEvent, this ) ); + + /* https://xkcd.com/221/ */ + m_threadPool.setMaxThreadCount(4); } void MediaLib::addToPlaylist(const QString& mrl, const QStringList* options) diff --git a/modules/gui/qt/medialibrary/medialib.hpp b/modules/gui/qt/medialibrary/medialib.hpp index d7646ea999..2274c359cd 100644 --- a/modules/gui/qt/medialibrary/medialib.hpp +++ b/modules/gui/qt/medialibrary/medialib.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -65,6 +66,8 @@ public: vlc_medialibrary_t* vlcMl(); + QThreadPool &threadPool() { return m_threadPool; } + signals: void reloadStarted(); void reloadCompleted(); @@ -90,4 +93,5 @@ private: vlc_medialibrary_t* m_ml; std::unique_ptr> m_event_cb; + QThreadPool m_threadPool; };