-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
247 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
#![cfg(feature = "multithread")] | ||
use std::sync::Arc; | ||
|
||
use ferrunix::Registry; | ||
|
||
macro_rules! make_type { | ||
($base:ident) => { | ||
paste! { | ||
#[allow(non_snake_case)] | ||
#[derive(Debug, Default)] | ||
pub(super) struct $base {} | ||
|
||
impl $base { | ||
#[allow(non_snake_case)] | ||
pub(super) fn register(registry: &ferrunix::Registry) { | ||
registry.transient(|| $base::default()); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
($base:ident, $($deps:ident),*) => { | ||
paste! { | ||
#[allow(non_snake_case)] | ||
#[derive(Debug, Default)] | ||
pub(super) struct $base { | ||
$( | ||
pub(super) [<_ $deps>]: $deps, | ||
)* | ||
} | ||
|
||
impl $base { | ||
#[allow(non_snake_case)] | ||
pub(super) fn register(registry: &ferrunix::Registry) { | ||
registry | ||
.with_deps::<_, ($( | ||
ferrunix::Transient<$deps>, | ||
)*)>() | ||
.transient(|($( | ||
[<_ $deps>], | ||
)*)| $base {$( | ||
[<_ $deps>]: [<_ $deps>].get(), | ||
)*}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! make_many_types { | ||
($modname:ident) => { | ||
mod $modname { | ||
use paste::paste; | ||
|
||
make_type!(Config); | ||
make_type!(TypeZero, Dep0); | ||
make_type!(Dep0, Dep1); | ||
make_type!(Dep1, Dep2); | ||
make_type!(Dep2, Dep3); | ||
make_type!(Dep3, Dep4); | ||
make_type!(Dep4); | ||
|
||
make_type!(TypeNoDeps0); | ||
make_type!(TypeNoDeps1); | ||
make_type!(TypeNoDeps2); | ||
make_type!(TypeNoDeps3); | ||
make_type!(TypeNoDeps4); | ||
make_type!(TypeNoDeps5); | ||
make_type!(TypeNoDeps6); | ||
make_type!(TypeNoDeps7); | ||
make_type!(TypeNoDeps8); | ||
make_type!(TypeNoDeps9); | ||
make_type!(TypeNoDeps10); | ||
|
||
make_type!( | ||
TypeManyDeps0, | ||
TypeNoDeps0, | ||
TypeNoDeps1, | ||
TypeNoDeps2, | ||
TypeNoDeps3, | ||
TypeNoDeps4, | ||
TypeNoDeps5 | ||
); | ||
|
||
make_type!( | ||
TypeManyDeps1, | ||
TypeNoDeps6, | ||
TypeNoDeps7, | ||
TypeNoDeps8, | ||
TypeNoDeps9, | ||
TypeNoDeps10 | ||
); | ||
|
||
make_type!(TypeSingleDep0, Config); | ||
make_type!(TypeSingleDep1, Config); | ||
make_type!(TypeSingleDep2, Config); | ||
make_type!(TypeSingleDep3, Config); | ||
make_type!(TypeSingleDep4, Config); | ||
make_type!(TypeSingleDep5, Config); | ||
make_type!(TypeSingleDep6, Config); | ||
make_type!(TypeSingleDep7, Config); | ||
make_type!(TypeSingleDep8, Config); | ||
make_type!(TypeSingleDep9, Config); | ||
make_type!(TypeSingleDep10, Config); | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! register_all_types { | ||
($modname:ident, $reg:ident) => { | ||
$modname::Config::register(&$reg); | ||
$modname::TypeZero::register(&$reg); | ||
$modname::Dep0::register(&$reg); | ||
$modname::Dep1::register(&$reg); | ||
$modname::Dep2::register(&$reg); | ||
$modname::Dep3::register(&$reg); | ||
$modname::Dep4::register(&$reg); | ||
|
||
$modname::TypeNoDeps0::register(&$reg); | ||
$modname::TypeNoDeps1::register(&$reg); | ||
$modname::TypeNoDeps2::register(&$reg); | ||
$modname::TypeNoDeps3::register(&$reg); | ||
$modname::TypeNoDeps4::register(&$reg); | ||
$modname::TypeNoDeps5::register(&$reg); | ||
$modname::TypeNoDeps6::register(&$reg); | ||
$modname::TypeNoDeps7::register(&$reg); | ||
$modname::TypeNoDeps8::register(&$reg); | ||
$modname::TypeNoDeps9::register(&$reg); | ||
$modname::TypeNoDeps10::register(&$reg); | ||
|
||
$modname::TypeManyDeps0::register(&$reg); | ||
$modname::TypeManyDeps1::register(&$reg); | ||
|
||
$modname::TypeSingleDep0::register(&$reg); | ||
$modname::TypeSingleDep1::register(&$reg); | ||
$modname::TypeSingleDep2::register(&$reg); | ||
$modname::TypeSingleDep3::register(&$reg); | ||
$modname::TypeSingleDep4::register(&$reg); | ||
$modname::TypeSingleDep5::register(&$reg); | ||
$modname::TypeSingleDep6::register(&$reg); | ||
$modname::TypeSingleDep7::register(&$reg); | ||
$modname::TypeSingleDep8::register(&$reg); | ||
$modname::TypeSingleDep9::register(&$reg); | ||
$modname::TypeSingleDep10::register(&$reg); | ||
|
||
$reg.validate_all().unwrap(); | ||
}; | ||
} | ||
|
||
make_many_types!(manytypes0); | ||
make_many_types!(manytypes1); | ||
make_many_types!(manytypes2); | ||
make_many_types!(manytypes3); | ||
make_many_types!(manytypes4); | ||
make_many_types!(manytypes5); | ||
make_many_types!(manytypes6); | ||
|
||
#[test] | ||
fn stress_registration() { | ||
let registry = Arc::new(Registry::empty()); | ||
|
||
let handle0 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes0, registry); | ||
}) | ||
}; | ||
let handle1 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes1, registry); | ||
}) | ||
}; | ||
let handle2 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes2, registry); | ||
}) | ||
}; | ||
let handle3 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes3, registry); | ||
}) | ||
}; | ||
let handle4 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes4, registry); | ||
}) | ||
}; | ||
let handle5 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes5, registry); | ||
}) | ||
}; | ||
let handle6 = { | ||
let registry = Arc::clone(®istry); | ||
std::thread::spawn(move || { | ||
register_all_types!(manytypes6, registry); | ||
}) | ||
}; | ||
|
||
handle0.join().unwrap(); | ||
handle1.join().unwrap(); | ||
handle2.join().unwrap(); | ||
handle3.join().unwrap(); | ||
handle4.join().unwrap(); | ||
handle5.join().unwrap(); | ||
handle6.join().unwrap(); | ||
|
||
registry.validate_all().unwrap(); | ||
// println!("{}", registry.dotgraph().unwrap()); | ||
} |