mod.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #[cfg(target_os = "dragonos")]
  2. use drstd as std;
  3. use std::{
  4. sync::{Arc, RwLock}, collections::hash_map::DefaultHasher, process::Child,
  5. };
  6. use std::hash::{Hash,Hasher};
  7. use crate::unit::Unit;
  8. use hashbrown::HashMap;
  9. use lazy_static::lazy_static;
  10. lazy_static! {
  11. pub static ref GLOBAL_UNIT_MANAGER: RwLock<UnitManager> = {
  12. RwLock::new(UnitManager {
  13. id_to_unit: HashMap::new(),
  14. path_to_unit: HashMap::new(),
  15. running_table: Vec::new(),
  16. })
  17. };
  18. }
  19. pub struct RunnnigUnit(Child,Arc<dyn Unit>);
  20. impl RunnnigUnit {
  21. pub fn new(p:Child,unit: Arc<dyn Unit>) -> Self {
  22. RunnnigUnit(p,unit)
  23. }
  24. }
  25. pub struct UnitManager {
  26. // 通过unit_id映射unit
  27. pub id_to_unit: HashMap<usize, Arc<dyn Unit>>,
  28. // 通过path的hash值来映射Unit
  29. pub path_to_unit: HashMap<u64,Arc<dyn Unit>>,
  30. pub running_table: Vec<RunnnigUnit>
  31. }
  32. unsafe impl Sync for UnitManager {}
  33. impl UnitManager {
  34. pub fn insert_into_path_table(&mut self,path: &str,unit: Arc<dyn Unit>){
  35. let mut hasher = DefaultHasher::new();
  36. path.hash(&mut hasher);
  37. let hash = hasher.finish();
  38. self.path_to_unit.insert(hash, unit);
  39. }
  40. pub fn contants_path(&self,path: &str) -> bool{
  41. let mut hasher = DefaultHasher::new();
  42. path.hash(&mut hasher);
  43. let hash = hasher.finish();
  44. self.path_to_unit.contains_key(&hash)
  45. }
  46. pub fn get_unit_with_path(&self,path: &str) -> Option<&Arc<dyn Unit>> {
  47. let mut hasher = DefaultHasher::new();
  48. path.hash(&mut hasher);
  49. let hash = hasher.finish();
  50. self.path_to_unit.get(&hash)
  51. }
  52. }