[FFmpeg-devel] [PATCH 1/4] avcodec/util: use a mutex instead of atomics in avcodec_register()

Michael Niedermayer michael at niedermayer.cc
Fri Jan 5 01:52:11 EET 2018


On Thu, Jan 04, 2018 at 03:19:01PM -0300, James Almer wrote:
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavcodec/utils.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index dfbfe98d63..61acf36c4b 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -26,7 +26,6 @@
>   */
>  
>  #include "config.h"
> -#include "libavutil/atomic.h"
>  #include "libavutil/attributes.h"
>  #include "libavutil/avassert.h"
>  #include "libavutil/avstring.h"
> @@ -95,7 +94,6 @@ void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
>  
>  /* encoder management */
>  static AVCodec *first_avcodec = NULL;
> -static AVCodec **last_avcodec = &first_avcodec;
>  
>  AVCodec *av_codec_next(const AVCodec *c)
>  {
> @@ -127,16 +125,21 @@ int av_codec_is_decoder(const AVCodec *codec)
>      return codec && (codec->decode || codec->receive_frame);
>  }
>  
> +static AVMutex codec_register_mutex = AV_MUTEX_INITIALIZER;
> +
>  av_cold void avcodec_register(AVCodec *codec)
>  {
>      AVCodec **p;
>      avcodec_init();
> -    p = last_avcodec;
> -    codec->next = NULL;
> +    ff_mutex_lock(&codec_register_mutex);
> +    p = &first_avcodec;
>  
> -    while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
> +    while (*p)
>          p = &(*p)->next;
> -    last_avcodec = &codec->next;
> +    *p          = codec;
> +    codec->next = NULL;
> +
> +    ff_mutex_unlock(&codec_register_mutex);

the code should keep track of the last entry of the list as previously and not
search through the list each time for the end, especially not under a mutex
this is not as negligible fast as you may think 
adding 1000 entries to a list like this will need ~ half millon iterations

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Concerning the gods, I have no means of knowing whether they exist or not
or of what sort they may be, because of the obscurity of the subject, and
the brevity of human life -- Protagoras
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20180105/c45804f7/attachment.sig>


More information about the ffmpeg-devel mailing list