[vlc-commits] adaptive: downloader: C++ify mutex usage

Alexandre Janniaux git at videolan.org
Fri Sep 18 21:01:54 CEST 2020


vlc | branch: master | Alexandre Janniaux <ajanni at videolabs.io> | Wed Sep 16 00:43:47 2020 +0200| [f42f0d58d5b34f104f8e3659d78704b5242e18c4] | committer: Alexandre Janniaux

adaptive: downloader: C++ify mutex usage

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f42f0d58d5b34f104f8e3659d78704b5242e18c4
---

 modules/demux/adaptive/http/Downloader.cpp | 28 ++++++++++++++--------------
 modules/demux/adaptive/http/Downloader.hpp |  6 ++++--
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/modules/demux/adaptive/http/Downloader.cpp b/modules/demux/adaptive/http/Downloader.cpp
index e62d1002bf..74a19b28de 100644
--- a/modules/demux/adaptive/http/Downloader.cpp
+++ b/modules/demux/adaptive/http/Downloader.cpp
@@ -31,8 +31,6 @@ using namespace adaptive::http;
 
 Downloader::Downloader()
 {
-    vlc_mutex_init(&lock);
-    vlc_cond_init(&waitcond);
     killed = false;
     thread_handle_valid = false;
 }
@@ -51,29 +49,32 @@ bool Downloader::start()
 
 Downloader::~Downloader()
 {
-    vlc_mutex_lock( &lock );
-    killed = true;
-    vlc_cond_signal(&waitcond);
-    vlc_mutex_unlock( &lock );
+    kill();
 
     if(thread_handle_valid)
         vlc_join(thread_handle, NULL);
 }
+
+void Downloader::kill()
+{
+    vlc::threads::mutex_locker locker {lock};
+    killed = true;
+    wait_cond.signal();
+}
+
 void Downloader::schedule(HTTPChunkBufferedSource *source)
 {
-    vlc_mutex_lock(&lock);
+    vlc::threads::mutex_locker locker {lock};
     source->hold();
     chunks.push_back(source);
-    vlc_cond_signal(&waitcond);
-    vlc_mutex_unlock(&lock);
+    wait_cond.signal();
 }
 
 void Downloader::cancel(HTTPChunkBufferedSource *source)
 {
-    vlc_mutex_lock(&lock);
+    vlc::threads::mutex_locker locker {lock};
     source->release();
     chunks.remove(source);
-    vlc_mutex_unlock(&lock);
 }
 
 void * Downloader::downloaderThread(void *opaque)
@@ -93,11 +94,11 @@ void Downloader::DownloadSource(HTTPChunkBufferedSource *source)
 
 void Downloader::Run()
 {
-    vlc_mutex_lock(&lock);
+    vlc::threads::mutex_locker locker {lock};
     while(1)
     {
         while(chunks.empty() && !killed)
-            vlc_cond_wait(&waitcond, &lock);
+            wait_cond.wait(lock);
 
         if(killed)
             break;
@@ -113,5 +114,4 @@ void Downloader::Run()
             }
         }
     }
-    vlc_mutex_unlock(&lock);
 }
diff --git a/modules/demux/adaptive/http/Downloader.hpp b/modules/demux/adaptive/http/Downloader.hpp
index 1d6cc5767d..e4ee0d5b93 100644
--- a/modules/demux/adaptive/http/Downloader.hpp
+++ b/modules/demux/adaptive/http/Downloader.hpp
@@ -23,6 +23,7 @@
 #include "Chunk.h"
 
 #include <vlc_common.h>
+#include <vlc_cxx_helpers.hpp>
 #include <list>
 
 namespace adaptive
@@ -44,9 +45,10 @@ namespace adaptive
                 static void * downloaderThread(void *);
                 void Run();
                 void DownloadSource(HTTPChunkBufferedSource *);
+                void kill();
                 vlc_thread_t thread_handle;
-                vlc_mutex_t  lock;
-                vlc_cond_t   waitcond;
+                vlc::threads::mutex lock;
+                vlc::threads::condition_variable wait_cond;
                 bool         thread_handle_valid;
                 bool         killed;
                 std::list<HTTPChunkBufferedSource *> chunks;



More information about the vlc-commits mailing list