nodejs / node-addon-api

Module for using Node-API from C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ThreadSafeFunction call does nothing

Symbitic opened this issue · comments

commented

I have a problem with TSFN BlockingCall and NonBlockingCall not executing.

typedef void* device_t;
std::vector<std::shared_ptr<Napi::ThreadSafeFunction>> device_tsfn_vector;
std::mutex device_tsfn_mutex;

void DeviceCallback(device_t device, void *userdata) {
  std::cout << "DeviceCallback\n";
  Napi::ThreadSafeFunction tsfn = *reinterpret_cast<Napi::ThreadSafeFunction *>(userdata);
  auto callback = [](Napi::Env env, Napi::Function jsCallback) {
    std::cout << "callback\n";
  };
  tsfn.NonBlockingCall(callback);
}

Napi::Value DeviceWrapper::SetCallback(const Napi::CallbackInfo &info) {
  bool lossless;
  Napi::Env env = info.Env();
  device_t device = reinterpret_cast<device_t>(info[0].As<Napi::BigInt>().Uint64Value(&lossless));
  Napi::Function callback = info[1].As<Napi::Function>();

  auto tsfn_ptr = std::make_shared<Napi::ThreadSafeFunction>(
    Napi::ThreadSafeFunction::New(env, callback, "SetCallback", 0, 1)
  );

  {
    std::unique_lock<std::mutex> lock(device_tsfn_mutex);
    device_tsfn_vector.push_back(tsfn_ptr);
  }

  device_set_callback(device, DeviceCallback, tsfn_ptr.get());
  return env.Null();
}

I've verified this exact same thing works in another file (AdapterWrapper); just not here. It also prints "DeviceCallback" so I know it is executing.
Can someone please help me figure out what's wrong?

What is the return value of tsfn.NonBlockingCall(callback)? Is it napi_ok?

commented

Return value is 0, so yes.