1 #![allow(dead_code)] 2 use crate::{ 3 libs::{spinlock::SpinLock, wait_queue::WaitQueue}, 4 syscall::SystemError, 5 time::timer::schedule_timeout, 6 }; 7 8 const COMPLETE_ALL: u32 = core::u32::MAX; 9 const MAX_TIMEOUT: i64 = core::i64::MAX; 10 11 #[derive(Debug)] 12 pub struct Completion { 13 inner: SpinLock<InnerCompletion>, 14 } 15 16 impl Completion { 17 pub const fn new() -> Self { 18 Self { 19 inner: SpinLock::new(InnerCompletion::new()), 20 } 21 } 22 23 /// @brief 基本函数:通用的处理wait命令的函数(即所有wait_for_completion函数最核心部分在这里) 24 /// 25 /// @param timeout 非负整数 26 /// @param interuptible 设置进程是否能被打断 27 /// @return 返回剩余时间或者SystemError 28 fn do_wait_for_common(&self, mut timeout: i64, interuptible: bool) -> Result<i64, SystemError> { 29 let mut inner = self.inner.lock_irqsave(); 30 31 if inner.done == 0 { 32 //loop break 类似 do while 保证进行一次信号检测 33 loop { 34 //检查当前线程是否有未处理的信号 35 // if (signal_pending_state(state, current)) { 36 // timeout = -ERESTARTSYS; 37 // break; 38 //} 39 40 if interuptible { 41 unsafe { inner.wait_queue.sleep_without_schedule() }; 42 } else { 43 unsafe { inner.wait_queue.sleep_without_schedule_uninterruptible() }; 44 } 45 drop(inner); 46 timeout = schedule_timeout(timeout)?; 47 inner = self.inner.lock_irqsave(); 48 if inner.done != 0 || timeout <= 0 { 49 break; 50 } 51 } 52 inner.wait_queue.wakeup(None); 53 if inner.done == 0 { 54 drop(inner); 55 return Ok(timeout); 56 } 57 } 58 if inner.done != COMPLETE_ALL { 59 inner.done -= 1; 60 } 61 drop(inner); 62 return Ok(if timeout > 0 { timeout } else { 1 }); 63 } 64 65 /// @brief 等待指定时间,超时后就返回, 同时设置pcb state为uninteruptible. 66 /// @param timeout 非负整数,等待指定时间,超时后就返回/或者提前done 67 pub fn wait_for_completion_timeout(&self, timeout: i64) -> Result<i64, SystemError> { 68 self.do_wait_for_common(timeout, false) 69 } 70 71 /// @brief 等待completion命令唤醒进程, 同时设置pcb state 为uninteruptible. 72 pub fn wait_for_completion(&self) -> Result<i64, SystemError> { 73 self.do_wait_for_common(MAX_TIMEOUT, false) 74 } 75 76 /// @brief @brief 等待completion的完成,但是可以被中断 77 pub fn wait_for_completion_interruptible(&self) -> Result<i64, SystemError> { 78 self.do_wait_for_common(MAX_TIMEOUT, true) 79 } 80 81 pub fn wait_for_completion_interruptible_timeout( 82 &mut self, 83 timeout: i64, 84 ) -> Result<i64, SystemError> { 85 assert!(timeout >= 0); 86 self.do_wait_for_common(timeout, true) 87 } 88 89 /// @brief 唤醒一个wait_queue中的节点 90 pub fn complete(&self) { 91 let mut inner = self.inner.lock_irqsave(); 92 if inner.done != COMPLETE_ALL { 93 inner.done += 1; 94 } 95 inner.wait_queue.wakeup(None); 96 // 脱离生命周期,自动释放guard 97 } 98 99 /// @brief 永久标记done为Complete_All,并从wait_queue中删除所有节点 100 pub fn complete_all(&mut self) { 101 let mut inner = self.inner.lock_irqsave(); 102 inner.done = COMPLETE_ALL; 103 inner.wait_queue.wakeup_all(None); 104 // 脱离生命周期,自动释放guard 105 } 106 107 /// @brief @brief 尝试获取completion的一个done!如果您在wait之前加上这个函数作为判断,说不定会加快运行速度。 108 /// 109 /// @return true - 表示不需要wait_for_completion,并且已经获取到了一个completion(即返回true意味着done已经被 减1 ) 110 /// @return false - 表示当前done=0,您需要进入等待,即wait_for_completion 111 pub fn try_wait_for_completion(&mut self) -> bool { 112 let mut inner = self.inner.lock_irqsave(); 113 if inner.done == 0 { 114 return false; 115 } 116 117 if inner.done != 0 { 118 return false; 119 } else if inner.done != COMPLETE_ALL { 120 inner.done -= 1; 121 } 122 return true; 123 // 脱离生命周期,自动释放guard 124 } 125 126 // @brief 测试一个completion是否有waiter。(即done是不是等于0) 127 pub fn completion_done(&self) -> bool { 128 let inner = self.inner.lock_irqsave(); 129 if inner.done == 0 { 130 return false; 131 } 132 133 if inner.done == 0 { 134 return false; 135 } 136 return true; 137 // 脱离生命周期,自动释放guard 138 } 139 } 140 #[derive(Debug)] 141 pub struct InnerCompletion { 142 done: u32, 143 wait_queue: WaitQueue, 144 } 145 146 impl InnerCompletion { 147 pub const fn new() -> Self { 148 Self { 149 done: 0, 150 wait_queue: WaitQueue::INIT, 151 } 152 } 153 } 154