diff --git a/src/Task/Manager.rs b/src/Task/Manager.rs index a34c691..2ad60c8 100644 --- a/src/Task/Manager.rs +++ b/src/Task/Manager.rs @@ -17,6 +17,7 @@ struct Task_internal_type { } /// A manager for tasks. +#[derive(Clone)] pub struct Manager_type { /// A map of all tasks managed by the manager. Tasks: Arc>>, @@ -146,6 +147,18 @@ impl Manager_type { R } + + pub fn Get_current_task_identifier(&self) -> Result { + let Tasks = self.Tasks.read().unwrap(); // Acquire lock + + for (Task_identifier, Task) in Tasks.iter() { + if Task.Thread.Get_name().unwrap() == std::thread::current().name().unwrap() { + return Ok(*Task_identifier); + } + } + + Err(()) + } } #[cfg(test)] @@ -154,7 +167,7 @@ mod tests { use std::{thread, time::Duration}; #[test] - fn New_tasks() { + fn Test() { let Manager = Manager_type::New(); Manager.New_root_task(None, || { diff --git a/src/Task/Task.rs b/src/Task/Task.rs index 59beb76..79ca075 100644 --- a/src/Task/Task.rs +++ b/src/Task/Task.rs @@ -41,4 +41,39 @@ impl<'a> Task_type<'a> { pub fn Get_name(&self) -> Result { self.Manager.Get_task_name(self.Identifier) } + + pub fn Get_identifier(&self) -> Task_identifier_type { + self.Identifier + } + + pub fn Get_manager(&self) -> &'a Manager_type { + self.Manager + } + + pub fn Get_current_task(Manager: &'a Manager_type) -> Result { + let Current_task_identifier = Manager.Get_current_task_identifier()?; + Ok(Self::New(Current_task_identifier, Manager)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn Test() { + let Manager = Manager_type::New(); + + let Manager_copy = Manager.clone(); + + let Root_task = Manager.New_root_task(None, move || { + let Task = Task_type::Get_current_task(&Manager_copy).unwrap(); + + let Child_task = Task + .New_child_task("Child task", None, || { + std::thread::sleep(std::time::Duration::from_secs(1)); + }) + .unwrap(); + }); + } }