Skip to content

Commit

Permalink
fix nim-lang#12726 Cannot take the compile-time sizeof Atomic types (n…
Browse files Browse the repository at this point in the history
…im-lang#15928)

* fix nim-lang#12726 Cannot take the compile-time sizeof Atomic types

* fix for arch 32
  • Loading branch information
bung87 authored and irdassis committed Feb 12, 2021
1 parent 5e22477 commit 87eaf92
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/pure/concurrency/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ when defined(cpp) or defined(nimdoc):
## with other moSequentiallyConsistent operations.

type
Atomic* {.importcpp: "std::atomic".} [T] = object
Atomic*[T] {.importcpp: "std::atomic", completeStruct.} = object
## An atomic object with underlying type `T`.
raw: T

AtomicFlag* {.importcpp: "std::atomic_flag".} = object
AtomicFlag* {.importcpp: "std::atomic_flag", size: 1.} = object
## An atomic boolean state.

# Access operations
Expand Down Expand Up @@ -249,10 +250,10 @@ else:
type
# Atomic* {.importcpp: "_Atomic('0)".} [T] = object

AtomicInt8 {.importc: "_Atomic NI8".} = object
AtomicInt16 {.importc: "_Atomic NI16".} = object
AtomicInt32 {.importc: "_Atomic NI32".} = object
AtomicInt64 {.importc: "_Atomic NI64".} = object
AtomicInt8 {.importc: "_Atomic NI8", size: 1.} = object
AtomicInt16 {.importc: "_Atomic NI16", size: 2.} = object
AtomicInt32 {.importc: "_Atomic NI32", size: 4.} = object
AtomicInt64 {.importc: "_Atomic NI64", size: 8.} = object

template atomicType*(T: typedesc[Trivial]): untyped =
# Maps the size of a trivial type to it's internal atomic type
Expand All @@ -262,7 +263,7 @@ else:
elif sizeof(T) == 8: AtomicInt64

type
AtomicFlag* {.importc: "atomic_flag".} = object
AtomicFlag* {.importc: "atomic_flag", size: 1.} = object

Atomic*[T] = object
when T is Trivial:
Expand Down
18 changes: 18 additions & 0 deletions tests/stdlib/concurrency/tatomics_size.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
discard """
targets: "c cpp"
"""
import std/atomics

block testSize: # issue 12726
type
Node = ptr object
# works
next: Atomic[pointer]
f:AtomicFlag
MyChannel = object
# type not defined completely
back: Atomic[ptr int]
f: AtomicFlag
static:
doAssert sizeof(Node) == sizeof(pointer)
doAssert sizeof(MyChannel) == sizeof(pointer) * 2

0 comments on commit 87eaf92

Please sign in to comment.