[vlc-devel] [PATCH v2 4/5] medialib: improve starting

Romain Vimont rom1v at videolabs.io
Tue Jan 12 10:10:05 UTC 2021


Some controls request to "start" the medialibrary (start to discover
some folders) if not already started. The actual starting should be done
at most once.

The fact that it was already started relied on the number of entry
points, which can only be known by executing SQL queries. For
asynchronous controls, this caused the execution of SQL queries on the
UI thread for every control requesting the medialibrary to be "started".

To avoid this unnecessary performance issue, add a simple atomic flag.
---
 modules/misc/medialibrary/medialibrary.cpp | 4 ++++
 modules/misc/medialibrary/medialibrary.h   | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/modules/misc/medialibrary/medialibrary.cpp b/modules/misc/medialibrary/medialibrary.cpp
index ff675cf865..1d7a12547c 100644
--- a/modules/misc/medialibrary/medialibrary.cpp
+++ b/modules/misc/medialibrary/medialibrary.cpp
@@ -462,6 +462,10 @@ bool MediaLibrary::Start()
     if ( Init() == false )
         return false;
 
+    if ( m_started.test_and_set() )
+        /* The code below should be executed at most once */
+        return true;
+
     /*
      * If we already provided the medialib with some entry points, then we have
      * nothing left to do
diff --git a/modules/misc/medialibrary/medialibrary.h b/modules/misc/medialibrary/medialibrary.h
index 09c773bf59..8ba8d5c108 100644
--- a/modules/misc/medialibrary/medialibrary.h
+++ b/modules/misc/medialibrary/medialibrary.h
@@ -170,6 +170,8 @@ private:
     vlc::threads::mutex m_mutex;
     bool m_initialized = false; /* protected by m_mutex */
 
+    std::atomic_flag m_started = ATOMIC_FLAG_INIT;
+
     // IMediaLibraryCb interface
 public:
     virtual void onMediaAdded(std::vector<medialibrary::MediaPtr> media) override;
-- 
2.30.0



More information about the vlc-devel mailing list