Skip to content

Commit

Permalink
loom macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenTauro committed Feb 6, 2020
1 parent 5739411 commit e0b5f28
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ maintenance = { status = "experimental" }
crossbeam-epoch = "0.8"
parking_lot = "0.10"
num_cpus = "1.12.0"
loom_macro = { path = "loom_macro" }

[dev-dependencies]
rand = "0.7"
Expand Down
15 changes: 15 additions & 0 deletions loom_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "loom_macro"
version = "0.0.1"
edition = "2018"
autotests = false
publish = false

[lib]
proc-macro = true

[dependencies]
loom = "0.2.14"
syn = { version = "0.15", features = ["extra-traits", "full", "visit-mut"] }
proc-macro2 = "0.4"
quote = "0.6"
22 changes: 22 additions & 0 deletions loom_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input};

#[proc_macro_attribute]
pub fn wrapper(args: TokenStream, input: TokenStream) -> TokenStream {
assert!(args.is_empty());

let ts = input.clone();
let ast = parse_macro_input!(ts);

if cfg!(all(loom, test)) {
let ts = quote! {
loom::model(|| {
#ast
})
};
ts.into()
} else {
input
}
}
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@
)]
#![warn(rust_2018_idioms)]

#[macro_use]
extern crate lazy_static;

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

#[cfg(debug_assertions)]
#[cfg(all(loom, test))]
use lazy_static::lazy_static;
#[cfg(all(loom, test))]
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))]
#[cfg(all(loom, test))]
use std::sync::{atomic::AtomicIsize, Once};

#[cfg(not(all(loom, test)))]
use std::sync::{
atomic::{AtomicIsize, AtomicUsize, Ordering},
Once,
Expand Down Expand Up @@ -57,10 +58,10 @@ const MAX_RESIZERS: isize = (1 << (isize_bits!() - RESIZE_STAMP_BITS)) - 1;
const RESIZE_STAMP_SHIFT: usize = isize_bits!() - RESIZE_STAMP_BITS;

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

#[cfg(debug_assertions)]
#[cfg(all(loom, test))]
lazy_static! {
static ref NCPU: AtomicUsize = AtomicUsize::new(0);
}
Expand Down
9 changes: 6 additions & 3 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crossbeam_epoch as epoch;
use flurry::*;
use loom_macro::wrapper;

#[cfg(all(loom, test))]
use loom::sync::Arc;
#[cfg(all(loom, test))]
use loom::thread;

#[test]
#[wrapper]
fn new() {
loom::model(|| {
let _map = HashMap::<usize, usize>::new();
});
let _map = HashMap::<usize, usize>::new();
}

#[test]
Expand Down

0 comments on commit e0b5f28

Please sign in to comment.