diff options
author | Gary E. Miller <gem@rellim.com> | 2016-01-04 16:33:57 -0800 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2016-01-04 16:39:12 -0800 |
commit | ade44708148b3034b19e1e8226bb10ac42f6289c (patch) | |
tree | 52bd800cf57c9dcd4e4d5047adc1ed608114460f /compiler.h | |
parent | 469f00e0ba8792e82278f40e69057cda8be1c193 (diff) | |
download | gpsd-ade44708148b3034b19e1e8226bb10ac42f6289c.tar.gz |
Clean up stdatomic.h confusion with C++
For atomics:
C98 and above needs <stdatomic.h>
C++11 and above needs <atomic>
C++98 and C++03 have no atomics
So this leaves a little hole where we have no atomics for C++98 and
C++03. So C++ clients need tp be careful...
Diffstat (limited to 'compiler.h')
-rw-r--r-- | compiler.h | 28 |
1 files changed, 21 insertions, 7 deletions
@@ -66,9 +66,15 @@ } while (0) #ifdef HAVE_STDATOMIC_H -#if !defined(__COVERITY__) && !defined(__cplusplus) -#include <stdatomic.h> -#endif /* __COVERITY__ || __cplusplus */ +#if !defined(__COVERITY__) +#if !defined(__cplusplus) + #include <stdatomic.h> +#elif __cplusplus >= 201103L + /* C++ before C++11 can not handle stdatomic.h or atomic */ + /* atomic is just C++ for stdatomic.h */ + #include <atomic> +#endif /* __cplusplus */ +#endif /* __COVERITY__ */ #endif /* HAVE_STDATOMIC_H */ #ifdef HAVE_OSATOMIC_H @@ -78,15 +84,23 @@ static inline void memory_barrier(void) /* prevent instruction reordering across any call to this function */ { -#ifdef HAVE_STDATOMIC_H -#ifndef __COVERITY__ +#ifdef __COVERITY__ + /* do nothing */ +#elif defined(__cplusplus) + /* we are C++ */ + #if __cplusplus >= 201103L + /* C++11 and later has atomics, earlier do not */ + atomic_thread_fence(memory_order_seq_cst); + #endif +#elif defined HAVE_STDATOMIC_H + /* we are C and atomics are in C98 and newer */ atomic_thread_fence(memory_order_seq_cst); -#endif /* __COVERITY__ */ #elif defined(HAVE_OSATOMIC_H) + /* do it the OS X way */ OSMemoryBarrier(); #elif defined(__GNUC__) asm volatile ("" : : : "memory"); -#endif /* HAVE_STD_ATOMIC_H */ +#endif } #endif /* _GPSD_COMPILER_H_ */ |