From 4f4ccec53d275d3ff3f4ebc2ccbf8191a6c951c9 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Mon, 8 Jan 2024 14:13:06 +0100 Subject: [PATCH] Add windows atomic (#304) * fix: set private z_atomic * feat: add refcount type (wip) * fix: clang-format * feat: supposedly, windows has c11 atomics now * fix: add explicit type casting * Revert "feat: add refcount type (wip)" This reverts commit e68987f11bf768b9c5d860214e146ad0264a0596. * build: remove print * fix: remove obsolete compile symbol * fix: revert type casting --- CMakeLists.txt | 6 ++++-- include/zenoh-pico/collections/pointer.h | 16 ++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2398e80bf..1f734f072 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,7 +90,6 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") add_definition(ZENOH_WINDOWS) add_definition(_CRT_SECURE_NO_WARNINGS) - add_definition(ZENOH_NO_STDATOMIC) elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") if(WITH_ZEPHYR) add_definition(ZENOH_ZEPHYR) @@ -161,6 +160,8 @@ if(CMAKE_BUILD_TYPE MATCHES "DEBUG") if(UNIX) add_compile_options(-c -Wall -Wextra -Werror -Wshadow -Wpedantic -Wunused -Wstrict-prototypes -pipe -g -O0) elseif(MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /std:c11 /experimental:c11atomics") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /experimental:c11atomics") add_compile_options(/W4 /WX /Od) elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes -pipe -g -O0) @@ -169,7 +170,8 @@ elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") if(UNIX) add_compile_options(-pipe -O3) elseif(MSVC) - # add_compile_options(/O2) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /std:c11 /experimental:c11atomics") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /experimental:c11atomics") elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") add_compile_options(-pipe -O3) endif() diff --git a/include/zenoh-pico/collections/pointer.h b/include/zenoh-pico/collections/pointer.h index 8e1fd54e0..9dc4c4859 100644 --- a/include/zenoh-pico/collections/pointer.h +++ b/include/zenoh-pico/collections/pointer.h @@ -18,11 +18,11 @@ #include #include -#if ZENOH_C_STANDARD != 99 && !defined(ZENOH_NO_STDATOMIC) +#if ZENOH_C_STANDARD != 99 #ifndef __cplusplus #include -#define z_atomic(X) _Atomic X +#define _z_atomic(X) _Atomic X #define _z_atomic_store_explicit atomic_store_explicit #define _z_atomic_fetch_add_explicit atomic_fetch_add_explicit #define _z_atomic_fetch_sub_explicit atomic_fetch_sub_explicit @@ -31,26 +31,26 @@ #define _z_memory_order_relaxed memory_order_relaxed #else #include -#define z_atomic(X) std::atomic +#define _z_atomic(X) std::atomic #define _z_atomic_store_explicit std::atomic_store_explicit #define _z_atomic_fetch_add_explicit std::atomic_fetch_add_explicit #define _z_atomic_fetch_sub_explicit std::atomic_fetch_sub_explicit #define _z_memory_order_acquire std::memory_order_acquire #define _z_memory_order_release std::memory_order_release #define _z_memory_order_relaxed std::memory_order_relaxed -#endif +#endif // __cplusplus /*------------------ Internal Array Macros ------------------*/ #define _Z_POINTER_DEFINE(name, type) \ typedef struct { \ type##_t *ptr; \ - z_atomic(unsigned int) * _cnt; \ + _z_atomic(unsigned int) * _cnt; \ } name##_sptr_t; \ static inline name##_sptr_t name##_sptr_new(type##_t val) { \ name##_sptr_t p; \ p.ptr = (type##_t *)z_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ - p._cnt = (z_atomic(unsigned int) *)z_malloc(sizeof(z_atomic(unsigned int) *)); \ + p._cnt = (_z_atomic(unsigned int) *)z_malloc(sizeof(_z_atomic(unsigned int) *)); \ if (p._cnt != NULL) { \ *p.ptr = val; \ _z_atomic_store_explicit(p._cnt, 1, _z_memory_order_relaxed); \ @@ -89,7 +89,7 @@ if (p->ptr != NULL) { \ type##_clear(p->ptr); \ z_free(p->ptr); \ - z_free(p->_cnt); \ + z_free((void *)p->_cnt); \ } \ } \ } \ @@ -150,6 +150,6 @@ } \ return dropped; \ } -#endif +#endif // ZENOH_C_STANDARD != 99 #endif /* ZENOH_PICO_COLLECTIONS_POINTER_H */