Skip to content

Commit

Permalink
Implement isUniqueRef to SharedPtr (#81)
Browse files Browse the repository at this point in the history
* add isUniqueRef

* bump version to include new fixed smartptrs in a nimble compatible way

* Apply suggestions from code review

---------

Co-authored-by: Andreas Rumpf <[email protected]>
  • Loading branch information
elcritch and Araq authored Dec 24, 2024
1 parent c69e13a commit c5a39a0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/tsmartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ block:
doAssert not a3.isNil
doAssert a3[] == 0

doAssert a1.isUniqueRef()
a1 = newSharedPtr(int)
a1[] = 1
doAssert a1[] == 1
var a4 = newSharedPtr(string)
a4[] = "hello world"
doAssert a4[] == "hello world"
doAssert a4.isUniqueRef()
var a4p = a4
doAssert(not a4.isUniqueRef())

block:
var a1: ConstPtr[float]
Expand Down
2 changes: 1 addition & 1 deletion threading.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.2.0"
version = "0.2.1"
author = "Araq"
description = "New threading primitives for --mm:arc/orc: atomics, channels, smart pointers and wait groups."
license = "MIT"
Expand Down
6 changes: 6 additions & 0 deletions threading/smartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ proc `[]=`*[T](p: SharedPtr[T], val: sink Isolated[T]) {.inline.} =
template `[]=`*[T](p: SharedPtr[T]; val: T) =
`[]=`(p, isolate(val))

proc isUniqueRef*[T](p: SharedPtr[T]): bool =
if p.val == nil:
return true
p.val.counter.load(moAcquireRelease) == 0


proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
if p.val == nil: "nil"
else: "(val: " & $p.val.value & ")"
Expand Down

0 comments on commit c5a39a0

Please sign in to comment.