From 6e39b2234a05f215c1b742294e6d56b8a1db9d62 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Thu, 14 Sep 2023 20:29:35 -0700 Subject: [PATCH] fixups --- datastore/threadproxyds.nim | 3 +++ datastore/threads/databuffer.nim | 11 ++++++----- datastore/threads/sharedptr.nim | 23 ++++++++++------------- datastore/threads/threadbackend.nim | 2 +- datastore/threads/threadsignalpool.nim | 2 ++ tests/datastore/testsharedptr.nim | 1 + tests/datastore/testthreadproxyds.nim | 10 +++++----- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/datastore/threadproxyds.nim b/datastore/threadproxyds.nim index ea11e5c..9cdad48 100644 --- a/datastore/threadproxyds.nim +++ b/datastore/threadproxyds.nim @@ -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() diff --git a/datastore/threads/databuffer.nim b/datastore/threads/databuffer.nim index 79e3a0e..ed0f0e1 100644 --- a/datastore/threads/databuffer.nim +++ b/datastore/threads/databuffer.nim @@ -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()) diff --git a/datastore/threads/sharedptr.nim b/datastore/threads/sharedptr.nim index d9eb168..bcf1803 100644 --- a/datastore/threads/sharedptr.nim +++ b/datastore/threads/sharedptr.nim @@ -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 diff --git a/datastore/threads/threadbackend.nim b/datastore/threads/threadbackend.nim index be15c49..d8585f7 100644 --- a/datastore/threads/threadbackend.nim +++ b/datastore/threads/threadbackend.nim @@ -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: diff --git a/datastore/threads/threadsignalpool.nim b/datastore/threads/threadsignalpool.nim index 1d7b7ea..b56459c 100644 --- a/datastore/threads/threadsignalpool.nim +++ b/datastore/threads/threadsignalpool.nim @@ -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] = diff --git a/tests/datastore/testsharedptr.nim b/tests/datastore/testsharedptr.nim index bc910bb..9391897 100644 --- a/tests/datastore/testsharedptr.nim +++ b/tests/datastore/testsharedptr.nim @@ -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) diff --git a/tests/datastore/testthreadproxyds.nim b/tests/datastore/testthreadproxyds.nim index 5d4e869..a0ac227 100644 --- a/tests/datastore/testthreadproxyds.nim +++ b/tests/datastore/testthreadproxyds.nim @@ -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)