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

Marvin Scholz epirat07 at gmail.com
Mon Mar 30 10:30:39 CEST 2020


Hi,

I agree with Rémi that this is much more readable and a bit easier to
understand than the pthread_once stuff.

On 30 Mar 2020, at 10:27, Rémi Denis-Courmont wrote:

> Hi,
>
> It's a very peculiar and disagreeable opinion that the existing code 
> is more readable. This patch follows the same logic as normal single 
> thread lazy init would, which is more readable to me than the 
> pthread_once() (anti)pattern.
>
> As for why, it's much faster than pthread_once since it removes 
> unnecessary barriers, and it removes the last (non-Linux) vlc_once 
> usage.
>
> Le 30 mars 2020 10:37:01 GMT+03:00, Thomas Guillem <thomas at gllm.fr> a 
> écrit :
>> 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
>> _______________________________________________
>> vlc-devel mailing list
>> To unsubscribe or modify your subscription options:
>> https://mailman.videolan.org/listinfo/vlc-devel
>
> -- 
> Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez 
> excuser ma brièveté._______________________________________________
> 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