Skip to content

Commit 5dc0470

Browse files
committed
Remove the need for _Atomic in MSVC builds
1 parent 07d690c commit 5dc0470

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

cwisstable/internal/base.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,12 @@
2323

2424
/// C++11 compatibility macros.
2525
///
26-
/// Atomic support, due to incompatibilities between C++ and C11 atomic syntax.
27-
/// - `CWISS_ATOMIC_T(Type)` names an atomic version of `Type`. We must use this
28-
/// instead of `_Atomic(Type)` to name an atomic type.
29-
/// - `CWISS_ATOMIC_INC(value)` will atomically increment `value` without
30-
/// performing synchronization. This is used as a weak entropy source
31-
/// elsewhere.
32-
///
3326
/// `extern "C"` support via `CWISS_END_EXTERN` and `CWISS_END_EXTERN`,
3427
/// which open and close an `extern "C"` block in C++ mode.
3528
#ifdef __cplusplus
36-
#include <atomic>
37-
#define CWISS_ATOMIC_T(Type_) std::atomic<Type_>
38-
#define CWISS_ATOMIC_INC(val_) (val_).fetch_add(1, std::memory_order_relaxed)
39-
4029
#define CWISS_BEGIN_EXTERN extern "C" {
4130
#define CWISS_END_EXTERN }
4231
#else
43-
#include <stdatomic.h>
44-
#define CWISS_ATOMIC_T(Type_) _Atomic(Type_)
45-
#define CWISS_ATOMIC_INC(val_) \
46-
atomic_fetch_add_explicit(&(val_), 1, memory_order_relaxed)
47-
4832
#define CWISS_BEGIN_EXTERN
4933
#define CWISS_END_EXTERN
5034
#endif
@@ -87,6 +71,34 @@
8771
#define CWISS_GCC_POP
8872
#endif
8973

74+
/// Atomic support, due to incompatibilities between C++ and C11 atomic syntax.
75+
/// - `CWISS_ATOMIC_T(Type)` names an atomic version of `Type`. We must use this
76+
/// instead of `_Atomic(Type)` to name an atomic type.
77+
/// - `CWISS_ATOMIC_INC(value)` will atomically increment `value` without
78+
/// performing synchronization. This is used as a weak entropy source
79+
/// elsewhere.
80+
///
81+
/// MSVC, of course, being that it does not support _Atomic in C mode, so we
82+
/// use `volatile`. This is *wrong*, but MSVC certainly won't miscompile it any
83+
/// worse than it would a relaxed atomic. It doesn't matter for our use of
84+
/// atomics.
85+
#ifdef __cplusplus
86+
#include <atomic>
87+
#define CWISS_ATOMIC_T(Type_) std::atomic<Type_>
88+
#define CWISS_ATOMIC_INC(val_) (val_).fetch_add(1, std::memory_order_relaxed)
89+
#elif CWISS_IS_MSVC
90+
#define CWISS_ATOMIC_T(Type_) volatile Type_
91+
#define CWISS_ATOMIC_INC(val_) (val_ += 1)
92+
#else
93+
#include <stdatomic.h>
94+
#define CWISS_ATOMIC_T(Type_) _Atomic(Type_)
95+
#define CWISS_ATOMIC_INC(val_) \
96+
atomic_fetch_add_explicit(&(val_), 1, memory_order_relaxed)
97+
98+
#define CWISS_BEGIN_EXTERN
99+
#define CWISS_END_EXTERN
100+
#endif
101+
90102
/// Warning control around `CWISS` symbol definitions. These macros will
91103
/// disable certain false-positive warnings that `CWISS` definitions tend to
92104
/// emit.

0 commit comments

Comments
 (0)