[vlc-devel] [PATCH] Make mutexes mutable

Steve Lhomme robux4 at ycbcr.xyz
Tue Jan 9 08:23:35 CET 2018


Le 08/01/2018 à 21:45, Romain Vimont a écrit :
> Mutexes must be locked and unlocked even in C++ const methods. To avoid
> const casts, simply declare them mutable.

I thought about overloading vlc_mutex_locker with another constructor 
but your solution is much cleaner.

> ---
>   modules/demux/adaptive/Streams.cpp                 |  8 ++++----
>   modules/demux/adaptive/Streams.hpp                 |  2 +-
>   .../adaptive/logic/RateBasedAdaptationLogic.cpp    |  4 ++--
>   .../adaptive/logic/RateBasedAdaptationLogic.h      |  2 +-
>   modules/demux/adaptive/plumbing/CommandsQueue.cpp  | 24 +++++++++++-----------
>   modules/demux/adaptive/plumbing/CommandsQueue.hpp  |  2 +-
>   modules/demux/adaptive/plumbing/FakeESOut.cpp      | 16 +++++++--------
>   modules/demux/adaptive/plumbing/FakeESOut.hpp      |  2 +-
>   8 files changed, 30 insertions(+), 30 deletions(-)
>
> diff --git a/modules/demux/adaptive/Streams.cpp b/modules/demux/adaptive/Streams.cpp
> index ecb3efc68b..29ba11d70f 100644
> --- a/modules/demux/adaptive/Streams.cpp
> +++ b/modules/demux/adaptive/Streams.cpp
> @@ -146,9 +146,9 @@ void AbstractStream::setDescription(const std::string &desc)
>   
>   mtime_t AbstractStream::getPCR() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       mtime_t pcr = isDisabled() ? VLC_TS_INVALID : commandsqueue->getPCR();
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return pcr;
>   }
>   
> @@ -162,7 +162,7 @@ mtime_t AbstractStream::getMinAheadTime() const
>   mtime_t AbstractStream::getFirstDTS() const
>   {
>       mtime_t dts;
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       if(isDisabled())
>       {
>           dts = VLC_TS_INVALID;
> @@ -173,7 +173,7 @@ mtime_t AbstractStream::getFirstDTS() const
>           if(dts == VLC_TS_INVALID)
>               dts = commandsqueue->getPCR();
>       }
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return dts;
>   }
>   
> diff --git a/modules/demux/adaptive/Streams.hpp b/modules/demux/adaptive/Streams.hpp
> index b6c188d081..a56f57c0b0 100644
> --- a/modules/demux/adaptive/Streams.hpp
> +++ b/modules/demux/adaptive/Streams.hpp
> @@ -125,7 +125,7 @@ namespace adaptive
>           AbstractDemuxer *demuxer;
>           AbstractSourceStream *demuxersource;
>           FakeESOut *fakeesout; /* to intercept/proxy what is sent from demuxstream */
> -        vlc_mutex_t lock; /* lock for everything accessed by dequeuing */
> +        mutable vlc_mutex_t lock; /* lock for everything accessed by dequeuing */
>   
>       private:
>           buffering_status doBufferize(mtime_t, unsigned, unsigned);
> diff --git a/modules/demux/adaptive/logic/RateBasedAdaptationLogic.cpp b/modules/demux/adaptive/logic/RateBasedAdaptationLogic.cpp
> index e100330cd2..814de251b2 100644
> --- a/modules/demux/adaptive/logic/RateBasedAdaptationLogic.cpp
> +++ b/modules/demux/adaptive/logic/RateBasedAdaptationLogic.cpp
> @@ -58,9 +58,9 @@ BaseRepresentation *RateBasedAdaptationLogic::getNextRepresentation(BaseAdaptati
>       if(adaptSet == NULL)
>           return NULL;
>   
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       size_t availBps = currentBps + ((currep) ? currep->getBandwidth() : 0);
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       if(availBps > usedBps)
>           availBps -= usedBps;
>       else
> diff --git a/modules/demux/adaptive/logic/RateBasedAdaptationLogic.h b/modules/demux/adaptive/logic/RateBasedAdaptationLogic.h
> index b99d18cdf2..572316bafa 100644
> --- a/modules/demux/adaptive/logic/RateBasedAdaptationLogic.h
> +++ b/modules/demux/adaptive/logic/RateBasedAdaptationLogic.h
> @@ -54,7 +54,7 @@ namespace adaptive
>                   size_t                  dlsize;
>                   mtime_t                 dllength;
>   
> -                vlc_mutex_t             lock;
> +                mutable vlc_mutex_t     lock;
>           };
>   
>           class FixedRateAdaptationLogic : public AbstractAdaptationLogic
> diff --git a/modules/demux/adaptive/plumbing/CommandsQueue.cpp b/modules/demux/adaptive/plumbing/CommandsQueue.cpp
> index 850c94b60e..b773e01171 100644
> --- a/modules/demux/adaptive/plumbing/CommandsQueue.cpp
> +++ b/modules/demux/adaptive/plumbing/CommandsQueue.cpp
> @@ -403,9 +403,9 @@ void CommandsQueue::Abort( bool b_reset )
>   
>   bool CommandsQueue::isEmpty() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       bool b_empty = commands.empty() && incoming.empty();
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return b_empty;
>   }
>   
> @@ -425,9 +425,9 @@ void CommandsQueue::setDraining()
>   
>   bool CommandsQueue::isDraining() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       bool b = b_draining;
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return b;
>   }
>   
> @@ -444,9 +444,9 @@ void CommandsQueue::setEOF( bool b )
>   
>   bool CommandsQueue::isEOF() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       bool b = b_eof;
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return b;
>   }
>   
> @@ -458,16 +458,16 @@ mtime_t CommandsQueue::getDemuxedAmount() const
>   mtime_t CommandsQueue::getBufferingLevel() const
>   {
>       mtime_t i_buffer;
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       i_buffer = bufferinglevel;
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return i_buffer;
>   }
>   
>   mtime_t CommandsQueue::getFirstDTS() const
>   {
>       std::list<AbstractCommand *>::const_iterator it;
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       mtime_t i_firstdts = pcr;
>       for( it = commands.begin(); it != commands.end(); ++it )
>       {
> @@ -479,7 +479,7 @@ mtime_t CommandsQueue::getFirstDTS() const
>               break;
>           }
>       }
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return i_firstdts;
>   }
>   
> @@ -491,8 +491,8 @@ void CommandsQueue::LockedSetDraining()
>   
>   mtime_t CommandsQueue::getPCR() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       mtime_t i_pcr = pcr;
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return i_pcr;
>   }
> diff --git a/modules/demux/adaptive/plumbing/CommandsQueue.hpp b/modules/demux/adaptive/plumbing/CommandsQueue.hpp
> index fe5abbd497..17551bfdf2 100644
> --- a/modules/demux/adaptive/plumbing/CommandsQueue.hpp
> +++ b/modules/demux/adaptive/plumbing/CommandsQueue.hpp
> @@ -171,7 +171,7 @@ namespace adaptive
>   
>           private:
>               CommandsFactory *commandsFactory;
> -            vlc_mutex_t lock;
> +            mutable vlc_mutex_t lock;
>               void LockedCommit();
>               void LockedSetDraining();
>               std::list<AbstractCommand *> incoming;
> diff --git a/modules/demux/adaptive/plumbing/FakeESOut.cpp b/modules/demux/adaptive/plumbing/FakeESOut.cpp
> index bc1517ebc7..cbf90803f9 100644
> --- a/modules/demux/adaptive/plumbing/FakeESOut.cpp
> +++ b/modules/demux/adaptive/plumbing/FakeESOut.cpp
> @@ -155,9 +155,9 @@ void FakeESOut::createOrRecycleRealEsID( FakeESOutID *es_id )
>   
>   mtime_t FakeESOut::getTimestampOffset() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       mtime_t time = timestamps_offset;
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return time;
>   }
>   
> @@ -165,11 +165,11 @@ size_t FakeESOut::esCount() const
>   {
>       size_t i_count = 0;
>       std::list<FakeESOutID *>::const_iterator it;
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       for( it=fakeesidlist.begin(); it!=fakeesidlist.end(); ++it )
>           if( (*it)->realESID() )
>               i_count++;
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return i_count;
>   }
>   
> @@ -237,14 +237,14 @@ bool FakeESOut::hasSelectedEs() const
>   {
>       bool b_selected = false;
>       std::list<FakeESOutID *>::const_iterator it;
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       for( it=fakeesidlist.begin(); it!=fakeesidlist.end() && !b_selected; ++it )
>       {
>           FakeESOutID *esID = *it;
>           if( esID->realESID() )
>               es_out_Control( real_es_out, ES_OUT_GET_ES_STATE, esID->realESID(), &b_selected );
>       }
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return b_selected;
>   }
>   
> @@ -257,9 +257,9 @@ bool FakeESOut::decodersDrained()
>   
>   bool FakeESOut::restarting() const
>   {
> -    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_lock(&lock);
>       bool b = !recycle_candidates.empty();
> -    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
> +    vlc_mutex_unlock(&lock);
>       return b;
>   }
>   
> diff --git a/modules/demux/adaptive/plumbing/FakeESOut.hpp b/modules/demux/adaptive/plumbing/FakeESOut.hpp
> index 1c1d686918..d559928fd4 100644
> --- a/modules/demux/adaptive/plumbing/FakeESOut.hpp
> +++ b/modules/demux/adaptive/plumbing/FakeESOut.hpp
> @@ -67,7 +67,7 @@ namespace adaptive
>               static void esOutDestroy_Callback( es_out_t * );
>   
>           private:
> -            vlc_mutex_t lock;
> +            mutable vlc_mutex_t lock;
>               es_out_t *real_es_out;
>               FakeESOutID * createNewID( const es_format_t * );
>               ExtraFMTInfoInterface *extrainfo;



More information about the vlc-devel mailing list