Implement execution task group
parent
61a045c8bb
commit
fe107fd81a
|
@ -1,3 +1,2 @@
|
|||
/target
|
||||
/Cargo.lock
|
||||
/.idea
|
||||
|
|
|
@ -0,0 +1,239 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.141"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "taskflow"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"parking_lot",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.27.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0de47a4eecbe11f498978a9b29d792f0d2692d1dd003650c24c76510e3bc001"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num_cpus",
|
||||
"pin-project-lite",
|
||||
"tokio-macros",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.45.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
|
|
@ -6,3 +6,5 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
parking_lot = "0.12.1"
|
||||
tokio = { version = "1.27.0", features = ["macros", "rt-multi-thread"] }
|
||||
|
|
63
src/lib.rs
63
src/lib.rs
|
@ -1,62 +1,3 @@
|
|||
pub mod task;
|
||||
|
||||
use crate::task::Task;
|
||||
|
||||
pub struct TF<'a> {
|
||||
task_vec: Vec<Task<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> TF<'a> {
|
||||
pub fn new() -> TF<'a> {
|
||||
TF {
|
||||
task_vec: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn emplace(&mut self, tasks: Vec<Box<dyn FnMut()>>) {
|
||||
for task in tasks {
|
||||
self.add_task(task);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_task(&mut self, fun: Box<dyn FnMut()>) {
|
||||
let mut task = Task::new(fun);
|
||||
task.set_pos(self.task_vec.len() as u32);
|
||||
self.task_vec.push(task);
|
||||
}
|
||||
|
||||
pub fn exec(&mut self) {
|
||||
// 是否退出循环
|
||||
let mut flag = false;
|
||||
|
||||
while !flag {
|
||||
flag = true;
|
||||
for task in self.task_vec.iter_mut() {
|
||||
// 入度为空且没有执行完成
|
||||
if task.get_indegree().is_empty() && !task.completed() {
|
||||
if flag {
|
||||
flag = false;
|
||||
}
|
||||
task.exec();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let mut taskflow = crate::TF::new();
|
||||
|
||||
taskflow.emplace(vec![
|
||||
Box::new(|| { println!("task 1"); }),
|
||||
Box::new(|| { println!("task 2"); }),
|
||||
Box::new(|| { println!("task 3"); }),
|
||||
]);
|
||||
taskflow.exec();
|
||||
}
|
||||
}
|
||||
pub mod task_wrap;
|
||||
pub mod task_group;
|
71
src/task.rs
71
src/task.rs
|
@ -1,68 +1,3 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
// 'task 表示拥有这些标记的对象生命周期相同
|
||||
pub struct Task<'task> {
|
||||
// fun 当前任务要执行的内容
|
||||
fun: Box<dyn FnMut() + 'task>,
|
||||
// done 当前任务是否执行完成
|
||||
done: bool,
|
||||
// 当前节点的位置
|
||||
pos: u32,
|
||||
// indegree 当前节点的入度
|
||||
indegree: HashSet<u32>,
|
||||
// outdegree 当前节点的出度
|
||||
outdegree: HashSet<u32>,
|
||||
}
|
||||
|
||||
// 'task 表示拥有这些标记的对象生命周期相同
|
||||
impl<'task> Task<'task> {
|
||||
|
||||
// 生成 task
|
||||
pub fn new<F: FnMut() + 'task>(f: F) -> Task<'task> {
|
||||
Task {
|
||||
pos: 0,
|
||||
fun: Box::new(f),
|
||||
done: false,
|
||||
indegree: HashSet::new(),
|
||||
outdegree: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
// 当前任务在任务队列中的位置
|
||||
pub(crate) fn set_pos(&mut self, pos: u32) {
|
||||
self.pos = pos
|
||||
}
|
||||
|
||||
// 执行 task 内的任务
|
||||
pub(crate) fn exec(&mut self) {
|
||||
(self.fun)();
|
||||
self.done = true;
|
||||
}
|
||||
|
||||
pub fn completed(&self) -> bool {
|
||||
self.done
|
||||
}
|
||||
|
||||
pub(crate) fn get_indegree(&self) -> &HashSet<u32> {
|
||||
&self.indegree
|
||||
}
|
||||
|
||||
pub(crate) fn get_outdegree(&self) -> &HashSet<u32> {
|
||||
&self.outdegree
|
||||
}
|
||||
|
||||
// 当前节点要先于入参节点执行
|
||||
fn precede(&mut self, nodes: Vec<Task>) {
|
||||
for task in nodes {
|
||||
self.outdegree.insert(task.pos);
|
||||
}
|
||||
}
|
||||
|
||||
// 当前节点要后于入参节点执行
|
||||
fn succeed(&mut self, nodes: Vec<Task>) {
|
||||
for task in nodes {
|
||||
self.indegree.insert(task.pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Task {
|
||||
fn run(&self);
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
use std::sync::Arc;
|
||||
use parking_lot::RwLock;
|
||||
use crate::task::Task;
|
||||
|
||||
pub struct TaskGroup {
|
||||
adjacency_matrix: Arc<RwLock<Vec<Vec<bool>>>>,
|
||||
task_vec: Vec<Arc<dyn Task + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl Task for TaskGroup {
|
||||
fn run(&self) {
|
||||
self.exec();
|
||||
}
|
||||
}
|
||||
|
||||
impl TaskGroup {
|
||||
pub fn new(task_vec: Vec<Arc<dyn Task + Send + Sync>>) -> TaskGroup {
|
||||
TaskGroup {
|
||||
adjacency_matrix: Arc::new(RwLock::new(vec![vec![false; task_vec.len()]; task_vec.len()])),
|
||||
task_vec,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn exec(&self) {
|
||||
for (i, task) in self.task_vec.iter().enumerate() {
|
||||
let adjacency_matrix = self.adjacency_matrix.clone();
|
||||
let task = task.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
if let Some(arr) = adjacency_matrix.try_read() {
|
||||
let mut pre_not_done = false;
|
||||
for arr in arr.iter() {
|
||||
pre_not_done = pre_not_done || arr[i];
|
||||
}
|
||||
if !pre_not_done {
|
||||
// Break out of the loop to acquire a read lock
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task.run();
|
||||
|
||||
loop {
|
||||
if let Some(mut arr) = adjacency_matrix.try_write() {
|
||||
for item in arr[i].iter_mut() {
|
||||
*item = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue