mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-06-21 00:40:16 +00:00
fix: last nits
This commit is contained in:
parent
d6785da874
commit
1897a30384
@ -47,7 +47,8 @@ proc emitLivenessEvent[T, P](ctx: ptr FFIContext[T], name: string, payload: P) =
|
||||
chronicles.error "liveness event encode failed", name = name, err = exc.msg
|
||||
return
|
||||
let dataPtr: pointer =
|
||||
if event.len > 0: unsafeAddr event[0] else: nil
|
||||
if event.len > 0: cast[pointer](unsafeAddr event[0])
|
||||
else: cast[pointer](emptyListenerPayload)
|
||||
ctx.dispatchToListeners(name, dataPtr, event.len)
|
||||
|
||||
proc onNotResponding*(ctx: ptr FFIContext) =
|
||||
|
||||
@ -106,7 +106,7 @@ proc deinitContextResources*[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
if not ctx.eventThreadExitSignal.isNil():
|
||||
?ctx.eventThreadExitSignal.close()
|
||||
ctx.eventThreadExitSignal = nil
|
||||
ok()
|
||||
return ok()
|
||||
|
||||
proc cleanUpResources[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
## Full cleanup for heap-allocated contexts: closes all resources and frees memory.
|
||||
@ -178,7 +178,7 @@ proc initContextResources*[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
return err("failed to create the event thread: " & getCurrentExceptionMsg())
|
||||
|
||||
success = true
|
||||
ok()
|
||||
return ok()
|
||||
|
||||
proc signalStop*[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
# Error paths intentionally skip onNotResponding: a back-pressuring
|
||||
@ -199,7 +199,7 @@ proc signalStop*[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
error "failed to signal eventQueueSignal in signalStop", error = evtSignaled.error
|
||||
elif evtSignaled.get() == false:
|
||||
error "failed to signal eventQueueSignal on time in signalStop"
|
||||
ok()
|
||||
return ok()
|
||||
|
||||
## If the FFI thread's event loop is blocked by a synchronous handler
|
||||
## (e.g. blocking I/O), it cannot process reqSignal in time to exit.
|
||||
@ -233,7 +233,7 @@ proc stopAndJoinThreads*[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
return err("event thread did not exit in time; leaking ctx to avoid hang")
|
||||
|
||||
joinThread(ctx.eventThread)
|
||||
ok()
|
||||
return ok()
|
||||
|
||||
proc clearContext[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
## Stops the FFI context that was created via createFFIContext[T]() (heap).
|
||||
@ -241,4 +241,4 @@ proc clearContext[T](ctx: ptr FFIContext[T]): Result[void, string] =
|
||||
return err("clearContext: " & $error)
|
||||
ctx.cleanUpResources().isOkOr:
|
||||
return err("cleanUpResources failed: " & $error)
|
||||
ok()
|
||||
return ok()
|
||||
|
||||
@ -203,7 +203,7 @@ proc eventQueueLen*(q: var EventQueue): int {.raises: [], gcsafe.} =
|
||||
return q.count
|
||||
|
||||
|
||||
const emptyListenerPayload: cstring = ""
|
||||
const emptyListenerPayload*: cstring = ""
|
||||
## Non-nil zero-length buffer handed to listeners when a payload is
|
||||
## empty, so a consumer doing `std::string(data, len)` / `memcpy` never
|
||||
## receives a nil pointer (which is UB even at len 0).
|
||||
@ -225,7 +225,8 @@ proc notifyListenersErr*(listeners: seq[FFIEventListener], msg: string) =
|
||||
## Error fan-out: adapts the message string to `notifyListeners`, which
|
||||
## supplies the non-nil pointer for the empty-message case.
|
||||
let p =
|
||||
if msg.len > 0: cast[pointer](unsafeAddr msg[0]) else: nil
|
||||
if msg.len > 0: cast[pointer](unsafeAddr msg[0])
|
||||
else: cast[pointer](emptyListenerPayload)
|
||||
notifyListeners(listeners, RET_ERR, p, msg.len)
|
||||
|
||||
var ffiCurrentEventRegistry* {.threadvar.}: ptr FFIEventRegistry
|
||||
@ -266,7 +267,8 @@ template dispatchFFIEvent*(eventName: string, body: untyped) =
|
||||
withFFIEventDispatch(eventName, listeners):
|
||||
let event = body
|
||||
let dataPtr: pointer =
|
||||
if event.len > 0: unsafeAddr event[0] else: nil
|
||||
if event.len > 0: cast[pointer](unsafeAddr event[0])
|
||||
else: cast[pointer](emptyListenerPayload)
|
||||
notifyListeners(listeners, RET_OK, dataPtr, event.len)
|
||||
|
||||
template dispatchFFIEventCbor*(eventName: string, eventPayload: typed) =
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user