-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cbindings): Thread-safe communication between the main threa…
…d and the Waku Thread (#1978) * Thread-safe comms between main thread & Waku Thread - ChannelSPSCSingle. * Renaming procs from 'new' to 'createShared'. They use the shared allocator. * peer_manager_request: no need to use ptr WakuNode. * waku_thread: moving the 'waitFor' to upper layer. * waku_thread: `poll()` -> `waitFor sleepAsync(1)` to avoid risk of blocking. * libwaku: thread-safe "sub-objects" in an inter-thread requests. When two threads send data each other, that data cannot contain any GC'ed type (string, seq, ref, closures) at any level. * Allocating the 'configJson' in main thread and deallocating in Waku Thread.
- Loading branch information
1 parent
2e515a0
commit 72f9066
Showing
12 changed files
with
378 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,36 @@ | ||
## Can be shared safely between threads | ||
type SharedSeq*[T] = tuple[data: ptr UncheckedArray[T], len: int] | ||
|
||
proc alloc*(str: cstring): cstring = | ||
# Byte allocation from the given address. | ||
# There should be the corresponding manual deallocation with deallocShared ! | ||
let ret = cast[cstring](allocShared(len(str) + 1)) | ||
copyMem(ret, str, len(str) + 1) | ||
return ret | ||
|
||
proc alloc*(str: string): cstring = | ||
## Byte allocation from the given address. | ||
## There should be the corresponding manual deallocation with deallocShared ! | ||
var ret = cast[cstring](allocShared(str.len + 1)) | ||
let s = cast[seq[char]](str) | ||
for i in 0..<str.len: | ||
ret[i] = s[i] | ||
ret[str.len] = '\0' | ||
return ret | ||
|
||
proc allocSharedSeq*[T](s: seq[T]): SharedSeq[T] = | ||
let data = cast[ptr T](allocShared(s.len)) | ||
copyMem(data, unsafeAddr s, s.len) | ||
return (cast[ptr UncheckedArray[T]](data), s.len) | ||
|
||
proc deallocSharedSeq*[T](s: var SharedSeq[T]) = | ||
deallocShared(s.data) | ||
s.len = 0 | ||
|
||
proc toSeq*[T](s: SharedSeq[T]): seq[T] = | ||
## Creates a seq[T] from a SharedSeq[T]. No explicit dealloc is required | ||
## as req[T] is a GC managed type. | ||
var ret = newSeq[T]() | ||
for i in 0..<s.len: | ||
ret.add(s.data[i]) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
library/waku_thread/inter_thread_communication/protocols/relay_request.nim
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
library/waku_thread/inter_thread_communication/request.nim
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.