From b82ff5a87bfb19c7c6a2a7cbf8d860acdadbe877 Mon Sep 17 00:00:00 2001 From: metagn Date: Mon, 30 Sep 2024 21:54:07 +0300 Subject: [PATCH] make C++ atomic opt in via -d:nimUseCppAtomics (#24209) refs #24207 The `-d:nimUseCAtomics` flag added in #24207 is now inverted and made into `-d:nimUseCppAtomics`, which means C++ atomics are only enabled with the define. This flag is now also documented and tested. --- changelog.md | 3 +++ lib/pure/concurrency/atomics.nim | 5 ++++- tests/stdlib/concurrency/tatomics.nim | 4 +++- tests/stdlib/concurrency/tatomics_size.nim | 5 +++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index dfbddeaa6c..61ffa806bf 100644 --- a/changelog.md +++ b/changelog.md @@ -62,6 +62,9 @@ (were `int32`, are now `uint32`) - `n_net` field of `Tnetent` (was `int32`, is now `uint32`) +- The `Atomic[T]` type on C++ now uses C11 primitives by default instead of + `std::atomic`. To use `std::atomic` instead, `-d:nimUseCppAtomics` can be defined. + ## Standard library additions and changes diff --git a/lib/pure/concurrency/atomics.nim b/lib/pure/concurrency/atomics.nim index a3ef2f5f70..818f1b37ac 100644 --- a/lib/pure/concurrency/atomics.nim +++ b/lib/pure/concurrency/atomics.nim @@ -10,6 +10,9 @@ ## Types and operations for atomic operations and lockless algorithms. ## ## Unstable API. +## +## By default, C++ uses C11 atomic primitives. To use C++ `std::atomic`, +## `-d:nimUseCppAtomics` can be defined. runnableExamples: # Atomic @@ -50,7 +53,7 @@ runnableExamples: flag.clear(moRelaxed) assert not flag.testAndSet -when (defined(cpp) and not defined(nimUseCAtomics)) or defined(nimdoc): +when (defined(cpp) and defined(nimUseCppAtomics)) or defined(nimdoc): # For the C++ backend, types and operations map directly to C++11 atomics. {.push, header: "".} diff --git a/tests/stdlib/concurrency/tatomics.nim b/tests/stdlib/concurrency/tatomics.nim index 3fb5197dae..08f2e7d3ee 100644 --- a/tests/stdlib/concurrency/tatomics.nim +++ b/tests/stdlib/concurrency/tatomics.nim @@ -1,5 +1,7 @@ discard """ - matrix: "--mm:refc; --mm:orc" + # test C with -d:nimUseCppAtomics as well to check nothing breaks + matrix: "--mm:refc; --mm:orc; --mm:refc -d:nimUseCppAtomics; --mm:orc -d:nimUseCppAtomics" + targets: "c cpp" """ # test atomic operations diff --git a/tests/stdlib/concurrency/tatomics_size.nim b/tests/stdlib/concurrency/tatomics_size.nim index cfe568623d..f64adb3082 100644 --- a/tests/stdlib/concurrency/tatomics_size.nim +++ b/tests/stdlib/concurrency/tatomics_size.nim @@ -1,5 +1,6 @@ discard """ - matrix: "--mm:refc; --mm:orc" + # test C with -d:nimUseCppAtomics as well to check nothing breaks + matrix: "--mm:refc; --mm:orc; --mm:refc -d:nimUseCppAtomics; --mm:orc -d:nimUseCppAtomics" targets: "c cpp" """ import std/atomics @@ -17,4 +18,4 @@ block testSize: # issue 12726 f: AtomicFlag static: doAssert sizeof(Node) == sizeof(pointer) - doAssert sizeof(MyChannel) == sizeof(pointer) * 2 \ No newline at end of file + doAssert sizeof(MyChannel) == sizeof(pointer) * 2