Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
msgpack_client_async.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "napi.h"
5#include <atomic>
6#include <mutex>
7#include <thread>
8
10
27class MsgpackClientAsync : public Napi::ObjectWrap<MsgpackClientAsync> {
28 public:
29 MsgpackClientAsync(const Napi::CallbackInfo& info);
30
38 Napi::Value setResponseCallback(const Napi::CallbackInfo& info);
39
47 Napi::Value call(const Napi::CallbackInfo& info);
48
53 Napi::Value acquire(const Napi::CallbackInfo& info);
54
59 Napi::Value release(const Napi::CallbackInfo& info);
60
61 static Napi::Function get_class(Napi::Env env);
62
63 private:
71 void poll_responses();
72
73 // IPC client for shared memory communication
74 std::unique_ptr<bb::ipc::IpcClient> client_;
75
76 // Background polling thread (detached - will be cleaned up by OS on process exit)
77 std::thread poll_thread_;
78
79 // Mutex protecting TSFN access from multiple threads
80 std::mutex tsfn_mutex_;
81
82 // JavaScript callback stored for lazy TSFN creation
83 Napi::FunctionReference js_callback_;
84
85 // ThreadSafeFunction for invoking JavaScript callback from background thread
86 // Created lazily when first needed, destroyed when no longer needed
87 Napi::ThreadSafeFunction tsfn_;
88
89 // Reference count for TSFN lifecycle management
90 // When 0→1: create TSFN, when 1→0: destroy TSFN
91 int ref_count_ = 0;
92};
93
94} // namespace bb::nodejs::msgpack_client
Asynchronous NAPI wrapper for msgpack calls via shared memory IPC.
static Napi::Function get_class(Napi::Env env)
void poll_responses()
Background thread function that polls for responses.
Napi::Value call(const Napi::CallbackInfo &info)
Send a msgpack buffer asynchronously.
Napi::Value release(const Napi::CallbackInfo &info)
Release a reference to allow the event loop to exit Called by TypeScript when there are no pending ca...
Napi::Value setResponseCallback(const Napi::CallbackInfo &info)
Set the JavaScript callback to be invoked when responses arrive.
std::unique_ptr< bb::ipc::IpcClient > client_
Napi::Value acquire(const Napi::CallbackInfo &info)
Acquire a reference to keep the event loop alive Called by TypeScript when there are pending callback...
void info(Args... args)
Definition log.hpp:75