[vlc-devel] [PATCH 4/4] cpu: remove initialization lock

Thomas Guillem thomas at gllm.fr
Mon Mar 30 09:37:01 CEST 2020


Hello,

On Sun, Mar 29, 2020, at 22:43, RĂ©mi Denis-Courmont wrote:
> In the unlikely event that multiple threads want to check the CPU
> flags concurrently, simply compute them as many times.
> ---
>  src/misc/cpu.c | 20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/src/misc/cpu.c b/src/misc/cpu.c
> index 7f3a89a3cb..7053a391e7 100644
> --- a/src/misc/cpu.c
> +++ b/src/misc/cpu.c
> @@ -29,6 +29,8 @@
>  # include "config.h"
>  #endif
>  
> +#include <stdatomic.h>
> +
>  #include <vlc_common.h>
>  #include <vlc_cpu.h>
>  #include <vlc_memstream.h>
> @@ -247,18 +249,18 @@ out:
>      return i_capabilities;
>  }
>  
> -static uint32_t cpu_flags;
> -
> -static void vlc_CPU_init(void)
> -{
> -    cpu_flags = vlc_CPU_raw();
> -}
> +static atomic_uint cpu_flags = ATOMIC_VAR_INIT(-1);
>  
>  VLC_WEAK unsigned vlc_CPU(void)
>  {
> -    static vlc_once_t once = VLC_STATIC_ONCE;
> -    vlc_once(&once, vlc_CPU_init);
> -    return cpu_flags;
> +    unsigned flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);
> +
> +    if (unlikely(flags == -1U)) {
> +        flags = vlc_CPU_raw();
> +        atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed);
> +    }
> +
> +    return flags;

I don't understand why we should need this optimization. pthread_once will very likely use an atomic to do the check.
Furthermore, the previous code is more readable for me.

>  }
>  
>  void vlc_CPU_dump (vlc_object_t *obj)
> -- 
> 2.26.0
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list