fix(napi): add tokio cleanup hook for more platforms (#1790)

This commit is contained in:
LongYinan 2023-11-06 13:59:54 +08:00 committed by GitHub
parent 65f57918fb
commit 3deae16442
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View file

@ -6,7 +6,7 @@ use std::ptr;
#[cfg(all(feature = "napi4", not(target_os = "wasi")))] #[cfg(all(feature = "napi4", not(target_os = "wasi")))]
use std::sync::atomic::AtomicPtr; use std::sync::atomic::AtomicPtr;
#[cfg(all( #[cfg(all(
any(target_os = "windows", target_os = "freebsd"), not(any(target_os = "macos", target_os = "wasi")),
feature = "napi4", feature = "napi4",
feature = "tokio_rt" feature = "tokio_rt"
))] ))]
@ -463,7 +463,7 @@ pub unsafe extern "C" fn napi_register_module_v1(
} }
#[cfg(all( #[cfg(all(
any(target_os = "windows", target_os = "freebsd"), not(any(target_os = "macos", target_os = "wasi")),
feature = "napi4", feature = "napi4",
feature = "tokio_rt" feature = "tokio_rt"
))] ))]

View file

@ -23,14 +23,14 @@ fn create_runtime() -> Option<Runtime> {
pub(crate) static RT: Lazy<RwLock<Option<Runtime>>> = Lazy::new(|| RwLock::new(create_runtime())); pub(crate) static RT: Lazy<RwLock<Option<Runtime>>> = Lazy::new(|| RwLock::new(create_runtime()));
#[cfg(any(target_os = "windows", target_os = "freebsd"))] #[cfg(not(any(target_os = "macos", target_os = "wasi")))]
static RT_REFERENCE_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); static RT_REFERENCE_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Ensure that the Tokio runtime is initialized. /// Ensure that the Tokio runtime is initialized.
/// In windows the Tokio runtime will be dropped when Node env exits. /// In windows the Tokio runtime will be dropped when Node env exits.
/// But in Electron renderer process, the Node env will exits and recreate when the window reloads. /// But in Electron renderer process, the Node env will exits and recreate when the window reloads.
/// So we need to ensure that the Tokio runtime is initialized when the Node env is created. /// So we need to ensure that the Tokio runtime is initialized when the Node env is created.
#[cfg(any(target_os = "windows", target_os = "freebsd"))] #[cfg(not(any(target_os = "macos", target_os = "wasi")))]
pub(crate) fn ensure_runtime() { pub(crate) fn ensure_runtime() {
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
@ -42,7 +42,7 @@ pub(crate) fn ensure_runtime() {
RT_REFERENCE_COUNT.fetch_add(1, Ordering::Relaxed); RT_REFERENCE_COUNT.fetch_add(1, Ordering::Relaxed);
} }
#[cfg(any(target_os = "windows", target_os = "freebsd"))] #[cfg(not(any(target_os = "macos", target_os = "wasi")))]
pub(crate) unsafe extern "C" fn drop_runtime(_arg: *mut std::ffi::c_void) { pub(crate) unsafe extern "C" fn drop_runtime(_arg: *mut std::ffi::c_void) {
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;