* fixes #17893
This commit is contained in:
Andreas Rumpf
2021-07-13 14:17:59 +02:00
committed by GitHub
parent b3aca78e22
commit 12da32a891
2 changed files with 68 additions and 6 deletions

View File

@@ -416,12 +416,7 @@ proc considerUserDefinedOp(c: var TLiftCtx; t: PType; body, x, y: PNode): bool =
body.add destructorCall(c, op, x)
result = true
#result = addDestructorCall(c, t, body, x)
of attachedAsgn, attachedSink:
var op = getAttachedOp(c.g, t, c.kind)
result = considerAsgnOrSink(c, t, body, x, y, op)
if op != nil:
setAttachedOp(c.g, c.idgen.module, t, c.kind, op)
of attachedTrace:
of attachedAsgn, attachedSink, attachedTrace:
var op = getAttachedOp(c.g, t, c.kind)
if op != nil and sfOverriden in op.flags:
if op.ast.isGenericRoutine:

View File

@@ -0,0 +1,67 @@
discard """
cmd: "nim c --threads:on --gc:arc $file"
action: compile
"""
# bug #17893
type
SharedPtr*[T] = object
val: ptr tuple[value: T, atomicCounter: int]
proc `=destroy`*[T](p: var SharedPtr[T]) =
mixin `=destroy`
if p.val != nil:
if atomicLoadN(addr p.val[].atomicCounter, AtomicConsume) == 0:
`=destroy`(p.val[])
deallocShared(p.val)
else:
discard atomicDec(p.val[].atomicCounter)
proc `=copy`*[T](dest: var SharedPtr[T], src: SharedPtr[T]) =
if src.val != nil:
discard atomicInc(src.val[].atomicCounter)
if dest.val != nil:
`=destroy`(dest)
dest.val = src.val
proc newSharedPtr*[T](val: sink T): SharedPtr[T] {.nodestroy.} =
result.val = cast[typeof(result.val)](allocShared(sizeof(result.val[])))
result.val.atomicCounter = 0
result.val.value = val
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
p.val == nil
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
when compileOption("boundChecks"):
doAssert(p.val != nil, "deferencing nil shared pointer")
result = p.val.value
type
Sender*[T] = object
queue: SharedPtr[seq[T]]
proc newSender*[T](queue: sink SharedPtr[seq[T]]): Sender[T] =
result = Sender[T](queue: queue)
proc send*[T](self: Sender[T]; t: sink T) =
self.queue[].add t
proc newChannel*(): Sender[int] =
let queue = newSharedPtr(newSeq[int]())
result = newSender(queue)
var
p: Thread[Sender[int]]
proc threadFn(tx: Sender[int]) =
send tx, 0
proc multiThreadedChannel =
let tx = newChannel()
createThread(p, threadFn, tx)
joinThread(p)
multiThreadedChannel()