From 258676a15f4e50bc8560e5e28957ffe461563dfb Mon Sep 17 00:00:00 2001 From: driftluo Date: Wed, 28 Aug 2024 14:40:52 +0800 Subject: [PATCH] fix: fix atomic ordering in multi-thread --- shared/src/shared.rs | 4 ++-- shared/src/types/header_map/kernel_lru.rs | 4 ++-- tx-pool/src/service.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shared/src/shared.rs b/shared/src/shared.rs index a6141e041d..2939230a5f 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -378,14 +378,14 @@ impl Shared { /// Return whether chain is in initial block download pub fn is_initial_block_download(&self) -> bool { // Once this function has returned false, it must remain false. - if self.ibd_finished.load(Ordering::Relaxed) { + if self.ibd_finished.load(Ordering::Acquire) { false } else if unix_time_as_millis().saturating_sub(self.snapshot().tip_header().timestamp()) > MAX_TIP_AGE { true } else { - self.ibd_finished.store(true, Ordering::Relaxed); + self.ibd_finished.store(true, Ordering::Release); false } } diff --git a/shared/src/types/header_map/kernel_lru.rs b/shared/src/types/header_map/kernel_lru.rs index 46dba8eb35..c82404658e 100644 --- a/shared/src/types/header_map/kernel_lru.rs +++ b/shared/src/types/header_map/kernel_lru.rs @@ -157,7 +157,7 @@ where self.stats().tick_primary_delete(); } // If IBD is not finished, don't shrink memory map - let allow_shrink_to_fit = self.ibd_finished.load(Ordering::Relaxed); + let allow_shrink_to_fit = self.ibd_finished.load(Ordering::Acquire); self.memory.remove(hash, allow_shrink_to_fit); if self.backend.is_empty() { return; @@ -175,7 +175,7 @@ where }); // If IBD is not finished, don't shrink memory map - let allow_shrink_to_fit = self.ibd_finished.load(Ordering::Relaxed); + let allow_shrink_to_fit = self.ibd_finished.load(Ordering::Acquire); self.memory .remove_batch(values.iter().map(|value| value.hash()), allow_shrink_to_fit); } diff --git a/tx-pool/src/service.rs b/tx-pool/src/service.rs index 83621e465e..c4e9e4e0d5 100644 --- a/tx-pool/src/service.rs +++ b/tx-pool/src/service.rs @@ -177,13 +177,13 @@ macro_rules! send_notify { impl TxPoolController { /// Return whether tx-pool service is started pub fn service_started(&self) -> bool { - self.started.load(Ordering::Relaxed) + self.started.load(Ordering::Acquire) } /// Set tx-pool service started, should only used for test #[cfg(feature = "internal")] pub fn set_service_started(&self, v: bool) { - self.started.store(v, Ordering::Relaxed); + self.started.store(v, Ordering::Release); } /// Return reference of tokio runtime handle @@ -658,7 +658,7 @@ impl TxPoolServiceBuilder { } } }); - self.started.store(true, Ordering::Relaxed); + self.started.store(true, Ordering::Release); if let Err(err) = self.tx_pool_controller.load_persisted_data(txs) { error!("Failed to import persistent txs, cause: {}", err); }