mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-02-09 00:03:09 +00:00
sharedptr
This commit is contained in:
parent
393ed6a39e
commit
9ced6620a5
@ -7,7 +7,6 @@ import pkg/questionable/results
|
||||
import pkg/upraises
|
||||
import pkg/taskpools
|
||||
import pkg/stew/results
|
||||
import pkg/threading/smartptrs
|
||||
|
||||
import ./key
|
||||
import ./query
|
||||
|
||||
@ -21,42 +21,43 @@ type
|
||||
SharedPtr*[T] = object
|
||||
## Shared ownership reference counting pointer.
|
||||
cnt: ptr int
|
||||
buf*: ptr T
|
||||
val*: ptr T
|
||||
|
||||
proc incr*[T](a: SharedPtr[T]) =
|
||||
let res = atomicAddFetch(a.cnt, 1, ATOMIC_RELAXED)
|
||||
echo "SIGNAL: manual incr: ", res
|
||||
echo "SharedPtr: manual incr: ", res
|
||||
|
||||
proc decr*[T](x: SharedPtr[T]) =
|
||||
if x.buf != nil and x.cnt != nil:
|
||||
if x.val != nil and x.cnt != nil:
|
||||
let res = atomicSubFetch(x.cnt, 1, ATOMIC_ACQUIRE)
|
||||
if res == 0:
|
||||
echo "SIGNAL: FREE: ", repr x.buf.pointer, " ", x.cnt[]
|
||||
deallocShared(x.buf)
|
||||
echo "SharedPtr: FREE: ", repr x.val.pointer, " ", x.cnt[]
|
||||
deallocShared(x.val)
|
||||
deallocShared(x.cnt)
|
||||
else:
|
||||
echo "SIGNAL: decr: ", repr x.buf.pointer, " ", x.cnt[]
|
||||
echo "SharedPtr: decr: ", repr x.val.pointer, " ", x.cnt[]
|
||||
|
||||
proc `=destroy`*[T](x: var SharedPtr[T]) =
|
||||
echo "SIGNAL: destroy: ", repr x.buf.pointer, " ", x.cnt.repr
|
||||
echo "SIGNAL: destroy:st: ", ($getStackTrace()).split("\n").join(";")
|
||||
echo "SharedPtr: destroy: ", repr x.val.pointer, " ", x.cnt.repr
|
||||
# echo "SharedPtr: destroy:st: ", ($getStackTrace()).split("\n").join(";")
|
||||
decr(x)
|
||||
|
||||
proc `=dup`*[T](src: SharedPtr[T]): SharedPtr[T] =
|
||||
if src.val != nil:
|
||||
discard fetchAdd(src.cnt, 1, ATOMIC_RELAXED)
|
||||
discard atomicAddFetch(src.cnt, 1, ATOMIC_RELAXED)
|
||||
result.val = src.val
|
||||
|
||||
proc `=copy`*[T](dest: var SharedPtr[T], src: SharedPtr[T]) =
|
||||
if src.val != nil:
|
||||
# echo "SharedPtr: copy: ", src.val.pointer.repr
|
||||
discard fetchAdd(src.val.counter, 1, ATOMIC_RELAXED)
|
||||
discard atomicAddFetch(src.cnt, 1, ATOMIC_RELAXED)
|
||||
`=destroy`(dest)
|
||||
dest.val = src.val
|
||||
|
||||
proc newSharedPtr*[T](val: sink Isolated[T]): SharedPtr[T] {.nodestroy.} =
|
||||
## Returns a shared pointer which shares
|
||||
## Returns a shared pointer which shares,
|
||||
## ownership of the object by reference counting.
|
||||
result.cnt = cast[ptr int](allocShared0(sizeof(result.cnt)))
|
||||
result.val = cast[typeof(result.val)](allocShared(sizeof(result.val[])))
|
||||
int(result.val.counter) = 1
|
||||
result.val.value = extract val
|
||||
@ -68,8 +69,9 @@ 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[])))
|
||||
int(result.val.counter) = 0
|
||||
result.cnt[] = 1
|
||||
echo "SharedPtr: allocT: ", result.val.pointer.repr, " tp: ", $(typeof(T))
|
||||
|
||||
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
|
||||
@ -77,11 +79,11 @@ proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
|
||||
|
||||
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
|
||||
checkNotNil(p)
|
||||
p.val.value
|
||||
p.val[]
|
||||
|
||||
proc `[]=`*[T](p: SharedPtr[T], val: sink Isolated[T]) {.inline.} =
|
||||
checkNotNil(p)
|
||||
p.val.value = extract val
|
||||
p.val[] = extract val
|
||||
|
||||
template `[]=`*[T](p: SharedPtr[T]; val: T) =
|
||||
`[]=`(p, isolate(val))
|
||||
|
||||
@ -5,8 +5,8 @@ import pkg/questionable/results
|
||||
import stew/results
|
||||
import pkg/upraises
|
||||
import pkg/taskpools
|
||||
import pkg/threading/smartptrs
|
||||
|
||||
import ./sharedptr
|
||||
import ../key
|
||||
import ../query
|
||||
import ./datastore
|
||||
@ -14,8 +14,7 @@ import ./databuffer
|
||||
import ./threadresults
|
||||
|
||||
# import pretty
|
||||
|
||||
export key, query, smartptrs, databuffer
|
||||
export key, query, sharedptr, databuffer
|
||||
export threadresults
|
||||
|
||||
push: {.upraises: [].}
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
|
||||
import pkg/chronos/threadsync
|
||||
import pkg/threading/smartptrs
|
||||
import pkg/chronos
|
||||
import std/locks
|
||||
import std/sets
|
||||
|
||||
import ./sharedptr
|
||||
import ./databuffer
|
||||
import ./threadsignalpool
|
||||
|
||||
export databuffer
|
||||
export smartptrs
|
||||
export sharedptr
|
||||
export threadsync
|
||||
|
||||
type
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
import pkg/chronos/threadsync
|
||||
import pkg/threading/smartptrs
|
||||
import pkg/chronos
|
||||
import std/locks
|
||||
import std/sets
|
||||
|
||||
import ./databuffer
|
||||
import ./sharedptr
|
||||
|
||||
export databuffer
|
||||
export smartptrs
|
||||
export sharedptr
|
||||
export threadsync
|
||||
|
||||
const
|
||||
|
||||
@ -35,6 +35,7 @@ suite "Test Basic ThreadProxyDatastore":
|
||||
check res1.isOk
|
||||
# print "res1: ", res1
|
||||
|
||||
|
||||
test "check get":
|
||||
# echo "\n\n=== get ==="
|
||||
let res2 = await sds.get(key1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user