Skip to content

Commit

Permalink
Add new test for drops in conc::Atomic with the overwrite drops.
Browse files Browse the repository at this point in the history
- Format long strings.

- Run `try_gc()` until succesful.
  • Loading branch information
ticki committed Jul 26, 2017
1 parent 4b8217f commit 3e4fbab
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
69 changes: 56 additions & 13 deletions conc/src/atomic.rs
Expand Up @@ -331,7 +331,7 @@ mod tests {
use std::sync::atomic::AtomicUsize;
use std::{thread, ptr};

#[derive(Clone)]
#[derive(Clone, Debug)]
struct Dropper {
d: Arc<AtomicUsize>,
}
Expand Down Expand Up @@ -421,12 +421,7 @@ mod tests {

assert!(opt.load(atomic::Ordering::Relaxed).is_none());

// To check that GC doesn't segfault or something.
let _ = ::try_gc();
let _ = ::try_gc();
let _ = ::try_gc();
let _ = ::try_gc();
let _ = ::try_gc();
while ::try_gc().is_err() {}
}

#[test]
Expand Down Expand Up @@ -455,12 +450,7 @@ mod tests {

assert!(opt.load(atomic::Ordering::Relaxed).is_none());

// To check that GC doesn't segfault or something.
let _ = ::try_gc();
let _ = ::try_gc();
let _ = ::try_gc();
let _ = ::try_gc();
let _ = ::try_gc();
while ::try_gc().is_err() {}
}
}

Expand Down Expand Up @@ -554,6 +544,59 @@ mod tests {

#[test]
fn drop3() {
let drops = Arc::new(AtomicUsize::default());

let d = Dropper {
d: drops.clone(),
};

for _ in 0..16 {
let atomic = Atomic::new(Some(Box::new(d.clone())));
atomic.store(Some(Box::new(d.clone())), atomic::Ordering::Relaxed);
let b = Box::new(d.clone());
let bptr = &*b as *const Dropper;
atomic.swap(Some(b), atomic::Ordering::Relaxed);
atomic.compare_and_swap(
Some(bptr),
Some(Box::new(d.clone())),
atomic::Ordering::Relaxed
).unwrap();
for _ in 0..10 {
atomic.compare_and_swap(
Some(bptr),
None,
atomic::Ordering::Relaxed
).unwrap_err();
unsafe {
atomic.compare_and_swap_raw(
bptr,
ptr::null_mut(),
atomic::Ordering::Relaxed
).unwrap_err();
}
}
let b = Box::new(d.clone());
let bptr = &*b as *const Dropper;
atomic.swap(Some(b), atomic::Ordering::Relaxed);
unsafe {
atomic.compare_and_swap_raw(
bptr,
Box::into_raw(Box::new(d.clone())),
atomic::Ordering::Relaxed
).unwrap();
}

atomic.store(None, atomic::Ordering::Relaxed);
}

drop(d);
while ::try_gc().is_err() {}

assert_eq!(drops.load(atomic::Ordering::Relaxed), 16 * 6);
}

#[test]
fn drop4() {
for i in 0..256 {
let opt = Arc::new(Atomic::new(Some(Box::new(i))));
let g = opt.load(atomic::Ordering::Relaxed);
Expand Down
8 changes: 5 additions & 3 deletions conc/src/guard.rs
Expand Up @@ -29,9 +29,11 @@ pub fn debug_assert_no_create() {
/// This "guards" the held pointer against garbage collection. First when all guards of said
/// pointer is gone (the data is unreachable), it can be collected.
// TODO: Remove this `'static` bound.
#[must_use = "You are getting a `conc::Guard<T>` without using it, which means it is potentially \
unnecessary overhead. Consider replacing the method with something that doesn't \
return a guard."]
#[must_use = "\
You are getting a `conc::Guard<T>` without using it, which means it is potentially \
unnecessary overhead. Consider replacing the method with something that doesn't \
return a guard.\
"]
#[derive(Debug)]
pub struct Guard<T: 'static + ?Sized> {
/// The inner hazard.
Expand Down

0 comments on commit 3e4fbab

Please sign in to comment.