Skip to content

Commit

Permalink
make C++ atomic opt in via -d:nimUseCppAtomics
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 30, 2024
1 parent 2a48182 commit 783adb6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:nimUseCppAtomic` can be defined.


## Standard library additions and changes

Expand Down
5 changes: 4 additions & 1 deletion lib/pure/concurrency/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: "<atomic>".}
Expand Down
4 changes: 3 additions & 1 deletion tests/stdlib/concurrency/tatomics.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/stdlib/concurrency/tatomics_size.nim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,4 +18,4 @@ block testSize: # issue 12726
f: AtomicFlag
static:
doAssert sizeof(Node) == sizeof(pointer)
doAssert sizeof(MyChannel) == sizeof(pointer) * 2
doAssert sizeof(MyChannel) == sizeof(pointer) * 2

0 comments on commit 783adb6

Please sign in to comment.