From 5a21bbeb8e1d844b6176a680e7eaecd5b0adb939 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 12 Apr 2021 17:08:12 +0800 Subject: [PATCH] fix(napi): memory ordering issues in tsfn --- napi/src/threadsafe_function.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/napi/src/threadsafe_function.rs b/napi/src/threadsafe_function.rs index c8d50085..b82a7e6c 100644 --- a/napi/src/threadsafe_function.rs +++ b/napi/src/threadsafe_function.rs @@ -235,7 +235,7 @@ impl ThreadsafeFunction { format!("Can not ref, Thread safe function already aborted"), )); } - self.ref_count.fetch_add(1, Ordering::Acquire); + self.ref_count.fetch_add(1, Ordering::AcqRel); check_status!(unsafe { sys::napi_ref_threadsafe_function(env.0, self.raw_tsfn) }) } @@ -248,12 +248,12 @@ impl ThreadsafeFunction { format!("Can not unref, Thread safe function already aborted"), )); } - self.ref_count.fetch_sub(1, Ordering::Acquire); + self.ref_count.fetch_sub(1, Ordering::AcqRel); check_status!(unsafe { sys::napi_unref_threadsafe_function(env.0, self.raw_tsfn) }) } pub fn aborted(&self) -> bool { - self.aborted.load(Ordering::Acquire) + self.aborted.load(Ordering::Relaxed) } pub fn abort(self) -> Result<()> { @@ -311,7 +311,7 @@ impl ThreadsafeFunction { impl Drop for ThreadsafeFunction { fn drop(&mut self) { - if !self.aborted.load(Ordering::Acquire) && self.ref_count.load(Ordering::Relaxed) > 0usize { + if !self.aborted.load(Ordering::Acquire) && self.ref_count.load(Ordering::Acquire) > 0usize { let release_status = unsafe { sys::napi_release_threadsafe_function( self.raw_tsfn,