xref: /DragonOS/kernel/src/sched/completion.rs (revision 971462be94ba0a5c74af7a5f9653dfabd4932a63)
11496ba7bSLoGin #![allow(dead_code)]
21496ba7bSLoGin use crate::{
31496ba7bSLoGin     libs::{spinlock::SpinLock, wait_queue::WaitQueue},
41496ba7bSLoGin     syscall::SystemError,
51496ba7bSLoGin     time::timer::schedule_timeout,
61496ba7bSLoGin };
71496ba7bSLoGin 
81496ba7bSLoGin const COMPLETE_ALL: u32 = core::u32::MAX;
91496ba7bSLoGin const MAX_TIMEOUT: i64 = core::i64::MAX;
101496ba7bSLoGin 
111496ba7bSLoGin #[derive(Debug)]
121496ba7bSLoGin pub struct Completion {
131496ba7bSLoGin     inner: SpinLock<InnerCompletion>,
141496ba7bSLoGin }
151496ba7bSLoGin 
161496ba7bSLoGin impl Completion {
171496ba7bSLoGin     pub const fn new() -> Self {
181496ba7bSLoGin         Self {
191496ba7bSLoGin             inner: SpinLock::new(InnerCompletion::new()),
201496ba7bSLoGin         }
211496ba7bSLoGin     }
221496ba7bSLoGin 
231496ba7bSLoGin     /// @brief 基本函数:通用的处理wait命令的函数(即所有wait_for_completion函数最核心部分在这里)
241496ba7bSLoGin     ///
251496ba7bSLoGin     /// @param timeout 非负整数
261496ba7bSLoGin     /// @param interuptible 设置进程是否能被打断
271496ba7bSLoGin     /// @return 返回剩余时间或者SystemError
281496ba7bSLoGin     fn do_wait_for_common(&self, mut timeout: i64, interuptible: bool) -> Result<i64, SystemError> {
291496ba7bSLoGin         let mut inner = self.inner.lock_irqsave();
301496ba7bSLoGin 
311496ba7bSLoGin         if inner.done == 0 {
321496ba7bSLoGin             //loop break 类似 do while 保证进行一次信号检测
331496ba7bSLoGin             loop {
341496ba7bSLoGin                 //检查当前线程是否有未处理的信号
351496ba7bSLoGin                 //             if (signal_pending_state(state, current)) {
361496ba7bSLoGin                 // timeout = -ERESTARTSYS;
371496ba7bSLoGin                 // break;
381496ba7bSLoGin                 //}
391496ba7bSLoGin 
401496ba7bSLoGin                 if interuptible {
411496ba7bSLoGin                     unsafe { inner.wait_queue.sleep_without_schedule() };
421496ba7bSLoGin                 } else {
431496ba7bSLoGin                     unsafe { inner.wait_queue.sleep_without_schedule_uninterruptible() };
441496ba7bSLoGin                 }
451496ba7bSLoGin                 drop(inner);
461496ba7bSLoGin                 timeout = schedule_timeout(timeout)?;
471496ba7bSLoGin                 inner = self.inner.lock_irqsave();
481496ba7bSLoGin                 if inner.done != 0 || timeout <= 0 {
491496ba7bSLoGin                     break;
501496ba7bSLoGin                 }
511496ba7bSLoGin             }
521496ba7bSLoGin             inner.wait_queue.wakeup(None);
531496ba7bSLoGin             if inner.done == 0 {
541496ba7bSLoGin                 drop(inner);
551496ba7bSLoGin                 return Ok(timeout);
561496ba7bSLoGin             }
571496ba7bSLoGin         }
581496ba7bSLoGin         if inner.done != COMPLETE_ALL {
591496ba7bSLoGin             inner.done -= 1;
601496ba7bSLoGin         }
611496ba7bSLoGin         drop(inner);
621496ba7bSLoGin         return Ok(if timeout > 0 { timeout } else { 1 });
631496ba7bSLoGin     }
641496ba7bSLoGin 
651496ba7bSLoGin     /// @brief 等待指定时间,超时后就返回, 同时设置pcb state为uninteruptible.
661496ba7bSLoGin     /// @param timeout 非负整数,等待指定时间,超时后就返回/或者提前done
671496ba7bSLoGin     pub fn wait_for_completion_timeout(&self, timeout: i64) -> Result<i64, SystemError> {
681496ba7bSLoGin         self.do_wait_for_common(timeout, false)
691496ba7bSLoGin     }
701496ba7bSLoGin 
711496ba7bSLoGin     /// @brief 等待completion命令唤醒进程, 同时设置pcb state 为uninteruptible.
721496ba7bSLoGin     pub fn wait_for_completion(&self) -> Result<i64, SystemError> {
731496ba7bSLoGin         self.do_wait_for_common(MAX_TIMEOUT, false)
741496ba7bSLoGin     }
751496ba7bSLoGin 
761496ba7bSLoGin     /// @brief @brief 等待completion的完成,但是可以被中断
771496ba7bSLoGin     pub fn wait_for_completion_interruptible(&self) -> Result<i64, SystemError> {
781496ba7bSLoGin         self.do_wait_for_common(MAX_TIMEOUT, true)
791496ba7bSLoGin     }
801496ba7bSLoGin 
811496ba7bSLoGin     pub fn wait_for_completion_interruptible_timeout(
821496ba7bSLoGin         &mut self,
831496ba7bSLoGin         timeout: i64,
841496ba7bSLoGin     ) -> Result<i64, SystemError> {
851496ba7bSLoGin         assert!(timeout >= 0);
861496ba7bSLoGin         self.do_wait_for_common(timeout, true)
871496ba7bSLoGin     }
881496ba7bSLoGin 
891496ba7bSLoGin     /// @brief 唤醒一个wait_queue中的节点
901496ba7bSLoGin     pub fn complete(&self) {
911496ba7bSLoGin         let mut inner = self.inner.lock_irqsave();
921496ba7bSLoGin         if inner.done != COMPLETE_ALL {
931496ba7bSLoGin             inner.done += 1;
941496ba7bSLoGin         }
951496ba7bSLoGin         inner.wait_queue.wakeup(None);
961496ba7bSLoGin         // 脱离生命周期,自动释放guard
971496ba7bSLoGin     }
981496ba7bSLoGin 
991496ba7bSLoGin     /// @brief 永久标记done为Complete_All,并从wait_queue中删除所有节点
100*971462beSGnoCiYeH     pub fn complete_all(&self) {
1011496ba7bSLoGin         let mut inner = self.inner.lock_irqsave();
1021496ba7bSLoGin         inner.done = COMPLETE_ALL;
1031496ba7bSLoGin         inner.wait_queue.wakeup_all(None);
1041496ba7bSLoGin         // 脱离生命周期,自动释放guard
1051496ba7bSLoGin     }
1061496ba7bSLoGin 
1071496ba7bSLoGin     /// @brief @brief 尝试获取completion的一个done!如果您在wait之前加上这个函数作为判断,说不定会加快运行速度。
1081496ba7bSLoGin     ///
1091496ba7bSLoGin     /// @return true - 表示不需要wait_for_completion,并且已经获取到了一个completion(即返回true意味着done已经被 减1 )
1101496ba7bSLoGin     /// @return false - 表示当前done=0,您需要进入等待,即wait_for_completion
1111496ba7bSLoGin     pub fn try_wait_for_completion(&mut self) -> bool {
1121496ba7bSLoGin         let mut inner = self.inner.lock_irqsave();
1131496ba7bSLoGin         if inner.done == 0 {
1141496ba7bSLoGin             return false;
1151496ba7bSLoGin         }
1161496ba7bSLoGin 
1171496ba7bSLoGin         if inner.done != 0 {
1181496ba7bSLoGin             return false;
1191496ba7bSLoGin         } else if inner.done != COMPLETE_ALL {
1201496ba7bSLoGin             inner.done -= 1;
1211496ba7bSLoGin         }
1221496ba7bSLoGin         return true;
1231496ba7bSLoGin         // 脱离生命周期,自动释放guard
1241496ba7bSLoGin     }
1251496ba7bSLoGin 
1261496ba7bSLoGin     // @brief 测试一个completion是否有waiter。(即done是不是等于0)
1271496ba7bSLoGin     pub fn completion_done(&self) -> bool {
1281496ba7bSLoGin         let inner = self.inner.lock_irqsave();
1291496ba7bSLoGin         if inner.done == 0 {
1301496ba7bSLoGin             return false;
1311496ba7bSLoGin         }
1321496ba7bSLoGin 
1331496ba7bSLoGin         if inner.done == 0 {
1341496ba7bSLoGin             return false;
1351496ba7bSLoGin         }
1361496ba7bSLoGin         return true;
1371496ba7bSLoGin         // 脱离生命周期,自动释放guard
1381496ba7bSLoGin     }
1391496ba7bSLoGin }
1401496ba7bSLoGin #[derive(Debug)]
1411496ba7bSLoGin pub struct InnerCompletion {
1421496ba7bSLoGin     done: u32,
1431496ba7bSLoGin     wait_queue: WaitQueue,
1441496ba7bSLoGin }
1451496ba7bSLoGin 
1461496ba7bSLoGin impl InnerCompletion {
1471496ba7bSLoGin     pub const fn new() -> Self {
1481496ba7bSLoGin         Self {
1491496ba7bSLoGin             done: 0,
1501496ba7bSLoGin             wait_queue: WaitQueue::INIT,
1511496ba7bSLoGin         }
1521496ba7bSLoGin     }
1531496ba7bSLoGin }
154