Skip to content

Commit

Permalink
fix: initialize GC
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Nov 30, 2024
1 parent 03431a9 commit 2267f1b
Showing 1 changed file with 61 additions and 30 deletions.
91 changes: 61 additions & 30 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ proc handleRequest(
userData: pointer,
): cint =
waku_thread.sendRequestToWakuThread(ctx, requestType, content, callback, userData).isOkOr:
foreignThreadGc:
let msg = "libwaku error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
let msg = "libwaku error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR

return RET_OK
Expand All @@ -70,15 +69,14 @@ proc relayEventCallback(ctx: ptr WakuContext): WakuRelayHandler =
error "eventUserData is nil"
return

try:
let event = $JsonMessageEvent.new(pubsubTopic, msg)
foreignThreadGc:
foreignThreadGc:
try:
let event = $JsonMessageEvent.new(pubsubTopic, msg)
cast[WakuCallBack](ctx[].eventCallback)(
RET_OK, unsafeAddr event[0], cast[csize_t](len(event)), ctx[].eventUserData
)
except Exception, CatchableError:
let msg = "Exception when calling 'eventCallBack': " & getCurrentExceptionMsg()
foreignThreadGc:
except Exception, CatchableError:
let msg = "Exception when calling 'eventCallBack': " & getCurrentExceptionMsg()
cast[WakuCallBack](ctx[].eventCallback)(
RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), ctx[].eventUserData
)
Expand All @@ -104,34 +102,39 @@ if defined(android):
) {.raises: [].} =
echo logLevel, msg

proc initializeLibrary() {.exported.} =
if not initialized.exchange(true):
NimMain() # Every Nim library needs to call `NimMain` once exactly
when declared(setupForeignThreadGc):
setupForeignThreadGc()
when declared(nimGC_setStackBottom):
var locals {.volatile, noinit.}: pointer
locals = addr(locals)
nimGC_setStackBottom(locals)

### End of library setup
################################################################################

################################################################################
### Exported procs
proc waku_setup() {.dynlib, exportc.} =
NimMain()
if not initialized.load:
initialized.store(true)

when declared(nimGC_setStackBottom):
var locals {.volatile, noinit.}: pointer
locals = addr(locals)
nimGC_setStackBottom(locals)
initializeLibrary()

proc waku_new(
configJson: cstring, callback: WakuCallback, userData: pointer
): pointer {.dynlib, exportc, cdecl.} =
initializeLibrary()

## Creates a new instance of the WakuNode.
if isNil(callback):
echo "error: missing callback in waku_new"
return nil

## Create the Waku thread that will keep waiting for req from the main thread.
var ctx = waku_thread.createWakuThread().valueOr:
foreignThreadGc:
let msg = "Error in createWakuThread: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
let msg = "Error in createWakuThread: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return nil

ctx.userData = userData
Expand All @@ -152,34 +155,34 @@ proc waku_new(
proc waku_destroy(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

waku_thread.destroyWakuThread(ctx).isOkOr:
foreignThreadGc:
let msg = "libwaku error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
let msg = "libwaku error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR

return RET_OK

proc waku_version(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

foreignThreadGc:
callback(
RET_OK,
cast[ptr cchar](WakuNodeVersionString),
cast[csize_t](len(WakuNodeVersionString)),
userData,
)
callback(
RET_OK,
cast[ptr cchar](WakuNodeVersionString),
cast[csize_t](len(WakuNodeVersionString)),
userData,
)

return RET_OK

proc waku_set_event_callback(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
) {.dynlib, exportc.} =
initializeLibrary()
ctx[].eventCallback = cast[pointer](callback)
ctx[].eventUserData = userData

Expand All @@ -194,6 +197,7 @@ proc waku_content_topic(
): cint {.dynlib, exportc.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding

initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let appStr = appName.alloc()
Expand All @@ -216,6 +220,7 @@ proc waku_pubsub_topic(
): cint {.dynlib, exportc, cdecl.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding

initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let topicNameStr = topicName.alloc()
Expand All @@ -234,6 +239,7 @@ proc waku_default_pubsub_topic(
): cint {.dynlib, exportc.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_default_pubsub_topic

initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

callback(
Expand All @@ -255,6 +261,7 @@ proc waku_relay_publish(
): cint {.dynlib, exportc, cdecl.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms

initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let jwm = jsonWakuMessage.alloc()
Expand Down Expand Up @@ -301,6 +308,7 @@ proc waku_relay_publish(
proc waku_start(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)
handleRequest(
ctx,
Expand All @@ -313,6 +321,7 @@ proc waku_start(
proc waku_stop(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)
handleRequest(
ctx,
Expand All @@ -328,6 +337,7 @@ proc waku_relay_subscribe(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
Expand All @@ -351,6 +361,7 @@ proc waku_relay_unsubscribe(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
Expand All @@ -375,6 +386,7 @@ proc waku_relay_get_num_connected_peers(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
Expand All @@ -395,6 +407,7 @@ proc waku_relay_get_num_peers_in_mesh(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
Expand All @@ -416,6 +429,7 @@ proc waku_lightpush_publish(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc, cdecl.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

let jwm = jsonWakuMessage.alloc()
Expand Down Expand Up @@ -461,6 +475,7 @@ proc waku_connect(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -476,6 +491,7 @@ proc waku_connect(
proc waku_disconnect_peer_by_id(
ctx: ptr WakuContext, peerId: cstring, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -496,6 +512,7 @@ proc waku_dial_peer(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -518,6 +535,7 @@ proc waku_dial_peer_by_id(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -533,6 +551,7 @@ proc waku_dial_peer_by_id(
proc waku_get_peerids_from_peerstore(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -546,6 +565,7 @@ proc waku_get_peerids_from_peerstore(
proc waku_get_connected_peers(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -559,6 +579,7 @@ proc waku_get_connected_peers(
proc waku_get_peerids_by_protocol(
ctx: ptr WakuContext, protocol: cstring, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -579,6 +600,7 @@ proc waku_store_query(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -592,6 +614,7 @@ proc waku_store_query(
proc waku_listen_addresses(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -610,6 +633,7 @@ proc waku_dns_discovery(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -627,6 +651,7 @@ proc waku_discv5_update_bootnodes(
): cint {.dynlib, exportc.} =
## Updates the bootnode list used for discovering new peers via DiscoveryV5
## bootnodes - JSON array containing the bootnode ENRs i.e. `["enr:...", "enr:..."]`
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -642,6 +667,7 @@ proc waku_discv5_update_bootnodes(
proc waku_get_my_enr(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -655,6 +681,7 @@ proc waku_get_my_enr(
proc waku_get_my_peerid(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -668,6 +695,7 @@ proc waku_get_my_peerid(
proc waku_start_discv5(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -681,6 +709,7 @@ proc waku_start_discv5(
proc waku_stop_discv5(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -694,6 +723,7 @@ proc waku_stop_discv5(
proc waku_peer_exchange_request(
ctx: ptr WakuContext, numPeers: uint64, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand All @@ -711,6 +741,7 @@ proc waku_ping_peer(
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibwakuParams(ctx, callback, userData)

handleRequest(
Expand Down

0 comments on commit 2267f1b

Please sign in to comment.