diff --git a/ext/napi/lib.rs b/ext/napi/lib.rs index b4f38fd0af..789aec5eb7 100644 --- a/ext/napi/lib.rs +++ b/ext/napi/lib.rs @@ -11,6 +11,7 @@ use deno_core::error::AnyError; use deno_core::futures::channel::mpsc; use deno_core::futures::StreamExt; use deno_core::op; +use deno_core::parking_lot::Mutex; use deno_core::serde_v8; use deno_core::Extension; use deno_core::OpState; @@ -338,7 +339,7 @@ pub struct NapiState { mpsc::UnboundedSender, pub env_cleanup_hooks: Rc>>, - pub tsfn_ref_counters: Rc>, + pub tsfn_ref_counters: Arc>, } impl Drop for NapiState { @@ -415,7 +416,7 @@ pub struct Env { mpsc::UnboundedSender, pub cleanup_hooks: Rc>>, - pub tsfn_ref_counters: Rc>, + pub tsfn_ref_counters: Arc>, pub last_error: napi_extended_error_info, } @@ -431,7 +432,7 @@ impl Env { cleanup_hooks: Rc< RefCell>, >, - tsfn_ref_counters: Rc>, + tsfn_ref_counters: Arc>, ) -> Self { let sc = sender.clone(); ASYNC_WORK_SENDER.with(|s| { @@ -498,13 +499,13 @@ impl Env { id: usize, counter: Arc, ) { - let mut counters = self.tsfn_ref_counters.borrow_mut(); + let mut counters = self.tsfn_ref_counters.lock(); assert!(!counters.iter().any(|(i, _)| *i == id)); counters.push((id, counter)); } pub fn remove_threadsafe_function_ref_counter(&mut self, id: usize) { - let mut counters = self.tsfn_ref_counters.borrow_mut(); + let mut counters = self.tsfn_ref_counters.lock(); let index = counters.iter().position(|(i, _)| *i == id).unwrap(); counters.remove(index); } @@ -533,7 +534,7 @@ pub fn init(unstable: bool) -> Extension { maybe_scheduling = true; } - let tsfn_ref_counters = napi_state.tsfn_ref_counters.borrow().clone(); + let tsfn_ref_counters = napi_state.tsfn_ref_counters.lock().clone(); for (_id, counter) in tsfn_ref_counters.iter() { if counter.load(std::sync::atomic::Ordering::SeqCst) > 0 { maybe_scheduling = true; @@ -572,7 +573,7 @@ pub fn init(unstable: bool) -> Extension { threadsafe_function_receiver, active_threadsafe_functions: 0, env_cleanup_hooks: Rc::new(RefCell::new(vec![])), - tsfn_ref_counters: Rc::new(RefCell::new(vec![])), + tsfn_ref_counters: Arc::new(Mutex::new(vec![])), }); state.put(Unstable(unstable)); Ok(())