mod.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #[cfg(target_os = "dragonos")]
  2. use drstd as std;
  3. use std::hash::{Hash, Hasher};
  4. use std::{
  5. collections::hash_map::DefaultHasher,
  6. collections::vec_deque::VecDeque,
  7. process::Child,
  8. sync::{Arc, Mutex, RwLock},
  9. vec::Vec,
  10. };
  11. use crate::unit::service::ServiceUnit;
  12. use crate::unit::{service, Unit};
  13. use hashbrown::HashMap;
  14. use lazy_static::lazy_static;
  15. lazy_static! {
  16. // 对于启动后即使退出亦认为其为运行状态的特殊注册类Service,对于这类进程做一个标记
  17. static ref FLAG_RUNNING: RwLock<Vec<usize>> = RwLock::new(Vec::new());
  18. // 任务等待队列,IDLE类型的service入队等待其它任务完成再执行
  19. static ref IDLE_SERVIEC_DEQUE: Mutex<VecDeque<usize>> = Mutex::new(VecDeque::new());
  20. // id到unit的映射表,全局的Unit管理表
  21. static ref ID_TO_UNIT_MAP: RwLock<HashMap<usize,Arc<Mutex<dyn Unit>>>> = RwLock::new(HashMap::new());
  22. // 辅助表,通过服务名映射其id
  23. static ref PATH_TO_UNIT_MAP: RwLock<HashMap<u64,usize>> = RwLock::new(HashMap::new());
  24. // 全局运行中的Unit表
  25. pub(super) static ref RUNNING_TABLE: RwLock<RunningTableManager> = RwLock::new(RunningTableManager { running_table: Vec::new() });
  26. }
  27. pub struct RunningTableManager {
  28. running_table: Vec<RunningUnit>,
  29. }
  30. impl<'a> IntoIterator for &'a mut RunningTableManager {
  31. type Item = &'a mut RunningUnit;
  32. type IntoIter = std::slice::IterMut<'a, RunningUnit>;
  33. fn into_iter(self) -> Self::IntoIter {
  34. self.running_table.iter_mut()
  35. }
  36. }
  37. pub struct RunningUnit {
  38. process: Child,
  39. unit_id: usize,
  40. }
  41. impl RunningUnit {
  42. pub fn new(p: Child, unit: usize) -> Self {
  43. RunningUnit {
  44. process: p,
  45. unit_id: unit,
  46. }
  47. }
  48. pub fn child(&mut self) -> &mut Child {
  49. &mut self.process
  50. }
  51. pub fn id(&self) -> &usize {
  52. &self.unit_id
  53. }
  54. }
  55. pub struct UnitManager;
  56. unsafe impl Sync for UnitManager {}
  57. impl UnitManager {
  58. pub fn insert_into_path_table(path: &str, unit: usize) {
  59. let mut hasher = DefaultHasher::new();
  60. path.hash(&mut hasher);
  61. let hash = hasher.finish();
  62. PATH_TO_UNIT_MAP.write().unwrap().insert(hash, unit);
  63. }
  64. pub fn contains_path(path: &str) -> bool {
  65. let mut hasher = DefaultHasher::new();
  66. path.hash(&mut hasher);
  67. let hash = hasher.finish();
  68. PATH_TO_UNIT_MAP.read().unwrap().contains_key(&hash)
  69. }
  70. pub fn get_unit_with_path(path: &str) -> Option<Arc<Mutex<dyn Unit>>> {
  71. let mut hasher = DefaultHasher::new();
  72. path.hash(&mut hasher);
  73. let hash = hasher.finish();
  74. let map = PATH_TO_UNIT_MAP.read().unwrap();
  75. let id = match map.get(&hash) {
  76. Some(id) => id,
  77. None => {
  78. return None;
  79. }
  80. };
  81. let map = ID_TO_UNIT_MAP.read().unwrap();
  82. let ret = map.get(id).cloned();
  83. ret
  84. }
  85. pub fn get_unit_with_id(id: &usize) -> Option<Arc<Mutex<dyn Unit>>> {
  86. let map = ID_TO_UNIT_MAP.read().unwrap();
  87. let ret = map.get(&id).cloned();
  88. ret
  89. }
  90. pub fn get_id_with_path(path: &str) -> Option<usize> {
  91. let mut hasher = DefaultHasher::new();
  92. path.hash(&mut hasher);
  93. let hash = hasher.finish();
  94. PATH_TO_UNIT_MAP.read().unwrap().get(&hash).cloned()
  95. }
  96. pub fn is_running_unit(id: &usize) -> bool {
  97. !RUNNING_TABLE
  98. .read()
  99. .unwrap()
  100. .running_table
  101. .iter()
  102. .filter(|x| x.unit_id == *id)
  103. .collect::<Vec<_>>()
  104. .is_empty()
  105. }
  106. pub fn push_running(unit: RunningUnit) {
  107. RUNNING_TABLE.write().unwrap().running_table.push(unit);
  108. }
  109. pub fn remove_running(id: usize) {
  110. let mut table = RUNNING_TABLE.write().unwrap();
  111. match table.running_table.iter().position(|x| x.unit_id == id) {
  112. Some(idx) => {
  113. table.running_table.remove(idx);
  114. }
  115. _ => (),
  116. }
  117. }
  118. pub fn insert_unit_with_id(id: usize, unit: Arc<Mutex<dyn Unit>>) {
  119. let mut map = ID_TO_UNIT_MAP.write().unwrap();
  120. if !map.contains_key(&id) {
  121. map.insert(id, unit);
  122. }
  123. }
  124. pub fn contains_id(id: &usize) -> bool{
  125. ID_TO_UNIT_MAP.read().unwrap().contains_key(id)
  126. }
  127. pub fn pop_a_idle_service() -> Option<Arc<Mutex<dyn Unit>>>{
  128. let id = IDLE_SERVIEC_DEQUE.lock().unwrap().pop_front();
  129. match id {
  130. Some(id) => {
  131. return Self::get_unit_with_id(&id);
  132. }
  133. None =>{
  134. return None;
  135. }
  136. }
  137. }
  138. pub fn push_a_idle_service(id: usize){
  139. if !Self::contains_id(&id) {
  140. return;
  141. }
  142. IDLE_SERVIEC_DEQUE.lock().unwrap().push_back(id);
  143. }
  144. pub fn push_flag_running(id: usize){
  145. let mut t = FLAG_RUNNING.write().unwrap();
  146. if t.contains(&id){
  147. return;
  148. }
  149. t.push(id);
  150. }
  151. pub fn running_count() -> usize{
  152. return RUNNING_TABLE.read().unwrap().running_table.len();
  153. }
  154. }