Merge pull request #399 from napi-rs/useless-aquire-tsfn
fix(napi): remove unexpected napi_acquire_threadsafe_function
This commit is contained in:
commit
e89209cccd
3 changed files with 14 additions and 14 deletions
|
@ -20,7 +20,7 @@ napi5 = ["napi4", "napi-sys/napi5"]
|
||||||
napi6 = ["napi5", "napi-sys/napi6"]
|
napi6 = ["napi5", "napi-sys/napi6"]
|
||||||
napi7 = ["napi6", "napi-sys/napi7"]
|
napi7 = ["napi6", "napi-sys/napi7"]
|
||||||
serde-json = ["serde", "serde_json"]
|
serde-json = ["serde", "serde_json"]
|
||||||
tokio_rt = ["futures", "tokio", "once_cell"]
|
tokio_rt = ["futures", "tokio", "once_cell", "napi4"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
napi-sys = {version = "1", path = "../sys"}
|
napi-sys = {version = "1", path = "../sys"}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use futures::prelude::*;
|
|
||||||
use std::os::raw::{c_char, c_void};
|
use std::os::raw::{c_char, c_void};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
|
use futures::prelude::*;
|
||||||
|
|
||||||
use crate::{check_status, sys, Env, JsError, NapiValue, Result};
|
use crate::{check_status, sys, Env, JsError, NapiValue, Result};
|
||||||
|
|
||||||
pub struct FuturePromise<T, V: NapiValue> {
|
pub struct FuturePromise<T, V: NapiValue> {
|
||||||
|
@ -45,7 +46,6 @@ impl<T, V: NapiValue> FuturePromise<T, V> {
|
||||||
pub(crate) fn start(self) -> Result<TSFNValue> {
|
pub(crate) fn start(self) -> Result<TSFNValue> {
|
||||||
let mut tsfn_value = ptr::null_mut();
|
let mut tsfn_value = ptr::null_mut();
|
||||||
let async_resource_name = self.async_resource_name;
|
let async_resource_name = self.async_resource_name;
|
||||||
let initial_thread_count = 1;
|
|
||||||
let env = self.env;
|
let env = self.env;
|
||||||
let self_ref = Box::leak(Box::from(self));
|
let self_ref = Box::leak(Box::from(self));
|
||||||
check_status!(unsafe {
|
check_status!(unsafe {
|
||||||
|
@ -55,7 +55,7 @@ impl<T, V: NapiValue> FuturePromise<T, V> {
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
async_resource_name,
|
async_resource_name,
|
||||||
0,
|
0,
|
||||||
initial_thread_count,
|
1,
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
None,
|
None,
|
||||||
self_ref as *mut _ as *mut c_void,
|
self_ref as *mut _ as *mut c_void,
|
||||||
|
@ -72,14 +72,12 @@ pub(crate) struct TSFNValue(sys::napi_threadsafe_function);
|
||||||
|
|
||||||
unsafe impl Send for TSFNValue {}
|
unsafe impl Send for TSFNValue {}
|
||||||
|
|
||||||
#[inline]
|
#[inline(always)]
|
||||||
pub(crate) async fn resolve_from_future<T: Send, F: Future<Output = Result<T>>>(
|
pub(crate) async fn resolve_from_future<T: Send, F: Future<Output = Result<T>>>(
|
||||||
tsfn_value: TSFNValue,
|
tsfn_value: TSFNValue,
|
||||||
fut: F,
|
fut: F,
|
||||||
) {
|
) {
|
||||||
let val = fut.await;
|
let val = fut.await;
|
||||||
check_status!(unsafe { sys::napi_acquire_threadsafe_function(tsfn_value.0) })
|
|
||||||
.expect("Failed to acquire thread safe function");
|
|
||||||
check_status!(unsafe {
|
check_status!(unsafe {
|
||||||
sys::napi_call_threadsafe_function(
|
sys::napi_call_threadsafe_function(
|
||||||
tsfn_value.0,
|
tsfn_value.0,
|
||||||
|
@ -88,6 +86,13 @@ pub(crate) async fn resolve_from_future<T: Send, F: Future<Output = Result<T>>>(
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.expect("Failed to call thread safe function");
|
.expect("Failed to call thread safe function");
|
||||||
|
check_status!(unsafe {
|
||||||
|
sys::napi_release_threadsafe_function(
|
||||||
|
tsfn_value.0,
|
||||||
|
sys::napi_threadsafe_function_release_mode::napi_tsfn_release,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.expect("Failed to release thread safe function");
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn call_js_cb<T, V: NapiValue>(
|
unsafe extern "C" fn call_js_cb<T, V: NapiValue>(
|
||||||
|
@ -101,7 +106,6 @@ unsafe extern "C" fn call_js_cb<T, V: NapiValue>(
|
||||||
let value: Result<T> = ptr::read(data as *const _);
|
let value: Result<T> = ptr::read(data as *const _);
|
||||||
let resolver = future_promise.resolver;
|
let resolver = future_promise.resolver;
|
||||||
let deferred = future_promise.deferred;
|
let deferred = future_promise.deferred;
|
||||||
let tsfn = future_promise.tsfn;
|
|
||||||
let js_value_to_resolve = value.and_then(move |v| (resolver)(&mut env, v));
|
let js_value_to_resolve = value.and_then(move |v| (resolver)(&mut env, v));
|
||||||
match js_value_to_resolve {
|
match js_value_to_resolve {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
|
@ -114,9 +118,4 @@ unsafe extern "C" fn call_js_cb<T, V: NapiValue>(
|
||||||
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
|
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
check_status!(sys::napi_release_threadsafe_function(
|
|
||||||
tsfn,
|
|
||||||
sys::napi_threadsafe_function_release_mode::napi_tsfn_release,
|
|
||||||
))
|
|
||||||
.expect("Release threadsafe function failed");
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,10 @@ pub(crate) enum Message {
|
||||||
Shutdown,
|
Shutdown,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static SENDER: OnceCell<mpsc::Sender<Message>> = OnceCell::new();
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn get_tokio_sender() -> &'static mpsc::Sender<Message> {
|
pub(crate) fn get_tokio_sender() -> &'static mpsc::Sender<Message> {
|
||||||
static SENDER: OnceCell<mpsc::Sender<Message>> = OnceCell::new();
|
|
||||||
SENDER.get_or_init(|| {
|
SENDER.get_or_init(|| {
|
||||||
let buffer_size = var("NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE")
|
let buffer_size = var("NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE")
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
|
Loading…
Reference in a new issue