[FFmpeg-devel] [PATCH 1/4] avcodec/allcodecs: make avcodec_register_all thread safe

wm4 nfxjfg at googlemail.com
Tue Mar 7 08:44:43 EET 2017


On Tue,  7 Mar 2017 02:47:36 +0700
Muhammad Faiz <mfcc64 at gmail.com> wrote:

> Signed-off-by: Muhammad Faiz <mfcc64 at gmail.com>
> ---
>  libavcodec/allcodecs.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index eee322b..1d05fc1 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -24,6 +24,8 @@
>   * Provide registration of all codecs, parsers and bitstream filters for libavcodec.
>   */
>  
> +#include <stdatomic.h>
> +
>  #include "config.h"
>  #include "avcodec.h"
>  #include "version.h"
> @@ -60,11 +62,14 @@
>  
>  void avcodec_register_all(void)
>  {
> -    static int initialized;
> +    static atomic_int initialized;
> +    static atomic_int ready;
>  
> -    if (initialized)
> +    if (atomic_exchange_explicit(&initialized, 1, memory_order_relaxed)) {
> +        while (!atomic_load_explicit(&ready, memory_order_relaxed))
> +            ;
>          return;
> -    initialized = 1;
> +    }
>  
>      /* hardware accelerators */
>      REGISTER_HWACCEL(H263_VAAPI,        h263_vaapi);
> @@ -716,4 +721,6 @@ void avcodec_register_all(void)
>      REGISTER_PARSER(VP8,                vp8);
>      REGISTER_PARSER(VP9,                vp9);
>      REGISTER_PARSER(XMA,                xma);
> +
> +    atomic_store_explicit(&ready, 1, memory_order_relaxed);
>  }

avcodec_register() is already "safe" by attempting to do lock-free and
wait-free list insertion (not very convincing IMHO, but it's there).
Should that code be removed? Does anyone know why avcodec_register() is
even still public API? The same affects some other things.

Also, if you want to do it right, please use ff_thread_once() to do it
without busy waiting. It works like pthread_once(), which was invented
to avoid such constructions.


More information about the ffmpeg-devel mailing list