1ab5c8ca4Slogin use core::{arch::x86_64::_rdtsc, hint::spin_loop}; 2bacd691cSlogin 3bacd691cSlogin use alloc::{boxed::Box, sync::Arc}; 4bacd691cSlogin 5bacd691cSlogin use crate::{ 6*1496ba7bSLoGin arch::{sched::sched, CurrentIrqArch}, 7ab5c8ca4Slogin exception::InterruptArch, 8ab5c8ca4Slogin include::bindings::bindings::{useconds_t, Cpu_tsc_freq}, 9*1496ba7bSLoGin process::ProcessManager, 10bacd691cSlogin syscall::SystemError, 11bacd691cSlogin }; 12bacd691cSlogin 13bacd691cSlogin use super::{ 14bacd691cSlogin timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper}, 15bacd691cSlogin TimeSpec, 16bacd691cSlogin }; 17bacd691cSlogin 18bacd691cSlogin /// @brief 休眠指定时间(单位:纳秒) 19bacd691cSlogin /// 20bacd691cSlogin /// @param sleep_time 指定休眠的时间 21bacd691cSlogin /// 22bacd691cSlogin /// @return Ok(TimeSpec) 剩余休眠时间 23bacd691cSlogin /// 24bacd691cSlogin /// @return Err(SystemError) 错误码 25ab5c8ca4Slogin pub fn nanosleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> { 26bacd691cSlogin if sleep_time.tv_nsec < 0 || sleep_time.tv_nsec >= 1000000000 { 27bacd691cSlogin return Err(SystemError::EINVAL); 28bacd691cSlogin } 29bacd691cSlogin // 对于小于500us的时间,使用spin/rdtsc来进行定时 30bacd691cSlogin 31bacd691cSlogin if sleep_time.tv_nsec < 500000 { 32bacd691cSlogin let expired_tsc: u64 = 33bacd691cSlogin unsafe { _rdtsc() + (sleep_time.tv_nsec as u64 * Cpu_tsc_freq) / 1000000000 }; 34bacd691cSlogin while unsafe { _rdtsc() } < expired_tsc { 35bacd691cSlogin spin_loop() 36bacd691cSlogin } 37bacd691cSlogin return Ok(TimeSpec { 38bacd691cSlogin tv_sec: 0, 39bacd691cSlogin tv_nsec: 0, 40bacd691cSlogin }); 41bacd691cSlogin } 42bacd691cSlogin // 创建定时器 43*1496ba7bSLoGin let handler: Box<WakeUpHelper> = WakeUpHelper::new(ProcessManager::current_pcb()); 44bacd691cSlogin let timer: Arc<Timer> = Timer::new( 45bacd691cSlogin handler, 46bacd691cSlogin next_n_us_timer_jiffies((sleep_time.tv_nsec / 1000) as u64), 47bacd691cSlogin ); 48bacd691cSlogin 49ab5c8ca4Slogin let irq_guard: crate::exception::IrqFlagsGuard = 50ab5c8ca4Slogin unsafe { CurrentIrqArch::save_and_disable_irq() }; 51*1496ba7bSLoGin ProcessManager::mark_sleep(true).ok(); 52bacd691cSlogin timer.activate(); 53*1496ba7bSLoGin 54ab5c8ca4Slogin drop(irq_guard); 55bacd691cSlogin 56bacd691cSlogin sched(); 57bacd691cSlogin 58bacd691cSlogin // TODO: 增加信号唤醒的功能后,返回正确的剩余时间 59bacd691cSlogin 60bacd691cSlogin return Ok(TimeSpec { 61bacd691cSlogin tv_sec: 0, 62bacd691cSlogin tv_nsec: 0, 63bacd691cSlogin }); 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