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