[vlc-devel] [PATCH 09/13] qt: medialib: define a medialib thread pool

Romain Vimont rom1v at videolabs.io
Mon Nov 23 17:30:23 CET 2020


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.
---
 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 <QMetaObject>
 #include <QMetaMethod>
 #include <QQmlEngine>
+#include <QThreadPool>
 
 #include <memory>
 
@@ -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<vlc_ml_event_callback_t, std::function<void(vlc_ml_event_callback_t*)>> m_event_cb;
 
+    QThreadPool m_threadPool;
 };
-- 
2.29.2



More information about the vlc-devel mailing list