qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 1/3] atomics: Test __STDC_VERSION__ for C11 comp


From: Paolo Bonzini
Subject: Re: [Qemu-devel] [PATCH 1/3] atomics: Test __STDC_VERSION__ for C11 compat
Date: Mon, 29 Aug 2016 12:11:17 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0


On 24/08/2016 22:44, Pranith Kumar wrote:
> This patch tries to do the Right Thing™ to test for C11 features,
> which is to test __STDC_VERSION__.

Compilers can provide atomics even for older standards, for example:

$ echo '__STDC_VERSION__' | gcc -E -x c - -std=gnu99 | tail -1
199901L
$ echo '__ATOMIC_RELAXED' | gcc -E -x c - -std=gnu99 | tail -1
0

Which is why the patch is not using __STDC_VERSION__.

Unfortunately, the C standard does not define a feature macro for
atomics; it provides __STDC_NO_ATOMICS__ but it is defined backwards.

Thanks,

Paolo

> Signed-off-by: Pranith Kumar <address@hidden>
> ---
>  include/qemu/atomic.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
> index 43b0645..d313e6b 100644
> --- a/include/qemu/atomic.h
> +++ b/include/qemu/atomic.h
> @@ -60,7 +60,7 @@
>          (unsigned short)1,                                                   
>       \
>        (expr)+0))))))
>  
> -#ifdef __ATOMIC_RELAXED
> +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
>  /* For C11 atomic ops */
>  
>  /* Manual memory barriers
> @@ -210,7 +210,7 @@
>  #define atomic_and(ptr, n) ((void) __atomic_fetch_and(ptr, n, 
> __ATOMIC_SEQ_CST))
>  #define atomic_or(ptr, n)  ((void) __atomic_fetch_or(ptr, n, 
> __ATOMIC_SEQ_CST))
>  
> -#else /* __ATOMIC_RELAXED */
> +#else /* __STDC_VERSION__ */
>  
>  /*
>   * We use GCC builtin if it's available, as that can use mfence on
> @@ -405,5 +405,5 @@
>  #define atomic_and(ptr, n)     ((void) __sync_fetch_and_and(ptr, n))
>  #define atomic_or(ptr, n)      ((void) __sync_fetch_and_or(ptr, n))
>  
> -#endif /* __ATOMIC_RELAXED */
> +#endif /* __STDC_VERSION__ */
>  #endif /* QEMU_ATOMIC_H */
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]