I’m getting a "type mismatch" when compiling my code, and I cannot see what I’m
doing wrong. Since the code is somewhat large, here is a summarized version:
# MODULE atomiks
type
VolatilePtr*[T] = distinct ptr T
template declVolatile*(name: untyped, T: typedesc, init: T) {.dirty.} =
## Defines an hidden volatile value, with an initial value, and a
pointer to it
var `volatile XXX name XXX value` {.global,volatile.} = init
let `name` {.global.} = cast[VolatilePtr[T]](addr `volatile XXX name XXX
value`)
# ...
proc atomicIncRelaxed*[T: AtomType](p: VolatilePtr[T], x: T = 1): T =
## Increments a volatile (32/64 bit) value, and returns the new value.
## Performed in RELAXED/No-Fence memory-model.
## Will only compile on Windows 8+!
let pp = cast[pointer](p)
when sizeof(T) == 8:
cast[T](interlockedExchangeAddNoFence64(pp, cast[int64](x)))
elif sizeof(T) == 4:
cast[T](interlockedExchangeAddNoFence32(pp, cast[int32](x)))
else:
static: assert(false, "invalid parameter size: " & $sizeof(T))
# MODULE kueues
type
MsgSeqID* = int32
declVolatile(myProcessGlobalSeqID, MsgSeqID, MsgSeqID(0))
# Current (global) per Process message sequence ID.
proc seqidProvider(): MsgSeqID =
## Generates a message sequence ID for the Thread.
result = atomicIncRelaxed(myProcessGlobalSeqID) # LINE NO 213
The line with atomicIncRelaxed() doesn’t compile with this message:
kueues.nim(213, 14) Error: type mismatch: got (int) but expected 'MsgSeqID
= int32'
I think that the type of “myProcessGlobalSeqID” should be
“VolatilePtr[MsgSeqID]”, therefore the call to
“atomicIncRelaxed(myProcessGlobalSeqID)” should receive a
“VolatilePtr[MsgSeqID]” as input parameter, and return a “MsgSeqID” as output,
since I cast the result of "interlockedExchangeAddNoFenceXX()" to "T". Since
“result” is of type “MsgSeqID”, everything should be fine, IMO.