mirror of
https://github.com/xythrez/RustMP.git
synced 2025-12-14 04:21:01 +00:00
Added critical! macro. Added "private" to par_for. Converted par_for to use tail-recursive parsing.
This commit is contained in:
parent
24c3772926
commit
d7745fd38a
2 changed files with 125 additions and 39 deletions
113
src/lib.rs
113
src/lib.rs
|
|
@ -39,18 +39,54 @@ impl<T> Capture<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! critical {
|
||||
(read $($r:ident)+; readwrite $($w:ident)+; $($ops:tt)+) => {
|
||||
{
|
||||
$(let $r = $r.read();)*
|
||||
$(let mut $w = $w.write();)*
|
||||
$($ops)*
|
||||
}
|
||||
};
|
||||
(readwrite $($w:ident)+; read $($r:ident)+; $($ops:tt)+) => {
|
||||
{
|
||||
$(let $r = $r.read();)*
|
||||
$(let mut $w = $w.write();)*
|
||||
$($ops)*
|
||||
}
|
||||
};
|
||||
(readwrite $($w:ident)+; $($ops:tt)+) => {
|
||||
{
|
||||
$(let mut $w = $w.write();)*
|
||||
$($ops)*
|
||||
}
|
||||
};
|
||||
(read $($r:ident)+; $($ops:tt)+) => {
|
||||
{
|
||||
$(let $r = $r.read();)*
|
||||
$($ops)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! __internal_par_for {
|
||||
($name:ident, $iterator:expr, $size:expr, $($captured:ident)*, $blk:block) => {
|
||||
(var_name($name:ident),
|
||||
iterator($iter:expr),
|
||||
blocksize($size:expr),
|
||||
captured($($captured:ident)*),
|
||||
private($($private:ident)*),
|
||||
$blk:block) => {
|
||||
let mut __rmp_tasks = Vec::new();
|
||||
$(let $captured = rustmp::Capture::new($captured);)*
|
||||
{
|
||||
let __rmp_tpm_mtx = rustmp::ThreadPoolManager::get_instance_guard();
|
||||
let __rmp_tpm = __rmp_tpm_mtx.lock().unwrap();
|
||||
let __rmp_iters = __rmp_tpm.split_iterators($iterator, $size);
|
||||
let __rmp_iters = __rmp_tpm.split_iterators($iter, $size);
|
||||
for iter in __rmp_iters {
|
||||
$(let $captured = $captured.clone();)*
|
||||
__rmp_tasks.push(rustmp::as_static_job(move || {
|
||||
$(let mut $private = $private.clone();)*
|
||||
for &$name in &iter
|
||||
$blk
|
||||
}));
|
||||
|
|
@ -59,6 +95,54 @@ macro_rules! __internal_par_for {
|
|||
}
|
||||
$(let $captured = $captured.unwrap();)*
|
||||
};
|
||||
// Parse blocksize
|
||||
(var_name($name:ident),
|
||||
iterator($iter:expr),
|
||||
blocksize($size:expr),
|
||||
captured($($captured:ident)*),
|
||||
private($($private:ident)*),
|
||||
blocksize $new_size:expr,
|
||||
$($rem:tt)+) => {
|
||||
rustmp::__internal_par_for!(
|
||||
var_name($name),
|
||||
iterator($iter),
|
||||
blocksize($new_size),
|
||||
captured($($captured)*),
|
||||
private($($private)*),
|
||||
$($rem)*)
|
||||
};
|
||||
// Parse capturing
|
||||
(var_name($name:ident),
|
||||
iterator($iter:expr),
|
||||
blocksize($size:expr),
|
||||
captured($($captured:ident)*),
|
||||
private($($private:ident)*),
|
||||
capturing $($new_captured:ident)*,
|
||||
$($rem:tt)+) => {
|
||||
rustmp::__internal_par_for!(
|
||||
var_name($name),
|
||||
iterator($iter),
|
||||
blocksize($size),
|
||||
captured($($new_captured)*),
|
||||
private($($private)*),
|
||||
$($rem)*)
|
||||
};
|
||||
// Parse private
|
||||
(var_name($name:ident),
|
||||
iterator($iter:expr),
|
||||
blocksize($size:expr),
|
||||
captured($($captured:ident)*),
|
||||
private($($private:ident)*),
|
||||
private $($new_private:ident)*,
|
||||
$($rem:tt)+) => {
|
||||
rustmp::__internal_par_for!(
|
||||
var_name($name),
|
||||
iterator($iter),
|
||||
blocksize($size),
|
||||
captured($($captured)*),
|
||||
private($($new_private)*),
|
||||
$($rem)*)
|
||||
};
|
||||
}
|
||||
|
||||
/// "parallel for" wrapper
|
||||
|
|
@ -67,22 +151,13 @@ macro_rules! __internal_par_for {
|
|||
/// Current implementation save limited (max depth 32) stack space for macro expansion.
|
||||
#[macro_export]
|
||||
macro_rules! par_for {
|
||||
(for $name:ident in $iterator:expr, blocksize $size:expr, capturing $($captured:ident)+, $blk:block) => {
|
||||
rustmp::__internal_par_for!($name, $iterator, $size, $($captured)*, $blk);
|
||||
};
|
||||
(for $name:ident in $iterator:expr, blocksize $size:expr, capturing $($captured:ident)+ $blk:block) => {
|
||||
rustmp::__internal_par_for!($name, $iterator, $size, $($captured)*, $blk);
|
||||
};
|
||||
(for $name:ident in $iterator:expr, capturing $($captured:ident)+, blocksize $size:expr, $blk:block) => {
|
||||
rustmp::__internal_par_for!($name, $iterator, $size, $($captured)*, $blk);
|
||||
};
|
||||
(for $name:ident in $iterator:expr, blocksize $size:expr, $blk:block) => {
|
||||
rustmp::__internal_par_for!($name, $iterator, $size,, $blk);
|
||||
};
|
||||
(for $name:ident in $iterator:expr, capturing $($captured:ident)+, $blk:block) => {
|
||||
rustmp::__internal_par_for!($name, $iterator, 1, $($captured)*, $blk);
|
||||
};
|
||||
(for $name:ident in $iterator:expr, capturing $($captured:ident)+ $blk:block) => {
|
||||
rustmp::__internal_par_for!($name, $iterator, 1, $($captured)*, $blk);
|
||||
(for $name:ident in $iter:expr, $($rem:tt)+) => {
|
||||
rustmp::__internal_par_for!(
|
||||
var_name($name),
|
||||
iterator($iter),
|
||||
blocksize(1),
|
||||
captured(),
|
||||
private(),
|
||||
$($rem)*)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue