Skip to content

Commit

Permalink
Add windows atomic (#304)
Browse files Browse the repository at this point in the history
* 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 e68987f.

* build: remove print

* fix: remove obsolete compile symbol

* fix: revert type casting
  • Loading branch information
jean-roland authored Jan 8, 2024
1 parent 203bae5 commit 4f4ccec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions include/zenoh-pico/collections/pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#include <stdbool.h>
#include <stdint.h>

#if ZENOH_C_STANDARD != 99 && !defined(ZENOH_NO_STDATOMIC)
#if ZENOH_C_STANDARD != 99

#ifndef __cplusplus
#include <stdatomic.h>
#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
Expand All @@ -31,26 +31,26 @@
#define _z_memory_order_relaxed memory_order_relaxed
#else
#include <atomic>
#define z_atomic(X) std::atomic<X>
#define _z_atomic(X) std::atomic<X>
#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); \
Expand Down Expand Up @@ -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); \
} \
} \
} \
Expand Down Expand Up @@ -150,6 +150,6 @@
} \
return dropped; \
}
#endif
#endif // ZENOH_C_STANDARD != 99

#endif /* ZENOH_PICO_COLLECTIONS_POINTER_H */

0 comments on commit 4f4ccec

Please sign in to comment.