From 94c70fd71971f640447c95764aaf873e18070405 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 15 Dec 2023 04:16:57 +0530 Subject: [PATCH] chore(node-api): reuse SendPtr (#21567) Pending review items from https://github.com/denoland/deno/pull/21406 --- cli/napi/async.rs | 5 +---- cli/napi/threadsafe_functions.rs | 14 ++++++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cli/napi/async.rs b/cli/napi/async.rs index 1fb0c6374c..8add67e1a5 100644 --- a/cli/napi/async.rs +++ b/cli/napi/async.rs @@ -3,6 +3,7 @@ use deno_runtime::deno_napi::*; use crate::check_env; +use crate::napi::threadsafe_functions::SendPtr; #[repr(C)] pub struct AsyncWork { @@ -64,10 +65,6 @@ fn napi_queue_async_work( return napi_invalid_arg; }; - #[repr(transparent)] - struct SendPtr(*const T); - unsafe impl Send for SendPtr {} - unsafe impl Sync for SendPtr {} let send_env = SendPtr(env_ptr); #[inline(always)] diff --git a/cli/napi/threadsafe_functions.rs b/cli/napi/threadsafe_functions.rs index 15395529d8..96cce7749e 100644 --- a/cli/napi/threadsafe_functions.rs +++ b/cli/napi/threadsafe_functions.rs @@ -9,6 +9,12 @@ use std::ptr::NonNull; use std::sync::atomic::AtomicUsize; use std::sync::Arc; +#[repr(transparent)] +pub struct SendPtr(pub *const T); + +unsafe impl Send for SendPtr {} +unsafe impl Sync for SendPtr {} + static TS_FN_ID_COUNTER: Lazy = Lazy::new(|| AtomicUsize::new(0)); pub struct TsFn { @@ -86,11 +92,6 @@ impl TsFn { pub fn call(&self, data: *mut c_void, is_blocking: bool) { let js_func = self.maybe_func.clone(); - #[repr(transparent)] - struct SendPtr(*const T); - unsafe impl Send for SendPtr {} - unsafe impl Sync for SendPtr {} - let env = SendPtr(self.env); let context = SendPtr(self.context); let data = SendPtr(data); @@ -146,7 +147,8 @@ impl TsFn { context: SendPtr, data: SendPtr, ) { - // SAFETY: We're calling the provided callback with valid args + // SAFETY: env is valid for the duration of the callback. + // data lifetime is users responsibility. unsafe { call_js_cb( env.0 as _,