switch to custom sharedptr

This commit is contained in:
Jaremy Creechley 2023-09-12 20:19:57 -07:00
parent fed5a906eb
commit 97d4e68f5d
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
3 changed files with 11 additions and 6 deletions

View File

@ -43,6 +43,11 @@ proc decr*[T](x: var SharedPtr[T]) =
else:
echo "SharedPtr: decr: ", repr x.val.pointer, " ", x.cnt[], " tp: ", $(typeof(T))
proc release*[T](x: var SharedPtr[T]) =
x.decr()
x.val = nil
x.cnt = nil
proc `=destroy`*[T](x: var SharedPtr[T]) =
if x.val != nil:
echo "SharedPtr: destroy: ", repr x.val.pointer, " cnt: ", x.cnt.repr, " tp: ", $(typeof(T))
@ -74,7 +79,6 @@ template newSharedPtr*[T](val: T): SharedPtr[T] =
proc newSharedPtr*[T](t: typedesc[T]): SharedPtr[T] =
## Returns a shared pointer. It is not initialized,
## so reading from it before writing to it is undefined behaviour!
result.cnt = cast[ptr int](allocShared0(sizeof(result.cnt)))
result.val = cast[typeof(result.val)](allocShared0(sizeof(result.val[])))
result.cnt[] = 0

View File

@ -63,7 +63,7 @@ proc newThreadResult*[T](
proc release*[T](res: var TResult[T]) {.raises: [].} =
## release TResult and it's ThreadSignal
res[].signal.release()
res.decr()
sharedptr.release(res)
proc success*[T](ret: TResult[T], value: T) =
## convenience wrapper for `TResult` to replicate

View File

@ -32,10 +32,11 @@ proc threadTest() =
test "check put":
# echo "\n\n=== put ==="
let res1 = await sds.put(key1, data)
check res1.isOk
# print "res1: ", res1
GC_fullCollect()
for i in 1..3:
let res1 = await sds.put(key1, data)
check res1.isOk
# print "res1: ", res1
GC_fullCollect()
threadTest()
GC_fullCollect()