14fda81ceSLoGin use core::hint::spin_loop; 2bacd691cSlogin 3bacd691cSlogin use alloc::{boxed::Box, sync::Arc}; 491e9d4abSLoGin use system_error::SystemError; 5bacd691cSlogin 6bacd691cSlogin use crate::{ 74fda81ceSLoGin arch::{sched::sched, CurrentIrqArch, CurrentTimeArch}, 8ab5c8ca4Slogin exception::InterruptArch, 9*8cb2e9b3SLoGin include::bindings::bindings::useconds_t, 101496ba7bSLoGin process::ProcessManager, 118612b6ceSLoGin time::timekeeping::getnstimeofday, 12bacd691cSlogin }; 13bacd691cSlogin 14bacd691cSlogin use super::{ 15bacd691cSlogin timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper}, 164fda81ceSLoGin TimeArch, TimeSpec, 17bacd691cSlogin }; 18bacd691cSlogin 19bacd691cSlogin /// @brief 休眠指定时间(单位:纳秒) 20bacd691cSlogin /// 21bacd691cSlogin /// @param sleep_time 指定休眠的时间 22bacd691cSlogin /// 23bacd691cSlogin /// @return Ok(TimeSpec) 剩余休眠时间 24bacd691cSlogin /// 25bacd691cSlogin /// @return Err(SystemError) 错误码 26ab5c8ca4Slogin pub fn nanosleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> { 27bacd691cSlogin if sleep_time.tv_nsec < 0 || sleep_time.tv_nsec >= 1000000000 { 28bacd691cSlogin return Err(SystemError::EINVAL); 29bacd691cSlogin } 30bacd691cSlogin // 对于小于500us的时间,使用spin/rdtsc来进行定时 318612b6ceSLoGin if sleep_time.tv_nsec < 500000 && sleep_time.tv_sec == 0 { 32*8cb2e9b3SLoGin let expired_tsc: usize = CurrentTimeArch::cal_expire_cycles(sleep_time.tv_nsec as usize); 33*8cb2e9b3SLoGin while CurrentTimeArch::get_cycles() < expired_tsc { 34bacd691cSlogin spin_loop() 35bacd691cSlogin } 36bacd691cSlogin return Ok(TimeSpec { 37bacd691cSlogin tv_sec: 0, 38bacd691cSlogin tv_nsec: 0, 39bacd691cSlogin }); 40bacd691cSlogin } 418612b6ceSLoGin 428612b6ceSLoGin let total_sleep_time_us: u64 = 438612b6ceSLoGin sleep_time.tv_sec as u64 * 1000000 + sleep_time.tv_nsec as u64 / 1000; 44bacd691cSlogin // 创建定时器 451496ba7bSLoGin let handler: Box<WakeUpHelper> = WakeUpHelper::new(ProcessManager::current_pcb()); 468612b6ceSLoGin let timer: Arc<Timer> = Timer::new(handler, next_n_us_timer_jiffies(total_sleep_time_us)); 47bacd691cSlogin 48ab5c8ca4Slogin let irq_guard: crate::exception::IrqFlagsGuard = 49ab5c8ca4Slogin unsafe { CurrentIrqArch::save_and_disable_irq() }; 501496ba7bSLoGin ProcessManager::mark_sleep(true).ok(); 518612b6ceSLoGin 528612b6ceSLoGin let start_time = getnstimeofday(); 53bacd691cSlogin timer.activate(); 541496ba7bSLoGin 55ab5c8ca4Slogin drop(irq_guard); 56bacd691cSlogin sched(); 57bacd691cSlogin 588612b6ceSLoGin let end_time = getnstimeofday(); 598612b6ceSLoGin // 返回正确的剩余时间 608612b6ceSLoGin let real_sleep_time = end_time - start_time; 618612b6ceSLoGin let rm_time: TimeSpec = (sleep_time - real_sleep_time.into()).into(); 62bacd691cSlogin 638612b6ceSLoGin return Ok(rm_time); 64bacd691cSlogin } 65bacd691cSlogin 66bacd691cSlogin /// @brief 休眠指定时间(单位:微秒) 67bacd691cSlogin /// 68bacd691cSlogin /// @param usec 微秒 69bacd691cSlogin /// 70bacd691cSlogin /// @return Ok(TimeSpec) 剩余休眠时间 71bacd691cSlogin /// 72bacd691cSlogin /// @return Err(SystemError) 错误码 73ab5c8ca4Slogin pub fn usleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> { 74ab5c8ca4Slogin match nanosleep(sleep_time) { 75bacd691cSlogin Ok(value) => return Ok(value), 76bacd691cSlogin Err(err) => return Err(err), 77bacd691cSlogin }; 78bacd691cSlogin } 79bacd691cSlogin 80bacd691cSlogin //===== 以下为提供给C的接口 ===== 81bacd691cSlogin 82bacd691cSlogin /// @brief 休眠指定时间(单位:微秒)(提供给C的接口) 83bacd691cSlogin /// 84bacd691cSlogin /// @param usec 微秒 85bacd691cSlogin /// 86bacd691cSlogin /// @return Ok(i32) 0 87bacd691cSlogin /// 88bacd691cSlogin /// @return Err(SystemError) 错误码 89bacd691cSlogin #[no_mangle] 90bacd691cSlogin pub extern "C" fn rs_usleep(usec: useconds_t) -> i32 { 91bacd691cSlogin let sleep_time = TimeSpec { 92bacd691cSlogin tv_sec: (usec / 1000000) as i64, 93bacd691cSlogin tv_nsec: ((usec % 1000000) * 1000) as i64, 94bacd691cSlogin }; 95ab5c8ca4Slogin match usleep(sleep_time) { 96bacd691cSlogin Ok(_) => { 97bacd691cSlogin return 0; 98bacd691cSlogin } 99bacd691cSlogin Err(err) => { 100bacd691cSlogin return err.to_posix_errno(); 101bacd691cSlogin } 102bacd691cSlogin }; 103bacd691cSlogin } 104