From f34fd933bd1db86bd48cffdacb05416c12313c2c Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 7 Mar 2022 21:14:48 +0100 Subject: [PATCH 1/8] add C11 and builtin atomic support --- CMakeLists.txt | 1 + include/re_atomic.h | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 include/re_atomic.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d1377116..3c9570b47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,7 @@ if(CMAKE_USE_PTHREADS_INIT) endif() add_definitions( + -HAVE_ATOMIC -DHAVE_INET6 -DHAVE_SELECT ) diff --git a/include/re_atomic.h b/include/re_atomic.h new file mode 100644 index 000000000..3656c07f3 --- /dev/null +++ b/include/re_atomic.h @@ -0,0 +1,19 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef HAVE_ATOMIC +#include +#elif __has_extension(c_atomic) +#define __CLANG_ATOMICS +#elif __GNUC_PREREQ__(4, 7) +#define __GNUC_ATOMICS +#elif defined(__GNUC__) +#define __SYNC_ATOMICS +#else +#error "Your compiler does not support atomics" +#endif + +#ifdef __cplusplus +} +#endif From 4f380b6cdf3e6d876b1dfe8e904cbba3fc960f3b Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 7 Mar 2022 21:17:27 +0100 Subject: [PATCH 2/8] cmake: fix HAVE_ATOMIC --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c9570b47..6193da497 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ if(CMAKE_USE_PTHREADS_INIT) endif() add_definitions( - -HAVE_ATOMIC + -DHAVE_ATOMIC -DHAVE_INET6 -DHAVE_SELECT ) From 9e09a46e16e250fe7935e5042bb1c8a449178af0 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 7 Mar 2022 21:35:04 +0100 Subject: [PATCH 3/8] fix pedantic and better builtin checks --- include/re_atomic.h | 8 ++++++-- mk/re.mk | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/re_atomic.h b/include/re_atomic.h index 3656c07f3..9573fb55e 100644 --- a/include/re_atomic.h +++ b/include/re_atomic.h @@ -4,10 +4,14 @@ extern "C" { #ifdef HAVE_ATOMIC #include -#elif __has_extension(c_atomic) +#elif defined(__has_extension) +#if __has_extension(c_atomic) #define __CLANG_ATOMICS -#elif __GNUC_PREREQ__(4, 7) +#endif +#elif defined(__GNUC_PREREQ__) +#if __GNUC_PREREQ__(4, 7) #define __GNUC_ATOMICS +#endif #elif defined(__GNUC__) #define __SYNC_ATOMICS #else diff --git a/mk/re.mk b/mk/re.mk index 9b5f92687..0528ae73d 100644 --- a/mk/re.mk +++ b/mk/re.mk @@ -339,10 +339,9 @@ CFLAGS += -std=c11 HAVE_ATOMIC := 1 endif -CFLAGS += -pedantic - ifneq ($(HAVE_ATOMIC),) CFLAGS += -DHAVE_ATOMIC +CFLAGS += -pedantic endif From 4c9f306b0cd77b335c0981ab870a39242a0b9e55 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 7 Mar 2022 21:41:11 +0100 Subject: [PATCH 4/8] cleanup --- include/re_atomic.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/include/re_atomic.h b/include/re_atomic.h index 9573fb55e..6e8a89521 100644 --- a/include/re_atomic.h +++ b/include/re_atomic.h @@ -4,14 +4,8 @@ extern "C" { #ifdef HAVE_ATOMIC #include -#elif defined(__has_extension) -#if __has_extension(c_atomic) +#elif defined(__clang__) #define __CLANG_ATOMICS -#endif -#elif defined(__GNUC_PREREQ__) -#if __GNUC_PREREQ__(4, 7) -#define __GNUC_ATOMICS -#endif #elif defined(__GNUC__) #define __SYNC_ATOMICS #else From c4f515e04b98bf8768d90d910cc0431aadeebcfc Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 7 Mar 2022 22:38:41 +0100 Subject: [PATCH 5/8] add description and fix format --- include/re_atomic.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/re_atomic.h b/include/re_atomic.h index 6e8a89521..7a57da5ea 100644 --- a/include/re_atomic.h +++ b/include/re_atomic.h @@ -1,3 +1,9 @@ +/** + * @file re_atomic.h Atomic support + * + * Copyright (C) 2022 Sebastian Reimers + */ + #ifdef __cplusplus extern "C" { #endif @@ -5,9 +11,9 @@ extern "C" { #ifdef HAVE_ATOMIC #include #elif defined(__clang__) -#define __CLANG_ATOMICS +#define __CLANG_ATOMICS #elif defined(__GNUC__) -#define __SYNC_ATOMICS +#define __SYNC_ATOMICS #else #error "Your compiler does not support atomics" #endif From a685a87d3925279d9160bfa4074fb2f62435cc7e Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Tue, 8 Mar 2022 07:27:27 +0100 Subject: [PATCH 6/8] allow multiple includes --- include/re_atomic.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/re_atomic.h b/include/re_atomic.h index 7a57da5ea..b77309c2b 100644 --- a/include/re_atomic.h +++ b/include/re_atomic.h @@ -4,6 +4,9 @@ * Copyright (C) 2022 Sebastian Reimers */ +#ifndef RE_H_ATOMIC__ +#define RE_H_ATOMIC__ + #ifdef __cplusplus extern "C" { #endif @@ -21,3 +24,5 @@ extern "C" { #ifdef __cplusplus } #endif + +#endif /* RE_H_ATOMIC__ */ From 4d17992c13cf86f968116ea85be77666cb57c1c3 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Tue, 8 Mar 2022 10:16:41 +0100 Subject: [PATCH 7/8] add gcc version check (gcc >= 4.9) --- include/re_atomic.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/re_atomic.h b/include/re_atomic.h index b77309c2b..4e3216559 100644 --- a/include/re_atomic.h +++ b/include/re_atomic.h @@ -13,10 +13,17 @@ extern "C" { #ifdef HAVE_ATOMIC #include + #elif defined(__clang__) #define __CLANG_ATOMICS + #elif defined(__GNUC__) +#if __GNUC_PREREQ(4, 9) #define __SYNC_ATOMICS +#else +#error "Atomic requires gcc >= 4.9" +#endif /* __GNUC_PREREQ */ + #else #error "Your compiler does not support atomics" #endif From cc0f585e90f7eef7bda36394c5751072f49c133c Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Tue, 8 Mar 2022 10:25:27 +0100 Subject: [PATCH 8/8] add some comments --- include/re_atomic.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/re_atomic.h b/include/re_atomic.h index 4e3216559..a73cc76da 100644 --- a/include/re_atomic.h +++ b/include/re_atomic.h @@ -11,9 +11,11 @@ extern "C" { #endif +/* C11 */ #ifdef HAVE_ATOMIC #include +/* C99 */ #elif defined(__clang__) #define __CLANG_ATOMICS @@ -26,7 +28,7 @@ extern "C" { #else #error "Your compiler does not support atomics" -#endif +#endif /* HAVE_ATOMIC */ #ifdef __cplusplus }