From ade44708148b3034b19e1e8226bb10ac42f6289c Mon Sep 17 00:00:00 2001 From: "Gary E. Miller" Date: Mon, 4 Jan 2016 16:33:57 -0800 Subject: Clean up stdatomic.h confusion with C++ For atomics: C98 and above needs C++11 and above needs 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... --- compiler.h | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/compiler.h b/compiler.h index 2f2a8edb..7d664b18 100644 --- a/compiler.h +++ b/compiler.h @@ -66,9 +66,15 @@ } while (0) #ifdef HAVE_STDATOMIC_H -#if !defined(__COVERITY__) && !defined(__cplusplus) -#include -#endif /* __COVERITY__ || __cplusplus */ +#if !defined(__COVERITY__) +#if !defined(__cplusplus) + #include +#elif __cplusplus >= 201103L + /* C++ before C++11 can not handle stdatomic.h or atomic */ + /* atomic is just C++ for stdatomic.h */ + #include +#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_ */ -- cgit v1.2.1