Skip to content

Commit 41b7148

Browse files
Nyctonarimiran
authored andcommitted
Resolve cross file resolution errors in atomics (#19422) [backport:1.6]
* Resolve call undeclared routine testAndSet * Fix undeclared field atomicType (cherry picked from commit 851e515)
1 parent 3d3b344 commit 41b7148

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

lib/pure/concurrency/atomics.nim

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,16 @@ else:
293293
AtomicInt32 {.importc: "_Atomic NI32".} = int32
294294
AtomicInt64 {.importc: "_Atomic NI64".} = int64
295295

296-
template atomicType*(T: typedesc[Trivial]): untyped =
297-
# Maps the size of a trivial type to it's internal atomic type
298-
when sizeof(T) == 1: AtomicInt8
299-
elif sizeof(T) == 2: AtomicInt16
300-
elif sizeof(T) == 4: AtomicInt32
301-
elif sizeof(T) == 8: AtomicInt64
302-
303296
type
304297
AtomicFlag* {.importc: "atomic_flag", size: 1.} = object
305298

306299
Atomic*[T] = object
307300
when T is Trivial:
308-
value: T.atomicType
301+
# Maps the size of a trivial type to it's internal atomic type
302+
when sizeof(T) == 1: value: AtomicInt8
303+
elif sizeof(T) == 2: value: AtomicInt16
304+
elif sizeof(T) == 4: value: AtomicInt32
305+
elif sizeof(T) == 8: value: AtomicInt64
309306
else:
310307
nonAtomicValue: T
311308
guard: AtomicFlag
@@ -364,11 +361,11 @@ else:
364361
cast[T](atomic_fetch_xor_explicit(addr(location.value), cast[nonAtomicType(T)](value), order))
365362

366363
template withLock[T: not Trivial](location: var Atomic[T]; order: MemoryOrder; body: untyped): untyped =
367-
while location.guard.testAndSet(moAcquire): discard
364+
while testAndSet(location.guard, moAcquire): discard
368365
try:
369366
body
370367
finally:
371-
location.guard.clear(moRelease)
368+
clear(location.guard, moRelease)
372369

373370
proc load*[T: not Trivial](location: var Atomic[T]; order: MemoryOrder = moSequentiallyConsistent): T {.inline.} =
374371
withLock(location, order):
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import atomics
2+
3+
type
4+
AtomicWithGeneric*[T] = object
5+
value: Atomic[T]
6+
7+
proc initAtomicWithGeneric*[T](value: T): AtomicWithGeneric[T] =
8+
result.value.store(value)
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import atomicSample
2+
3+
block crossFileObjectContainingAGenericWithAComplexObject:
4+
discard initAtomicWithGeneric[string]("foo")
5+
6+
block crossFileObjectContainingAGenericWithAnInteger:
7+
discard initAtomicWithGeneric[int](1)
8+
discard initAtomicWithGeneric[int8](1)
9+
discard initAtomicWithGeneric[int16](1)
10+
discard initAtomicWithGeneric[int32](1)
11+
discard initAtomicWithGeneric[int64](1)

0 commit comments

Comments
 (0)