Skip to content

Commit

Permalink
Add loom behind cfg debug_assertions and make all the test use it
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenTauro committed Jan 27, 2020
1 parent 051ca79 commit fdb2bf2
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 253 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ maintenance = { status = "experimental" }
crossbeam-epoch = "0.8"
parking_lot = "0.10"
num_cpus = "1.12.0"
lazy_static = "1.4"
loom = "0.2.14"

[dev-dependencies]
rand = "0.7"
72 changes: 39 additions & 33 deletions src/iter/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,52 @@ mod tests {

#[test]
fn iter() {
let map = HashMap::<usize, usize>::new();

let guard = epoch::pin();
map.insert(1, 42, &guard);
map.insert(2, 84, &guard);

let guard = epoch::pin();
assert_eq!(
map.iter(&guard).collect::<HashSet<(&usize, &usize)>>(),
HashSet::from_iter(vec![(&1, &42), (&2, &84)])
);
loom::model(|| {
let map = HashMap::<usize, usize>::new();

let guard = epoch::pin();
map.insert(1, 42, &guard);
map.insert(2, 84, &guard);

let guard = epoch::pin();
assert_eq!(
map.iter(&guard).collect::<HashSet<(&usize, &usize)>>(),
HashSet::from_iter(vec![(&1, &42), (&2, &84)])
);
});
}

#[test]
fn keys() {
let map = HashMap::<usize, usize>::new();

let guard = epoch::pin();
map.insert(1, 42, &guard);
map.insert(2, 84, &guard);

let guard = epoch::pin();
assert_eq!(
map.keys(&guard).collect::<HashSet<&usize>>(),
HashSet::from_iter(vec![&1, &2])
);
loom::model(|| {
let map = HashMap::<usize, usize>::new();

let guard = epoch::pin();
map.insert(1, 42, &guard);
map.insert(2, 84, &guard);

let guard = epoch::pin();
assert_eq!(
map.keys(&guard).collect::<HashSet<&usize>>(),
HashSet::from_iter(vec![&1, &2])
);
});
}

#[test]
fn values() {
let map = HashMap::<usize, usize>::new();

let mut guard = epoch::pin();
map.insert(1, 42, &guard);
map.insert(2, 84, &guard);
guard.repin();

assert_eq!(
map.values(&guard).collect::<HashSet<&usize>>(),
HashSet::from_iter(vec![&42, &84])
);
loom::model(|| {
let map = HashMap::<usize, usize>::new();

let mut guard = epoch::pin();
map.insert(1, 42, &guard);
map.insert(2, 84, &guard);
guard.repin();

assert_eq!(
map.values(&guard).collect::<HashSet<&usize>>(),
HashSet::from_iter(vec![&42, &84])
);
});
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@
)]
#![warn(rust_2018_idioms)]

#[macro_use]
extern crate lazy_static;

mod map;
mod node;
mod raw;
Expand Down
14 changes: 14 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ use std::collections::hash_map::RandomState;
use std::fmt::{self, Debug, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::FromIterator;

#[cfg(debug_assertions)]
use loom::sync::atomic::{AtomicUsize, Ordering};
#[cfg(debug_assertions)]
use std::sync::atomic::AtomicIsize;
#[cfg(debug_assertions)]
use std::sync::Once;
#[cfg(not(debug_assertions))]
use std::sync::{
atomic::{AtomicIsize, AtomicUsize, Ordering},
Once,
Expand Down Expand Up @@ -49,8 +57,14 @@ const MAX_RESIZERS: isize = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
const RESIZE_STAMP_SHIFT: usize = 32 - RESIZE_STAMP_BITS;

static NCPU_INITIALIZER: Once = Once::new();
#[cfg(not(debug_assertions))]
static NCPU: AtomicUsize = AtomicUsize::new(0);

#[cfg(debug_assertions)]
lazy_static! {
static ref NCPU: AtomicUsize = AtomicUsize::new(0);
}

/// A concurrent hash table.
///
/// See the [crate-level documentation](index.html) for details.
Expand Down
Loading

0 comments on commit fdb2bf2

Please sign in to comment.