summaryrefslogtreecommitdiff
path: root/cli/napi/threadsafe_functions.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2023-05-18 19:15:47 +0530
committerGitHub <noreply@github.com>2023-05-18 19:15:47 +0530
commitc3f7e6ed6ea2da6b8be5c7b29619eb51e0c9a6e3 (patch)
treedee07dbadf9dfe69bbfac707dde344f9528ef05a /cli/napi/threadsafe_functions.rs
parent695b5de6cb0cb4a10b95cbae99f2f19e5621a9eb (diff)
fix(cli/napi): handle finalizers (#19168)
Fixes https://github.com/denoland/deno/issues/17325
Diffstat (limited to 'cli/napi/threadsafe_functions.rs')
-rw-r--r--cli/napi/threadsafe_functions.rs16
1 files changed, 13 insertions, 3 deletions
diff --git a/cli/napi/threadsafe_functions.rs b/cli/napi/threadsafe_functions.rs
index 119ee81da..0168c98d5 100644
--- a/cli/napi/threadsafe_functions.rs
+++ b/cli/napi/threadsafe_functions.rs
@@ -18,6 +18,8 @@ pub struct TsFn {
pub context: *mut c_void,
pub thread_counter: usize,
pub ref_counter: Arc<AtomicUsize>,
+ finalizer: Option<napi_finalize>,
+ finalizer_data: *mut c_void,
sender: mpsc::UnboundedSender<PendingNapiAsyncWork>,
tsfn_sender: mpsc::UnboundedSender<ThreadSafeFunctionStatus>,
}
@@ -25,7 +27,12 @@ pub struct TsFn {
impl Drop for TsFn {
fn drop(&mut self) {
let env = unsafe { self.env.as_mut().unwrap() };
- env.remove_threadsafe_function_ref_counter(self.id)
+ env.remove_threadsafe_function_ref_counter(self.id);
+ if let Some(finalizer) = self.finalizer {
+ unsafe {
+ (finalizer)(self.env as _, self.finalizer_data, ptr::null_mut());
+ }
+ }
}
}
@@ -126,8 +133,8 @@ fn napi_create_threadsafe_function(
_async_resource_name: napi_value,
_max_queue_size: usize,
initial_thread_count: usize,
- _thread_finialize_data: *mut c_void,
- _thread_finalize_cb: napi_finalize,
+ thread_finialize_data: *mut c_void,
+ thread_finalize_cb: Option<napi_finalize>,
context: *mut c_void,
maybe_call_js_cb: Option<napi_threadsafe_function_call_js>,
result: *mut napi_threadsafe_function,
@@ -153,10 +160,13 @@ fn napi_create_threadsafe_function(
context,
thread_counter: initial_thread_count,
sender: env_ref.async_work_sender.clone(),
+ finalizer: thread_finalize_cb,
+ finalizer_data: thread_finialize_data,
tsfn_sender: env_ref.threadsafe_function_sender.clone(),
ref_counter: Arc::new(AtomicUsize::new(1)),
env,
};
+
env_ref
.add_threadsafe_function_ref_counter(tsfn.id, tsfn.ref_counter.clone());