Replace Mutex<bool> with AtomicBool

This commit is contained in:
David Tolnay 2019-10-05 18:03:17 -04:00
parent 92b850f452
commit 794ba39c89
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -313,7 +313,9 @@ mod repr_correctness {
use super::*; use super::*;
use std::marker::Unpin; use std::marker::Unpin;
use std::mem; use std::mem;
use std::sync::{Arc, Mutex}; use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::Arc;
#[test] #[test]
fn size_of_error() { fn size_of_error() {
@ -329,7 +331,7 @@ mod repr_correctness {
#[test] #[test]
fn destructors_work() { fn destructors_work() {
#[derive(Debug)] #[derive(Debug)]
struct HasDrop(Box<Arc<Mutex<bool>>>); struct HasDrop(Arc<AtomicBool>);
impl StdError for HasDrop {} impl StdError for HasDrop {}
impl Display for HasDrop { impl Display for HasDrop {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -338,16 +340,15 @@ mod repr_correctness {
} }
impl Drop for HasDrop { impl Drop for HasDrop {
fn drop(&mut self) { fn drop(&mut self) {
let mut has_dropped = self.0.lock().unwrap(); let has_dropped = self.0.swap(true, SeqCst);
assert!(!*has_dropped); assert!(!has_dropped);
*has_dropped = true;
} }
} }
let has_dropped = Arc::new(Mutex::new(false)); let has_dropped = Arc::new(AtomicBool::new(false));
drop(Error::from(HasDrop(Box::new(has_dropped.clone())))); drop(Error::from(HasDrop(has_dropped.clone())));
assert!(*has_dropped.lock().unwrap()); assert!(has_dropped.load(SeqCst));
} }
} }