Skip to content

Commit

Permalink
Workaround nim-lang/Nim#13048
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jan 5, 2020
1 parent 392c483 commit a25441c
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion weave/channels/channels_mpsc_unbounded_batch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ import
../primitives/compiler_optimization_hints, # for prefetch
../instrumentation/[contracts, loggers]

# type dereference macro
# ------------------------------------------------
# This macro dereference pointer types
# This workarounds:
# - https://github.com/nim-lang/Nim/issues/12714
# - https://github.com/nim-lang/Nim/issues/13048

macro derefMPSC*(T: typedesc): typedesc =
# This somehows isn't bound properly when used in a typesection
let instantiated = T.getTypeInst
instantiated.expectkind(nnkBracketExpr)
doAssert instantiated[0].eqIdent"typeDesc"

let ptrT = instantiated[1]
if ptrT.kind == nnkPtrTy:
return ptrT[0]

let ptrTImpl = instantiated[1].getImpl
ptrTimpl.expectKind(nnkTypeDef)
ptrTImpl[2].expectKind(nnkPtrTy)
ptrTImpl[2][0].expectKind({nnkObjectTy, nnkSym})

return ptrTImpl[2][0]

# MPSC channel
# ------------------------------------------------

type
Enqueueable = concept x, type T
x is ptr
Expand Down Expand Up @@ -33,7 +60,7 @@ type
# Producers and consumer slow-path
back{.align: WV_CacheLinePadding.}: Atomic[pointer] # Workaround generic atomics bug: https://github.com/nim-lang/Nim/issues/12695
# Consumer only - front is a dummy node
front{.align: WV_CacheLinePadding.}: typeof(default(T)[])
front{.align: WV_CacheLinePadding.}: derefMPSC(T)

# Debugging
# --------------------------------------------------------------
Expand Down

0 comments on commit a25441c

Please sign in to comment.