chore(napi): reduce Mutex usage while loading addon

This commit is contained in:
LongYinan 2022-12-15 23:24:22 +08:00
parent 6ad1740dac
commit a9f9dbb232
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
10 changed files with 123 additions and 276 deletions
crates/sys/src

View file

@ -738,7 +738,7 @@ pub use napi7::*;
pub use napi8::*;
#[cfg(windows)]
pub(super) unsafe fn load() -> Result<(), libloading::Error> {
pub(super) unsafe fn load() -> Result<libloading::Library, libloading::Error> {
let host = match libloading::os::windows::Library::this() {
Ok(lib) => lib.into(),
Err(err) => {
@ -764,5 +764,5 @@ pub(super) unsafe fn load() -> Result<(), libloading::Error> {
napi8::load(&host)?;
#[cfg(feature = "experimental")]
experimental::load(&host)?;
Ok(())
Ok(host)
}

View file

@ -77,28 +77,21 @@ macro_rules! generate {
};
}
#[cfg(windows)]
use std::sync::Once;
mod functions;
mod types;
pub use functions::*;
pub use types::*;
#[cfg(windows)]
static SETUP: Once = Once::new();
/// Loads N-API symbols from host process.
/// Must be called at least once before using any functions in bindings or
/// they will panic.
/// Safety: `env` must be a valid `napi_env` for the current thread
#[cfg(windows)]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn setup() {
SETUP.call_once(|| {
if let Err(err) = load() {
panic!("{}", err);
}
});
pub unsafe fn setup() -> libloading::Library {
match load() {
Err(err) => panic!("{}", err),
Ok(l) => l,
}
}