From 68369a7e4be7916a51dc127d3a107bdb667f50a5 Mon Sep 17 00:00:00 2001 From: Roman Moisieiev Date: Fri, 12 Sep 2025 14:04:07 +0100 Subject: [PATCH] Remove unnecessary Mutex --- src/channel.rs | 7 ++++--- src/sync/crossbeam.rs | 4 +++- src/sync/flume.rs | 4 +++- src/sync/std.rs | 3 ++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index 8da830b..8452487 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -1,3 +1,4 @@ +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use crate::error::Error; @@ -12,7 +13,7 @@ where { pub(crate) request_tx: ST, pub(crate) response_rx: SR, - pub(crate) closed: Mutex, + pub(crate) closed: AtomicBool, pub(crate) _phantom: std::marker::PhantomData, } @@ -127,7 +128,7 @@ where /// Get the current value from the producer pub fn get(&self) -> Result { // Check if locally marked as closed - if *self.closed.lock().unwrap() { + if self.closed.load(Ordering::Acquire) { return Err(Error::ChannelClosed); } @@ -152,7 +153,7 @@ where /// Close the channel from the consumer side pub fn close(&self) -> Result<(), Error> { // Mark locally as closed - *self.closed.lock().unwrap() = true; + self.closed.store(true, Ordering::Release); // Send close request self.request_tx diff --git a/src/sync/crossbeam.rs b/src/sync/crossbeam.rs index c20217c..20bc162 100644 --- a/src/sync/crossbeam.rs +++ b/src/sync/crossbeam.rs @@ -1,3 +1,5 @@ +use std::sync::atomic::AtomicBool; + #[cfg(feature = "sync-crossbeam")] use crate::sync::traits::{ChannelError, ChannelReceiver, ChannelSender, ChannelType}; use crate::types; @@ -70,7 +72,7 @@ impl CrossbeamSuck { let sucker = crate::Sucker { request_tx, response_rx, - closed: std::sync::Mutex::new(false), + closed: AtomicBool::new(false), _phantom: std::marker::PhantomData, }; diff --git a/src/sync/flume.rs b/src/sync/flume.rs index 9176b14..5208cf5 100644 --- a/src/sync/flume.rs +++ b/src/sync/flume.rs @@ -1,3 +1,5 @@ +use std::sync::atomic::AtomicBool; + #[cfg(feature = "sync-flume")] use crate::sync::traits::{ChannelError, ChannelReceiver, ChannelSender, ChannelType}; use crate::types; @@ -70,7 +72,7 @@ impl FlumeSuck { let sucker = crate::Sucker { request_tx, response_rx, - closed: std::sync::Mutex::new(false), + closed: AtomicBool::new(false), _phantom: std::marker::PhantomData, }; diff --git a/src/sync/std.rs b/src/sync/std.rs index d0458a1..d735ebb 100644 --- a/src/sync/std.rs +++ b/src/sync/std.rs @@ -1,5 +1,6 @@ use crate::sync::traits::{ChannelError, ChannelReceiver, ChannelSender, ChannelType}; use crate::types; +use std::sync::atomic::AtomicBool; #[cfg(feature = "sync-std")] use std::sync::mpsc; @@ -68,7 +69,7 @@ impl StdSuck { let sucker = crate::Sucker { request_tx, response_rx, - closed: std::sync::Mutex::new(false), + closed: AtomicBool::new(false), _phantom: std::marker::PhantomData, };