2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
use deno_core::futures::channel::mpsc;
|
2023-12-11 12:52:55 -05:00
|
|
|
use deno_core::V8CrossThreadTaskSpawner;
|
2022-10-05 10:06:44 -04:00
|
|
|
use deno_runtime::deno_napi::*;
|
2023-01-12 07:47:55 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2022-10-05 10:06:44 -04:00
|
|
|
use std::mem::forget;
|
2023-12-11 12:52:55 -05:00
|
|
|
use std::ptr::NonNull;
|
2023-01-12 07:47:55 -05:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-12-14 17:46:57 -05:00
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct SendPtr<T>(pub *const T);
|
|
|
|
|
|
|
|
unsafe impl<T> Send for SendPtr<T> {}
|
|
|
|
unsafe impl<T> Sync for SendPtr<T> {}
|
|
|
|
|
2023-01-12 07:47:55 -05:00
|
|
|
static TS_FN_ID_COUNTER: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
pub struct TsFn {
|
2023-01-12 07:47:55 -05:00
|
|
|
pub id: usize,
|
2022-10-05 10:06:44 -04:00
|
|
|
pub env: *mut Env,
|
|
|
|
pub maybe_func: Option<v8::Global<v8::Function>>,
|
|
|
|
pub maybe_call_js_cb: Option<napi_threadsafe_function_call_js>,
|
|
|
|
pub context: *mut c_void,
|
|
|
|
pub thread_counter: usize,
|
2023-01-12 07:47:55 -05:00
|
|
|
pub ref_counter: Arc<AtomicUsize>,
|
2023-05-18 09:45:47 -04:00
|
|
|
finalizer: Option<napi_finalize>,
|
|
|
|
finalizer_data: *mut c_void,
|
2023-12-11 12:52:55 -05:00
|
|
|
sender: V8CrossThreadTaskSpawner,
|
2022-10-05 10:06:44 -04:00
|
|
|
tsfn_sender: mpsc::UnboundedSender<ThreadSafeFunctionStatus>,
|
|
|
|
}
|
|
|
|
|
2023-01-12 07:47:55 -05:00
|
|
|
impl Drop for TsFn {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let env = unsafe { self.env.as_mut().unwrap() };
|
2023-05-18 09:45:47 -04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 07:47:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
impl TsFn {
|
2023-05-18 10:17:53 -04:00
|
|
|
pub fn acquire(&mut self) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
self.thread_counter += 1;
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
pub fn release(mut self) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
self.thread_counter -= 1;
|
|
|
|
if self.thread_counter == 0 {
|
2023-05-18 10:17:53 -04:00
|
|
|
if self
|
2022-10-05 10:06:44 -04:00
|
|
|
.tsfn_sender
|
|
|
|
.unbounded_send(ThreadSafeFunctionStatus::Dead)
|
2023-05-18 10:17:53 -04:00
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return napi_generic_failure;
|
|
|
|
}
|
2022-10-05 10:06:44 -04:00
|
|
|
drop(self);
|
|
|
|
} else {
|
|
|
|
forget(self);
|
|
|
|
}
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
pub fn ref_(&mut self) -> napi_status {
|
2023-01-12 07:47:55 -05:00
|
|
|
self
|
|
|
|
.ref_counter
|
|
|
|
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2023-01-12 07:47:55 -05:00
|
|
|
}
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
pub fn unref(&mut self) -> napi_status {
|
2023-01-12 07:47:55 -05:00
|
|
|
let _ = self.ref_counter.fetch_update(
|
|
|
|
std::sync::atomic::Ordering::SeqCst,
|
|
|
|
std::sync::atomic::Ordering::SeqCst,
|
|
|
|
|x| {
|
|
|
|
if x == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(x - 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2023-01-12 07:47:55 -05:00
|
|
|
}
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
pub fn call(&self, data: *mut c_void, is_blocking: bool) {
|
|
|
|
let js_func = self.maybe_func.clone();
|
2023-12-11 12:52:55 -05:00
|
|
|
|
|
|
|
let env = SendPtr(self.env);
|
|
|
|
let context = SendPtr(self.context);
|
|
|
|
let data = SendPtr(data);
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn spawn(
|
|
|
|
sender: &V8CrossThreadTaskSpawner,
|
|
|
|
is_blocking: bool,
|
|
|
|
f: impl FnOnce(&mut v8::HandleScope) + Send + 'static,
|
|
|
|
) {
|
|
|
|
if is_blocking {
|
|
|
|
sender.spawn_blocking(f);
|
|
|
|
} else {
|
|
|
|
sender.spawn(f);
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
if let Some(call_js_cb) = self.maybe_call_js_cb {
|
2023-12-11 12:52:55 -05:00
|
|
|
if let Some(func) = js_func {
|
|
|
|
let func = SendPtr(func.into_raw().as_ptr());
|
|
|
|
#[inline(always)]
|
|
|
|
fn call(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
call_js_cb: napi_threadsafe_function_call_js,
|
|
|
|
func: SendPtr<v8::Function>,
|
|
|
|
env: SendPtr<Env>,
|
|
|
|
context: SendPtr<c_void>,
|
|
|
|
data: SendPtr<c_void>,
|
|
|
|
) {
|
|
|
|
// SAFETY: This is a valid global from above
|
|
|
|
let func: v8::Global<v8::Function> = unsafe {
|
|
|
|
v8::Global::<v8::Function>::from_raw(
|
|
|
|
scope,
|
|
|
|
NonNull::new_unchecked(func.0 as _),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let func: v8::Local<v8::Value> =
|
|
|
|
func.open(scope).to_object(scope).unwrap().into();
|
|
|
|
// SAFETY: env is valid for the duration of the callback.
|
|
|
|
// data lifetime is users responsibility.
|
|
|
|
unsafe {
|
|
|
|
call_js_cb(env.0 as _, func.into(), context.0 as _, data.0 as _)
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
2023-12-11 12:52:55 -05:00
|
|
|
}
|
|
|
|
spawn(&self.sender, is_blocking, move |scope| {
|
|
|
|
call(scope, call_js_cb, func, env, context, data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
#[inline(always)]
|
|
|
|
fn call(
|
|
|
|
call_js_cb: napi_threadsafe_function_call_js,
|
|
|
|
env: SendPtr<Env>,
|
|
|
|
context: SendPtr<c_void>,
|
|
|
|
data: SendPtr<c_void>,
|
|
|
|
) {
|
2023-12-14 17:46:57 -05:00
|
|
|
// SAFETY: env is valid for the duration of the callback.
|
|
|
|
// data lifetime is users responsibility.
|
2023-12-11 12:52:55 -05:00
|
|
|
unsafe {
|
|
|
|
call_js_cb(
|
|
|
|
env.0 as _,
|
|
|
|
std::mem::zeroed(),
|
|
|
|
context.0 as _,
|
|
|
|
data.0 as _,
|
|
|
|
)
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-12-11 12:52:55 -05:00
|
|
|
spawn(&self.sender, is_blocking, move |_| {
|
|
|
|
call(call_js_cb, env, context, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
spawn(&self.sender, is_blocking, |_| {
|
2022-10-05 10:06:44 -04:00
|
|
|
// TODO: func.call
|
|
|
|
});
|
2023-12-11 12:52:55 -05:00
|
|
|
};
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_create_threadsafe_function(
|
|
|
|
env: *mut Env,
|
|
|
|
func: napi_value,
|
|
|
|
_async_resource: napi_value,
|
|
|
|
_async_resource_name: napi_value,
|
|
|
|
_max_queue_size: usize,
|
|
|
|
initial_thread_count: usize,
|
2023-06-26 09:10:27 -04:00
|
|
|
thread_finalize_data: *mut c_void,
|
2023-05-18 09:45:47 -04:00
|
|
|
thread_finalize_cb: Option<napi_finalize>,
|
2022-10-05 10:06:44 -04:00
|
|
|
context: *mut c_void,
|
|
|
|
maybe_call_js_cb: Option<napi_threadsafe_function_call_js>,
|
|
|
|
result: *mut napi_threadsafe_function,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
|
|
|
let Some(env_ref) = env.as_mut() else {
|
|
|
|
return napi_generic_failure;
|
|
|
|
};
|
2022-10-05 10:06:44 -04:00
|
|
|
if initial_thread_count == 0 {
|
2023-05-18 10:17:53 -04:00
|
|
|
return napi_invalid_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut maybe_func = None;
|
|
|
|
|
|
|
|
if let Some(value) = *func {
|
|
|
|
let Ok(func) = v8::Local::<v8::Function>::try_from(value) else {
|
|
|
|
return napi_function_expected;
|
|
|
|
};
|
|
|
|
maybe_func = Some(v8::Global::new(&mut env_ref.scope(), func));
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
2023-01-12 07:47:55 -05:00
|
|
|
let id = TS_FN_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
let tsfn = TsFn {
|
2023-01-12 07:47:55 -05:00
|
|
|
id,
|
2022-10-05 10:06:44 -04:00
|
|
|
maybe_func,
|
|
|
|
maybe_call_js_cb,
|
|
|
|
context,
|
|
|
|
thread_counter: initial_thread_count,
|
|
|
|
sender: env_ref.async_work_sender.clone(),
|
2023-05-18 09:45:47 -04:00
|
|
|
finalizer: thread_finalize_cb,
|
2023-06-26 09:10:27 -04:00
|
|
|
finalizer_data: thread_finalize_data,
|
2022-10-05 10:06:44 -04:00
|
|
|
tsfn_sender: env_ref.threadsafe_function_sender.clone(),
|
2023-01-12 07:47:55 -05:00
|
|
|
ref_counter: Arc::new(AtomicUsize::new(1)),
|
2022-10-05 10:06:44 -04:00
|
|
|
env,
|
|
|
|
};
|
2023-05-18 09:45:47 -04:00
|
|
|
|
2023-01-12 07:47:55 -05:00
|
|
|
env_ref
|
|
|
|
.add_threadsafe_function_ref_counter(tsfn.id, tsfn.ref_counter.clone());
|
2022-10-05 10:06:44 -04:00
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
if env_ref
|
2022-10-05 10:06:44 -04:00
|
|
|
.threadsafe_function_sender
|
|
|
|
.unbounded_send(ThreadSafeFunctionStatus::Alive)
|
2023-05-18 10:17:53 -04:00
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return napi_generic_failure;
|
|
|
|
}
|
2022-10-05 10:06:44 -04:00
|
|
|
*result = transmute::<Box<TsFn>, _>(Box::new(tsfn));
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_acquire_threadsafe_function(
|
|
|
|
tsfn: napi_threadsafe_function,
|
|
|
|
_mode: napi_threadsafe_function_release_mode,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
let tsfn: &mut TsFn = &mut *(tsfn as *mut TsFn);
|
2023-05-18 10:17:53 -04:00
|
|
|
tsfn.acquire()
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_unref_threadsafe_function(
|
|
|
|
_env: &mut Env,
|
|
|
|
tsfn: napi_threadsafe_function,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2023-01-12 07:47:55 -05:00
|
|
|
let tsfn: &mut TsFn = &mut *(tsfn as *mut TsFn);
|
2023-05-18 10:17:53 -04:00
|
|
|
tsfn.unref()
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Maybe called from any thread.
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
pub fn napi_get_threadsafe_function_context(
|
|
|
|
func: napi_threadsafe_function,
|
|
|
|
result: *mut *const c_void,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
let tsfn: &TsFn = &*(func as *const TsFn);
|
|
|
|
*result = tsfn.context;
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_call_threadsafe_function(
|
|
|
|
func: napi_threadsafe_function,
|
|
|
|
data: *mut c_void,
|
|
|
|
is_blocking: napi_threadsafe_function_call_mode,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
let tsfn: &TsFn = &*(func as *const TsFn);
|
|
|
|
tsfn.call(data, is_blocking != 0);
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
2023-01-12 07:47:55 -05:00
|
|
|
fn napi_ref_threadsafe_function(
|
|
|
|
_env: &mut Env,
|
|
|
|
func: napi_threadsafe_function,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2023-01-12 07:47:55 -05:00
|
|
|
let tsfn: &mut TsFn = &mut *(func as *mut TsFn);
|
2023-05-18 10:17:53 -04:00
|
|
|
tsfn.ref_()
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_release_threadsafe_function(
|
|
|
|
tsfn: napi_threadsafe_function,
|
|
|
|
_mode: napi_threadsafe_function_release_mode,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
let tsfn: Box<TsFn> = Box::from_raw(tsfn as *mut TsFn);
|
2023-05-18 10:17:53 -04:00
|
|
|
tsfn.release()
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|