From 33ab69055ce3c8c78d487452e7b82ce303251455 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 11 Aug 2021 12:29:39 +0800 Subject: [PATCH] arch,lib/seqlock: implement seqlock with C++ atomic if compiled with C++ because some functions declared by share the same names with those declared by , for instance `kill_dependency()` is defined as a macro by , while it is defined as a template function in . this renders it impossible to compile an ioengine written in C++ if its source file includes both and . the latter includes via arch/arch.h. the compile error would look like: In file included from ../src/test/fio/fio_ceph_objectstore.cc:26: In file included from src/fio/fio.h:18: In file included from src/fio/thread_options.h:8: In file included from src/fio/gettime.h:7: src/fio/lib/seqlock.h:21:9: error: expected ')' seq = atomic_load_acquire(&s->sequence); ^ src/fio/arch/../lib/../arch/arch.h:47:32: note: expanded from macro 'atomic_load_acquire' atomic_load_explicit((_Atomic typeof(*(p)) *)(p), \ ^ src/fio/lib/seqlock.h:21:9: note: to match this '(' to address this issue, instead of using the functions in to implement seqlock, use the primitives offered by C++ standard library if the source code is compiled using a C++ compiler. Signed-off-by: Kefu Chai --- arch/arch.h | 20 ++++++++++++++++++++ lib/seqlock.h | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/arch/arch.h b/arch/arch.h index a25779d4..fca003be 100644 --- a/arch/arch.h +++ b/arch/arch.h @@ -1,7 +1,11 @@ #ifndef ARCH_H #define ARCH_H +#ifdef __cplusplus +#include +#else #include +#endif #include "../lib/types.h" @@ -36,6 +40,21 @@ extern unsigned long arch_flags; #define ARCH_CPU_CLOCK_WRAPS +#ifdef __cplusplus +#define atomic_add(p, v) \ + std::atomic_fetch_add(p, (v)) +#define atomic_sub(p, v) \ + std::atomic_fetch_sub(p, (v)) +#define atomic_load_relaxed(p) \ + std::atomic_load_explicit(p, \ + std::memory_order_relaxed) +#define atomic_load_acquire(p) \ + std::atomic_load_explicit(p, \ + std::memory_order_acquire) +#define atomic_store_release(p, v) \ + std::atomic_store_explicit(p, (v), \ + std::memory_order_release) +#else #define atomic_add(p, v) \ atomic_fetch_add((_Atomic typeof(*(p)) *)(p), v) #define atomic_sub(p, v) \ @@ -49,6 +68,7 @@ extern unsigned long arch_flags; #define atomic_store_release(p, v) \ atomic_store_explicit((_Atomic typeof(*(p)) *)(p), (v), \ memory_order_release) +#endif /* IWYU pragma: begin_exports */ #if defined(__i386__) diff --git a/lib/seqlock.h b/lib/seqlock.h index 56f3e37d..ef3aa091 100644 --- a/lib/seqlock.h +++ b/lib/seqlock.h @@ -5,7 +5,11 @@ #include "../arch/arch.h" struct seqlock { +#ifdef __cplusplus + std::atomic sequence; +#else volatile unsigned int sequence; +#endif }; static inline void seqlock_init(struct seqlock *s) -- 2.25.1