2021-11-22 17:51:21 +09:00
|
|
|
use std::ffi::CStr;
|
2020-05-12 14:59:20 +09:00
|
|
|
use std::mem;
|
2021-11-02 21:36:34 +09:00
|
|
|
use std::os::raw::c_void;
|
2020-05-12 14:59:20 +09:00
|
|
|
use std::ptr;
|
2021-11-22 17:51:21 +09:00
|
|
|
use std::rc::Rc;
|
2021-11-04 19:44:06 +09:00
|
|
|
use std::sync::atomic::{AtomicU8, Ordering};
|
2020-05-12 14:59:20 +09:00
|
|
|
|
2021-04-01 19:50:38 +09:00
|
|
|
use crate::{
|
2021-11-02 21:36:34 +09:00
|
|
|
bindgen_runtime::ToNapiValue, check_status, js_values::NapiValue, sys, Env, JsError, JsObject,
|
|
|
|
Result, Task,
|
2021-04-01 19:50:38 +09:00
|
|
|
};
|
2020-05-12 14:59:20 +09:00
|
|
|
|
2020-09-26 19:12:52 +09:00
|
|
|
struct AsyncWork<T: Task> {
|
2020-05-12 14:59:20 +09:00
|
|
|
inner_task: T,
|
|
|
|
deferred: sys::napi_deferred,
|
2020-09-26 19:12:52 +09:00
|
|
|
value: Result<mem::MaybeUninit<T::Output>>,
|
|
|
|
napi_async_work: sys::napi_async_work,
|
2021-11-04 19:44:06 +09:00
|
|
|
status: Rc<AtomicU8>,
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
|
2021-11-02 21:36:34 +09:00
|
|
|
pub struct AsyncWorkPromise {
|
|
|
|
pub(crate) napi_async_work: sys::napi_async_work,
|
2020-09-26 19:12:52 +09:00
|
|
|
raw_promise: sys::napi_value,
|
2021-11-02 21:36:34 +09:00
|
|
|
pub(crate) deferred: sys::napi_deferred,
|
|
|
|
env: sys::napi_env,
|
2021-11-04 19:44:06 +09:00
|
|
|
/// share with AsyncWork
|
|
|
|
/// 0: not started
|
|
|
|
/// 1: completed
|
|
|
|
/// 2: canceled
|
|
|
|
pub(crate) status: Rc<AtomicU8>,
|
2020-09-26 19:12:52 +09:00
|
|
|
}
|
|
|
|
|
2021-11-02 21:36:34 +09:00
|
|
|
impl AsyncWorkPromise {
|
2020-09-26 19:12:52 +09:00
|
|
|
pub fn promise_object(&self) -> JsObject {
|
2021-11-02 21:36:34 +09:00
|
|
|
unsafe { JsObject::from_raw_unchecked(self.env, self.raw_promise) }
|
2020-09-26 19:12:52 +09:00
|
|
|
}
|
|
|
|
|
2021-11-02 21:36:34 +09:00
|
|
|
pub fn cancel(&self) -> Result<()> {
|
|
|
|
// must be happened in the main thread, relaxed is enough
|
2021-11-04 19:44:06 +09:00
|
|
|
self.status.store(2, Ordering::Relaxed);
|
2021-11-02 21:36:34 +09:00
|
|
|
check_status!(unsafe { sys::napi_cancel_async_work(self.env, self.napi_async_work) })
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:36:34 +09:00
|
|
|
pub fn run<T: Task>(
|
|
|
|
env: sys::napi_env,
|
|
|
|
task: T,
|
2021-11-04 19:44:06 +09:00
|
|
|
abort_status: Option<Rc<AtomicU8>>,
|
2021-11-02 21:36:34 +09:00
|
|
|
) -> Result<AsyncWorkPromise> {
|
2020-09-26 19:12:52 +09:00
|
|
|
let mut raw_resource = ptr::null_mut();
|
2021-11-02 21:36:34 +09:00
|
|
|
check_status!(unsafe { sys::napi_create_object(env, &mut raw_resource) })?;
|
2020-09-26 19:12:52 +09:00
|
|
|
let mut raw_promise = ptr::null_mut();
|
|
|
|
let mut deferred = ptr::null_mut();
|
2021-11-02 21:36:34 +09:00
|
|
|
check_status!(unsafe { sys::napi_create_promise(env, &mut deferred, &mut raw_promise) })?;
|
2021-11-04 19:44:06 +09:00
|
|
|
let task_status = abort_status.unwrap_or_else(|| Rc::new(AtomicU8::new(0)));
|
2020-09-26 19:12:52 +09:00
|
|
|
let result = Box::leak(Box::new(AsyncWork {
|
|
|
|
inner_task: task,
|
|
|
|
deferred,
|
|
|
|
value: Ok(mem::MaybeUninit::zeroed()),
|
|
|
|
napi_async_work: ptr::null_mut(),
|
2021-11-04 19:44:06 +09:00
|
|
|
status: task_status.clone(),
|
2020-09-26 19:12:52 +09:00
|
|
|
}));
|
2021-11-22 17:51:21 +09:00
|
|
|
let async_work_name = unsafe { CStr::from_bytes_with_nul_unchecked(b"napi_rs_async_work\0") };
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-09-26 19:12:52 +09:00
|
|
|
sys::napi_create_async_work(
|
2021-11-02 21:36:34 +09:00
|
|
|
env,
|
2020-09-26 19:12:52 +09:00
|
|
|
raw_resource,
|
2021-11-15 17:47:55 +09:00
|
|
|
async_work_name.as_ptr() as *mut _,
|
2020-09-26 19:12:52 +09:00
|
|
|
Some(execute::<T> as unsafe extern "C" fn(env: sys::napi_env, data: *mut c_void)),
|
|
|
|
Some(
|
|
|
|
complete::<T>
|
|
|
|
as unsafe extern "C" fn(env: sys::napi_env, status: sys::napi_status, data: *mut c_void),
|
|
|
|
),
|
|
|
|
result as *mut _ as *mut c_void,
|
|
|
|
&mut result.napi_async_work,
|
|
|
|
)
|
|
|
|
})?;
|
2021-11-02 21:36:34 +09:00
|
|
|
check_status!(unsafe { sys::napi_queue_async_work(env, result.napi_async_work) })?;
|
2020-09-26 19:12:52 +09:00
|
|
|
Ok(AsyncWorkPromise {
|
|
|
|
napi_async_work: result.napi_async_work,
|
|
|
|
raw_promise,
|
2021-11-02 21:36:34 +09:00
|
|
|
deferred,
|
2020-09-26 19:12:52 +09:00
|
|
|
env,
|
2021-11-04 19:44:06 +09:00
|
|
|
status: task_status,
|
2020-09-26 19:12:52 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-14 12:21:03 +09:00
|
|
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
2020-05-12 14:59:20 +09:00
|
|
|
unsafe impl<T: Task> Send for AsyncWork<T> {}
|
|
|
|
|
|
|
|
unsafe impl<T: Task> Sync for AsyncWork<T> {}
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
/// env here is the same with the one in `CallContext`.
|
|
|
|
/// So it actually could do nothing here, because `execute` function is called in the other thread mostly.
|
2020-05-12 14:59:20 +09:00
|
|
|
unsafe extern "C" fn execute<T: Task>(_env: sys::napi_env, data: *mut c_void) {
|
2022-01-13 12:15:02 +09:00
|
|
|
let mut work = unsafe { Box::from_raw(data as *mut AsyncWork<T>) };
|
2020-09-26 19:12:52 +09:00
|
|
|
let _ = mem::replace(
|
|
|
|
&mut work.value,
|
2020-12-01 15:55:19 +09:00
|
|
|
work.inner_task.compute().map(mem::MaybeUninit::new),
|
2020-09-26 19:12:52 +09:00
|
|
|
);
|
2021-11-04 19:44:06 +09:00
|
|
|
Box::leak(work);
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn complete<T: Task>(
|
|
|
|
env: sys::napi_env,
|
|
|
|
status: sys::napi_status,
|
|
|
|
data: *mut c_void,
|
|
|
|
) {
|
2022-01-13 12:15:02 +09:00
|
|
|
let mut work = unsafe { Box::from_raw(data as *mut AsyncWork<T>) };
|
2020-09-26 19:12:52 +09:00
|
|
|
let value_ptr = mem::replace(&mut work.value, Ok(mem::MaybeUninit::zeroed()));
|
2020-05-12 14:59:20 +09:00
|
|
|
let deferred = mem::replace(&mut work.deferred, ptr::null_mut());
|
2020-09-26 19:12:52 +09:00
|
|
|
let napi_async_work = mem::replace(&mut work.napi_async_work, ptr::null_mut());
|
2020-12-18 12:32:07 +09:00
|
|
|
let value = match value_ptr {
|
|
|
|
Ok(v) => {
|
2022-01-13 12:15:02 +09:00
|
|
|
let output = unsafe { v.assume_init() };
|
|
|
|
work
|
|
|
|
.inner_task
|
|
|
|
.resolve(unsafe { Env::from_raw(env) }, output)
|
2020-12-18 12:32:07 +09:00
|
|
|
}
|
2022-01-13 12:15:02 +09:00
|
|
|
Err(e) => work.inner_task.reject(unsafe { Env::from_raw(env) }, e),
|
2020-12-18 12:32:07 +09:00
|
|
|
};
|
2021-11-04 19:44:06 +09:00
|
|
|
if status != sys::Status::napi_cancelled && work.status.load(Ordering::Relaxed) != 2 {
|
|
|
|
match check_status!(status)
|
|
|
|
.and_then(move |_| value)
|
2022-01-13 12:15:02 +09:00
|
|
|
.and_then(|v| unsafe { ToNapiValue::to_napi_value(env, v) })
|
2021-11-04 19:44:06 +09:00
|
|
|
{
|
|
|
|
Ok(v) => {
|
2022-01-13 12:15:02 +09:00
|
|
|
let status = unsafe { sys::napi_resolve_deferred(env, deferred, v) };
|
2021-11-04 19:44:06 +09:00
|
|
|
debug_assert!(status == sys::Status::napi_ok, "Resolve promise failed");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2022-01-13 12:15:02 +09:00
|
|
|
let status =
|
|
|
|
unsafe { sys::napi_reject_deferred(env, deferred, JsError::from(e).into_value(env)) };
|
2021-11-04 19:44:06 +09:00
|
|
|
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-01-13 12:15:02 +09:00
|
|
|
if let Err(e) = work.inner_task.finally(unsafe { Env::from_raw(env) }) {
|
2021-11-04 19:44:06 +09:00
|
|
|
debug_assert!(false, "Panic in Task finally fn: {:?}", e);
|
|
|
|
}
|
2022-01-13 12:15:02 +09:00
|
|
|
let delete_status = unsafe { sys::napi_delete_async_work(env, napi_async_work) };
|
2020-09-26 19:12:52 +09:00
|
|
|
debug_assert!(
|
2020-11-11 13:15:13 +09:00
|
|
|
delete_status == sys::Status::napi_ok,
|
2020-09-26 19:12:52 +09:00
|
|
|
"Delete async work failed"
|
|
|
|
);
|
2021-11-04 19:44:06 +09:00
|
|
|
work.status.store(1, Ordering::Relaxed);
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|