Skip to content

Commit

Permalink
another tweak whatfer greening up some balls (#34)
Browse files Browse the repository at this point in the history
* rm spam

* gardening

* rm another tsan warning
  • Loading branch information
disruptek authored Mar 20, 2024
1 parent fd16652 commit d71bfdd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
20 changes: 12 additions & 8 deletions loony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ proc toStrTuple*(tag: TagPtr): string =

proc fetchAddSlot(tag: TagPtr; w: uint): uint =
## A convenience to fetchAdd the node's slot.
fetchAddSlot(cast[ptr Node](nptr tag)[], idx tag, w)
if tag == 0:
raise AssertionDefect.newException "tagptr was empty"
else:
result = fetchAddSlot(tag.node, idx tag, w)

proc fetchTail(queue: LoonyQueue, moorder: MemoryOrder = moRelaxed): TagPtr =
## get the TagPtr of the tail (nptr: NodePtr, idx: uint16)
TagPtr(load(queue.tail, order = moorder))
TagPtr load(queue.tail, order = moorder)

proc fetchHead(queue: LoonyQueue, moorder: MemoryOrder = moRelaxed): TagPtr =
## get the TagPtr of the head (nptr: NodePtr, idx: uint16)
TagPtr(load(queue.head, order = moorder))
TagPtr load(queue.head, order = moorder)

proc fetchCurrTail(queue: LoonyQueue): NodePtr {.used.} =
# get the NodePtr of the current tail
Expand Down Expand Up @@ -191,11 +194,12 @@ proc advTail[T](queue: LoonyQueue[T]; pel: uint; tag: TagPtr): AdvTail =
var next = origTail.fetchNext()
if cast[ptr Node](next).isNil():
# Prepare the new node with our element in it
var (node, null) = (allocNode pel, 0'u) # Atomic compareExchange requires variables
var node = allocNode pel
var null: uint
if origTail.compareAndSwapNext(null, node.toUint):
# Successfully inserted our node into current/original nodes next
# Since we have already inserted a slot, we try to replace the queues
# tail tagptr with the new node with an index of 1
# Successfully inserted our node into current/original nodes next.
# Since we have already inserted a slot, we try to replace the queue's
# tail tagptr with the new node, with an index of 1.
while not queue.compareAndSwapTail(currTTag, node.toUint + 1):
# Loop is not relevant to compareAndSwapStrong; consider weak swap?
if currTTag.nptr != origTail:
Expand Down Expand Up @@ -396,7 +400,7 @@ proc popImpl[T](queue: LoonyQueue[T]; forcedCoherence: static bool = false): T =
# ideally, no one knows about this reference, so we'll
# make an adjustment here to counter the cast incref and
# afford ordering elsewhere
let owners = atomicDecRef(result, ATOMIC_ACQ_REL)
let owners {.used.} = atomicDecRef(result, ATOMIC_ACQ_REL)
# since we have some extra information here, we'll throw
# in a guard which should only trigger in the event the
# ownership was corrupted while the ref was in the queue
Expand Down
5 changes: 3 additions & 2 deletions loony/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ proc prepareElement*[T](el: var T): uint =
## Prepare an item for the queue: ensure that no other threads will free
## it, then turn it into an integer and add the WRITER bit.
when T is ref:
let owners = atomicRC(el, ATOMIC_ACQUIRE)
let owners {.used.} = atomicRC(el, ATOMIC_ACQUIRE)
when loonyIsolated:
if owners != 0:
raise AssertionDefect.newException:
Expand Down Expand Up @@ -137,7 +137,8 @@ proc tryReclaim*(node: var Node; start: uint16) =
for i in start..<N:
template s: Atomic[uint] = node.slots[i]
if (s.load(order = moAcquire) and CONSUMED) != CONSUMED:
var prev = s.fetchAdd(RESUME, order = moRelaxed) and CONSUMED
# cpp impl is Relaxed; we use Release here to remove tsan warning
var prev = s.fetchAdd(RESUME, order = moRelease) and CONSUMED
if prev != CONSUMED:
incRecPathCounter()
break done
Expand Down

0 comments on commit d71bfdd

Please sign in to comment.