diff mbox

[1/3] atomics: Test __STDC_VERSION__ for C11 compat

Message ID 20160824204424.14041-1-bobby.prani@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Pranith Kumar Aug. 24, 2016, 8:44 p.m. UTC
This patch tries to do the Right Thing™ to test for C11 features,
which is to test __STDC_VERSION__.

Signed-off-by: Pranith Kumar <bobby.prani@gmail.com>
---
 include/qemu/atomic.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Paolo Bonzini Aug. 29, 2016, 10:11 a.m. UTC | #1
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 <bobby.prani@gmail.com>
> ---
>  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 */
>
diff mbox

Patch

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 */