[vlc-devel] [PATCH 1/2] adaptive: chunk: C++ify mutex usage

Steve Lhomme robux4 at ycbcr.xyz
Thu Sep 17 07:53:30 CEST 2020


On 2020-09-16 1:04, Alexandre Janniaux wrote:
> ---
>   modules/demux/adaptive/http/Chunk.cpp | 69 +++++++++++++--------------
>   modules/demux/adaptive/http/Chunk.h   |  5 +-
>   2 files changed, 36 insertions(+), 38 deletions(-)
> 
> diff --git a/modules/demux/adaptive/http/Chunk.cpp b/modules/demux/adaptive/http/Chunk.cpp
> index 2af20cd6b2..c6682c2466 100644
> --- a/modules/demux/adaptive/http/Chunk.cpp
> +++ b/modules/demux/adaptive/http/Chunk.cpp
> @@ -36,6 +36,7 @@
>   #include <algorithm>
>   
>   using namespace adaptive::http;
> +using vlc::threads::mutex_locker;
>   
>   AbstractChunkSource::AbstractChunkSource()
>   {
> @@ -144,7 +145,6 @@ HTTPChunkSource::HTTPChunkSource(const std::string& url, AbstractConnectionManag
>       connManager  (manager),
>       consumed     (0)
>   {
> -    vlc_mutex_init(&lock);
>       prepared = false;
>       eof = false;
>       sourceid = id;
> @@ -161,7 +161,7 @@ HTTPChunkSource::~HTTPChunkSource()
>   
>   bool HTTPChunkSource::init(const std::string &url)
>   {
> -    vlc_mutex_locker locker(&lock);
> +    mutex_locker locker {lock};
>       params = ConnectionParams(url);
>       params.setUseAccess(usesAccess());
>   
> @@ -176,7 +176,7 @@ bool HTTPChunkSource::init(const std::string &url)
>   
>   bool HTTPChunkSource::hasMoreData() const
>   {
> -    vlc_mutex_locker locker(&lock);
> +    mutex_locker locker {lock};
>       if(eof)
>           return false;
>       else if(contentLength)
> @@ -186,7 +186,7 @@ bool HTTPChunkSource::hasMoreData() const
>   
>   block_t * HTTPChunkSource::read(size_t readsize)
>   {
> -    vlc_mutex_locker locker(&lock);
> +    mutex_locker locker {lock};
>       if(!prepare())
>       {
>           eof = true;
> @@ -233,7 +233,7 @@ block_t * HTTPChunkSource::read(size_t readsize)
>   
>   std::string HTTPChunkSource::getContentType() const
>   {
> -    vlc_mutex_locker locker(&lock);
> +    mutex_locker locker {lock};
>       if(connection)
>           return connection->getContentType();
>       else
> @@ -298,7 +298,6 @@ HTTPChunkBufferedSource::HTTPChunkBufferedSource(const std::string& url, Abstrac
>       pp_tail    (&p_head),
>       buffered     (0)
>   {
> -    vlc_cond_init(&avail);
>       done = false;
>       eof = false;
>       held = false;
> @@ -310,10 +309,10 @@ HTTPChunkBufferedSource::~HTTPChunkBufferedSource()
>       /* cancel ourself if in queue */
>       connManager->cancel(this);
>   
> -    vlc_mutex_lock(&lock);
> +    mutex_locker locker {lock};
>       done = true;
>       if(held) /* wait release if not in queue but currently downloaded */
> -        vlc_cond_wait(&avail, &lock);
> +        avail.wait(lock);
>   
>       if(p_head)
>       {
> @@ -322,48 +321,46 @@ HTTPChunkBufferedSource::~HTTPChunkBufferedSource()
>           pp_tail = &p_head;
>       }
>       buffered = 0;
> -    vlc_mutex_unlock(&lock);
>   }
>   
>   bool HTTPChunkBufferedSource::isDone() const
>   {
> -    vlc_mutex_locker locker( &lock );
> +    mutex_locker locker {lock};
>       return done;
>   }
>   
>   void HTTPChunkBufferedSource::hold()
>   {
> -    vlc_mutex_locker locker( &lock );
> +    mutex_locker locker {lock};
>       held = true;
>   }
>   
>   void HTTPChunkBufferedSource::release()
>   {
> -    vlc_mutex_locker locker( &lock );
> +    mutex_locker locker {lock};
>       held = false;
> -    vlc_cond_signal(&avail);
> +    avail.signal();
>   }
>   
>   void HTTPChunkBufferedSource::bufferize(size_t readsize)
>   {
> -    vlc_mutex_lock(&lock);
> -    if(!prepare())
>       {

You might as well remove this block level since you change the whole block.

> -        done = true;
> -        eof = true;
> -        vlc_cond_signal(&avail);
> -        vlc_mutex_unlock(&lock);
> -        return;
> +        mutex_locker locker {lock};
> +        if(!prepare())
> +        {
> +            done = true;
> +            eof = true;
> +            avail.signal();
> +            return;
> +        }
> +
> +        if(readsize < HTTPChunkSource::CHUNK_SIZE)
> +            readsize = HTTPChunkSource::CHUNK_SIZE;
> +
> +        if(contentLength && readsize > contentLength - buffered)
> +            readsize = contentLength - buffered;
>       }
>   
> -    if(readsize < HTTPChunkSource::CHUNK_SIZE)
> -        readsize = HTTPChunkSource::CHUNK_SIZE;
> -
> -    if(contentLength && readsize > contentLength - buffered)
> -        readsize = contentLength - buffered;
> -
> -    vlc_mutex_unlock(&lock);
> -
>       block_t *p_block = block_Alloc(readsize);
>       if(!p_block)
>       {


More information about the vlc-devel mailing list