88
99import std/ [atomics, locks, options, tables]
1010import chronicles, chronos, chronos/ threadsync, taskpools/ channels_spsc_single, results
11- import ./ ffi_types, ./ ffi_events, ./ ffi_thread_request, ./ logging, ./ cbor_serial
11+ import
12+ ./ ffi_types,
13+ ./ ffi_events,
14+ ./ ffi_handles,
15+ ./ ffi_thread_request,
16+ ./ logging,
17+ ./ cbor_serial
1218
13- export ffi_events
19+ export ffi_events, ffi_handles
1420
1521type FFIContext * [T] = object
1622 myLib* : ptr T # main library object (Waku, LibP2P, SDS, …)
@@ -27,6 +33,7 @@ type FFIContext*[T] = object
2733 eventThreadExitSignal: ThreadSignalPtr # mirrors threadExitSignal for the event thread
2834 userData* : pointer
2935 eventRegistry* : FFIEventRegistry
36+ handles* : FFIHandleRegistry # live {.ffiHandle.} objects, keyed by uint64 id
3037 eventQueue* : EventQueue
3138 ffiHeartbeat* : Atomic [int64 ]
3239 # advanced each FFI-thread loop; event thread reads for liveness
@@ -57,6 +64,7 @@ proc deinitContextResources*[T](ctx: ptr FFIContext[T]): Result[void, string] =
5764 # # fields are nil'd after close so re-init on the same slot is safe.
5865 ctx.lock.deinitLock ()
5966 deinitEventRegistry (ctx[].eventRegistry)
67+ deinitHandleRegistry (ctx[].handles)
6068 deinitEventQueue (ctx[].eventQueue)
6169 when defined (gcRefc):
6270 # ThreadSignalPtr.close() under refc traps in safeUnregisterAndCloseFd
@@ -95,6 +103,7 @@ proc initContextResources*[T](ctx: ptr FFIContext[T]): Result[void, string] =
95103 ctx.eventThreadExitSignal = nil
96104 ctx.lock.initLock ()
97105 initEventRegistry (ctx[].eventRegistry)
106+ initHandleRegistry (ctx[].handles)
98107 initEventQueue (ctx[].eventQueue)
99108 ctx.ffiHeartbeat.store (0 )
100109 ctx.eventQueueStuck.store (false )
0 commit comments