From c28f035f5cfc97c7e0f2174a4e5519abc67ca502 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 4 Jan 2021 14:56:32 +0800 Subject: [PATCH 1/2] fix(napi): remove unexpected napi_acquire_threadsafe_function --- napi/src/promise.rs | 23 +++++++++++------------ napi/src/tokio_rt.rs | 3 ++- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/napi/src/promise.rs b/napi/src/promise.rs index 4ab3d4f0..5875c7c4 100644 --- a/napi/src/promise.rs +++ b/napi/src/promise.rs @@ -1,7 +1,8 @@ -use futures::prelude::*; use std::os::raw::{c_char, c_void}; use std::ptr; +use futures::prelude::*; + use crate::{check_status, sys, Env, JsError, NapiValue, Result}; pub struct FuturePromise { @@ -45,7 +46,6 @@ impl FuturePromise { pub(crate) fn start(self) -> Result { let mut tsfn_value = ptr::null_mut(); let async_resource_name = self.async_resource_name; - let initial_thread_count = 1; let env = self.env; let self_ref = Box::leak(Box::from(self)); check_status!(unsafe { @@ -55,7 +55,7 @@ impl FuturePromise { ptr::null_mut(), async_resource_name, 0, - initial_thread_count, + 1, ptr::null_mut(), None, 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 {} -#[inline] +#[inline(always)] pub(crate) async fn resolve_from_future>>( tsfn_value: TSFNValue, fut: F, ) { 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 { sys::napi_call_threadsafe_function( tsfn_value.0, @@ -88,6 +86,13 @@ pub(crate) async fn resolve_from_future>>( ) }) .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( @@ -101,7 +106,6 @@ unsafe extern "C" fn call_js_cb( let value: Result = ptr::read(data as *const _); let resolver = future_promise.resolver; let deferred = future_promise.deferred; - let tsfn = future_promise.tsfn; let js_value_to_resolve = value.and_then(move |v| (resolver)(&mut env, v)); match js_value_to_resolve { Ok(v) => { @@ -114,9 +118,4 @@ unsafe extern "C" fn call_js_cb( 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"); } diff --git a/napi/src/tokio_rt.rs b/napi/src/tokio_rt.rs index 54069506..38bdc583 100644 --- a/napi/src/tokio_rt.rs +++ b/napi/src/tokio_rt.rs @@ -16,9 +16,10 @@ pub(crate) enum Message { Shutdown, } +static SENDER: OnceCell> = OnceCell::new(); + #[inline] pub(crate) fn get_tokio_sender() -> &'static mpsc::Sender { - static SENDER: OnceCell> = OnceCell::new(); SENDER.get_or_init(|| { let buffer_size = var("NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE") .map_err(|_| ()) From 0c9981beb762e547a94fa717e4693077f717078c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 4 Jan 2021 14:57:45 +0800 Subject: [PATCH 2/2] fix(napi): enable napi4 when tokio_rt enabled --- napi/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 9c78cc7b..1cfd9242 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -20,7 +20,7 @@ napi5 = ["napi4", "napi-sys/napi5"] napi6 = ["napi5", "napi-sys/napi6"] napi7 = ["napi6", "napi-sys/napi7"] serde-json = ["serde", "serde_json"] -tokio_rt = ["futures", "tokio", "once_cell"] +tokio_rt = ["futures", "tokio", "once_cell", "napi4"] [dependencies] napi-sys = {version = "1", path = "../sys"}