173779f3dSLoGin use std::{ 273779f3dSLoGin path::PathBuf, 373779f3dSLoGin sync::{Arc, Mutex}, 473779f3dSLoGin thread::JoinHandle, 573779f3dSLoGin }; 673779f3dSLoGin 7*eaa67f3cSLoGin use crate::{context::Action, scheduler::TID_EID}; 873779f3dSLoGin 973779f3dSLoGin use super::{SchedEntity, Scheduler}; 1073779f3dSLoGin 1173779f3dSLoGin // 最大线程数 1273779f3dSLoGin pub const MAX_THREAD_NUM: usize = 32; 1373779f3dSLoGin // 默认线程数 1473779f3dSLoGin pub const DEFAULT_THREAD_NUM: usize = 2; 1573779f3dSLoGin 1673779f3dSLoGin lazy_static! { 1773779f3dSLoGin // 全局任务队列 1873779f3dSLoGin pub static ref TASK_DEQUE: Mutex<TaskDeque> = Mutex::new(TaskDeque { 1973779f3dSLoGin max_num: DEFAULT_THREAD_NUM, 2073779f3dSLoGin queue: Vec::new(), 2173779f3dSLoGin }); 2273779f3dSLoGin } 2373779f3dSLoGin 2473779f3dSLoGin /// # 任务队列 2573779f3dSLoGin pub struct TaskDeque { 2673779f3dSLoGin max_num: usize, 2773779f3dSLoGin queue: Vec<JoinHandle<()>>, 2873779f3dSLoGin } 2973779f3dSLoGin 3073779f3dSLoGin impl TaskDeque { 3173779f3dSLoGin /// 将构建或安装DADK任务添加到任务队列中 3273779f3dSLoGin /// 3373779f3dSLoGin /// ## 参数 3473779f3dSLoGin /// 3573779f3dSLoGin /// - `action` : 要执行的操作 3673779f3dSLoGin /// - `dragonos_dir` : DragonOS sysroot在主机上的路径 3773779f3dSLoGin /// - `entity` : 任务实体 3873779f3dSLoGin /// 3973779f3dSLoGin /// ## 返回值 4073779f3dSLoGin /// 4173779f3dSLoGin /// true 任务添加成功 4273779f3dSLoGin /// false 任务添加失败 build_install_task( &mut self, action: Action, dragonos_dir: PathBuf, entity: Arc<SchedEntity>, ) -> bool4373779f3dSLoGin pub fn build_install_task( 4473779f3dSLoGin &mut self, 4573779f3dSLoGin action: Action, 4673779f3dSLoGin dragonos_dir: PathBuf, 4773779f3dSLoGin entity: Arc<SchedEntity>, 4873779f3dSLoGin ) -> bool { 4973779f3dSLoGin // log::warn!("push stack: task:{} {entity:?}", entity.id()); 5073779f3dSLoGin if self.queue.len() < self.max_num { 5173779f3dSLoGin let id = entity.id(); 5273779f3dSLoGin let handler = std::thread::spawn(move || { 5373779f3dSLoGin Scheduler::execute(action, dragonos_dir.clone(), entity) 5473779f3dSLoGin }); 5573779f3dSLoGin TID_EID.lock().unwrap().insert(handler.thread().id(), id); 5673779f3dSLoGin self.queue.push(handler); 5773779f3dSLoGin return true; 5873779f3dSLoGin } 5973779f3dSLoGin return false; 6073779f3dSLoGin } 6173779f3dSLoGin 6273779f3dSLoGin /// 将清理DADK任务添加到任务队列中 6373779f3dSLoGin /// 6473779f3dSLoGin /// ## 参数 6573779f3dSLoGin /// 6673779f3dSLoGin /// - `action` : 要执行的操作 6773779f3dSLoGin /// - `dragonos_dir` : DragonOS sysroot在主机上的路径 6873779f3dSLoGin /// - `entity` : 任务实体 6973779f3dSLoGin /// 7073779f3dSLoGin /// ## 返回值 7173779f3dSLoGin /// 7273779f3dSLoGin /// 无 clean_task(&mut self, action: Action, dragonos_dir: PathBuf, entity: Arc<SchedEntity>)7373779f3dSLoGin pub fn clean_task(&mut self, action: Action, dragonos_dir: PathBuf, entity: Arc<SchedEntity>) { 7473779f3dSLoGin while self.queue.len() >= self.max_num { 7573779f3dSLoGin self.queue.retain(|x| !x.is_finished()); 7673779f3dSLoGin } 7773779f3dSLoGin let handler = 7873779f3dSLoGin std::thread::spawn(move || Scheduler::execute(action, dragonos_dir.clone(), entity)); 7973779f3dSLoGin self.queue.push(handler); 8073779f3dSLoGin } 8173779f3dSLoGin queue(&self) -> &Vec<JoinHandle<()>>8273779f3dSLoGin pub fn queue(&self) -> &Vec<JoinHandle<()>> { 8373779f3dSLoGin return &self.queue; 8473779f3dSLoGin } 8573779f3dSLoGin queue_mut(&mut self) -> &mut Vec<JoinHandle<()>>8673779f3dSLoGin pub fn queue_mut(&mut self) -> &mut Vec<JoinHandle<()>> { 8773779f3dSLoGin return &mut self.queue; 8873779f3dSLoGin } 8973779f3dSLoGin set_thread(&mut self, mut thread: usize)9073779f3dSLoGin pub fn set_thread(&mut self, mut thread: usize) { 9173779f3dSLoGin if thread > MAX_THREAD_NUM { 9273779f3dSLoGin thread = MAX_THREAD_NUM; 9373779f3dSLoGin } 9473779f3dSLoGin self.max_num = thread; 9573779f3dSLoGin } 9673779f3dSLoGin } 97