This commit is contained in:
Jaremy Creechley 2023-09-14 20:29:35 -07:00
parent 7960ed9786
commit 6e39b2234a
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
7 changed files with 28 additions and 24 deletions

View File

@ -82,6 +82,9 @@ method get*(
var ret = newThreadResult(ValueBuffer)
proc submitGet() =
let bkey = KeyBuffer.new(key)
echo "\n"
echo "get:bkey: ", $key, " => ", bkey.toString(), " :: ", bkey.repr()
echo "\n"
self.tds[].tp.spawn getTask(sig, ret, self.tds, bkey)
submitGet()

View File

@ -42,15 +42,15 @@ proc `==`*(a, b: DataBuffer): bool =
elif a[].buf == b[].buf: return true
else: a.hash() == b.hash()
proc new*[D: DataBuffer](tp: typedesc[D], size: int = 0): D =
proc new*(tp: typedesc[DataBuffer], size: int = 0): DataBuffer =
## allocate new buffer with given size
result = newSharedPtr(DataBufferHolder(
buf: cast[typeof(result[].buf)](allocShared0(size)),
size: size,
))
echoed "DataBuffer:new: ", result.unsafeRawPtr().repr,
" tp ", $(typeof(D)),
" @ ", result[].buf.pointer.repr
# echoed "DataBuffer:new: ", result.unsafeRawPtr().repr,
# " tp ", $(typeof(D)),
# " @ ", result[].buf.pointer.repr
proc new*[T: byte | char; D: DataBuffer](tp: typedesc[D], data: openArray[T]): D =
## allocate new buffer and copies indata from openArray
@ -86,7 +86,8 @@ import ../key
import stew/results
proc new*(tp: typedesc[KeyBuffer], key: Key): KeyBuffer =
result = KeyBuffer.new(key.id())
let ks = key.id()
result = KeyBuffer.new(ks)
echoed "KeyBuffer:new: ", $result
proc toKey*(kb: KeyBuffer): Key =
let res = Key.init(kb.toString())

View File

@ -18,12 +18,12 @@ template checkNotNil(p: typed) =
raiseNilAccess()
when not defined(datastoreEchoString):
proc echoed*(vals: varargs[string, `$`]) =
when not defined(datastoreEchoedThread):
proc echoed*(vals: varargs[string]) =
discard
else:
import std/terminal
proc echoed*(vals: varargs[string, `$`]) =
proc echoed*(vals: varargs[string]) =
proc printThread(): ForegroundColor =
let tclr = [fgRed, fgGreen, fgBlue, fgMagenta, fgCyan]
let tid = getThreadId()
@ -59,7 +59,7 @@ proc decr*[T](x: SharedPtr[T]) =
if x.container != nil:
let res = atomicSubFetch(addr x.container.cnt, 1, ATOMIC_RELEASE)
if res == 0:
echoed "SharedPtr: FREE: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: FREE: ", $x.container.pointer.repr, " cnt: ", $x.container.cnt, " tp: ", $(typeof(T))
when compiles(`=destroy`(x[])):
echoed "SharedPtr:call:child:destructor: ", $(typeof(x[]))
`=destroy`(x[])
@ -68,7 +68,7 @@ proc decr*[T](x: SharedPtr[T]) =
deallocShared(x.container)
# x.container = nil # alas
else:
echoed "SharedPtr: decr: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: decr: ", $x.container.pointer.repr, " cnt: ", $x.container.cnt, " tp: ", $(typeof(T))
proc release*[T](x: var SharedPtr[T]) =
echoed "SharedPtr: release: ", $(typeof(T))
@ -77,7 +77,7 @@ proc release*[T](x: var SharedPtr[T]) =
proc `=destroy`*[T](x: var SharedPtr[T]) =
if x.container != nil and not x.container.manual:
echoed "SharedPtr: destroy: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: destroy: ", $x.container.pointer.repr, " cnt: ", $x.container.cnt, " tp: ", $(typeof(T))
decr(x)
proc `=dup`*[T](src: SharedPtr[T]): SharedPtr[T] =
@ -91,9 +91,9 @@ proc `=copy`*[T](dest: var SharedPtr[T], src: SharedPtr[T]) =
if src.container != nil and not src.container.manual:
# echo "SharedPtr: copy: ", src.container.pointer.repr
discard atomicAddFetch(addr src.container.cnt, 1, ATOMIC_RELAXED)
echoed "SharedPtr: copy:src: ", src.container.pointer.repr, " cnt: ", src.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: copy:src: ", $src.container.pointer.repr, " cnt: ", $src.container.cnt, " tp: ", $(typeof(T))
if dest.container != nil:
echoed "SharedPtr: copy:dest: ", dest.container.pointer.repr, " cnt: ", dest.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: copy:dest: ", $dest.container.pointer.repr, " cnt: ", $dest.container.cnt, " tp: ", $(typeof(T))
`=destroy`(dest)
dest.container = src.container
@ -104,7 +104,7 @@ proc newSharedPtr*[T](val: sink Isolated[T], manualCount: Natural = 0): SharedPt
result.container.cnt = max(1, manualCount)
result.container.manual = manualCount > 0
result.container.value = extract val
echoed "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: alloc: ", $result.container.pointer.repr, " cnt: ", $result.container.cnt, " tp: ", $(typeof(T))
template newSharedPtr*[T](val: T): SharedPtr[T] =
newSharedPtr(isolate(val))
@ -115,14 +115,11 @@ proc newSharedPtr*[T](t: typedesc[T], manualCount: Natural = 0): SharedPtr[T] =
result.container = cast[typeof(result.container)](allocShared0(sizeof(result.container[])))
result.container.cnt = max(1, manualCount)
result.container.manual = manualCount > 0
echoed "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T))
echoed "SharedPtr: alloc: ", $result.container.pointer.repr, " cnt: ", $result.container.cnt, " tp: ", $(typeof(T))
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
p.container == nil
proc unsafeRawPtr*[T](p: SharedPtr[T]): pointer {.inline.} =
p.container.pointer
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
checkNotNil(p)
p.container.value

View File

@ -111,7 +111,7 @@ proc getTask*(
kb: KeyBuffer,
) =
echo "\n"
echoed "getTask: ", $getThreadId()
echoed "getTask: ", $getThreadId(), " kb: ", kb.repr
let key = kb.toKey()
echoed "getTask: key: ", $key
try:

View File

@ -97,6 +97,8 @@ proc acquireSig*(sig: SharedSignal): Future[void] {.async.} =
sig[].sigptr = await getThreadSignal()
proc wait*(sig: SharedSignal): Future[void] =
assert sig.isNil == false
assert sig[].sigptr != nil
sig[].sigptr.wait()
proc fireSync*(sig: SharedSignal): Result[bool, string] =

View File

@ -97,6 +97,7 @@ proc testSuite() =
check a.isNil == true
a.decr()
test "test destroy release generic no proc":
# echo "\nintref setup:\n"
let intref: ref int = new(ref int)

View File

@ -37,16 +37,16 @@ suite "Test Basic ThreadProxyDatastore":
test "check put":
# echo "\n\n=== put ==="
let res1 = await sds.put(key1, data)
print "res1: ", res1
echo "res1: ", res1.repr
check res1.isOk
test "check get":
echo "\n\n=== get ==="
print "get send key: ", key1
echo "get send key: ", key1.repr
let res2 = await sds.get(key1)
print "get key post: ", key1
print "get res2: ", res2
check res2.get() == data
echo "get key post: ", key1.repr
echo "get res2: ", res2.repr
# echo res2.get() == data
var val = ""
for c in res2.get():
val &= char(c)