fix sys_nanosleep

master
WangRunji 6 years ago
parent 491353acfd
commit ab63c933c2

@ -58,7 +58,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
024 => sys_yield(), 024 => sys_yield(),
033 => sys_dup2(args[0], args[1]), 033 => sys_dup2(args[0], args[1]),
// 034 => sys_pause(), // 034 => sys_pause(),
035 => sys_sleep(args[0]), // TODO: nanosleep 035 => sys_nanosleep(args[0] as *const TimeSpec),
039 => sys_getpid(), 039 => sys_getpid(),
041 => sys_socket(args[0], args[1], args[2]), 041 => sys_socket(args[0], args[1], args[2]),
042 => sys_connect(args[0], args[1] as *const SockaddrIn, args[2]), 042 => sys_connect(args[0], args[1] as *const SockaddrIn, args[2]),

@ -199,14 +199,11 @@ pub fn sys_exit(exit_code: isize) -> ! {
unreachable!(); unreachable!();
} }
pub fn sys_sleep(time: usize) -> SysResult { pub fn sys_nanosleep(req: *const TimeSpec) -> SysResult {
info!("sleep: time: {}", time); process().memory_set.check_ptr(req)?;
if time >= 1 << 31 { let time = unsafe { req.read() };
thread::park(); info!("nanosleep: time: {:#?}", time);
} else { thread::sleep(time.to_duration());
use core::time::Duration;
thread::sleep(Duration::from_millis(time as u64 * 10));
}
Ok(0) Ok(0)
} }

@ -4,6 +4,7 @@ use super::*;
use crate::arch::consts::USEC_PER_TICK; use crate::arch::consts::USEC_PER_TICK;
use crate::arch::driver::rtc_cmos; use crate::arch::driver::rtc_cmos;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use core::time::Duration;
lazy_static! { lazy_static! {
pub static ref EPOCH_BASE: u64 = unsafe { rtc_cmos::read_epoch() }; pub static ref EPOCH_BASE: u64 = unsafe { rtc_cmos::read_epoch() };
@ -11,7 +12,7 @@ lazy_static! {
} }
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct TimeVal { pub struct TimeVal {
sec: u64, sec: u64,
usec: u64, usec: u64,
@ -28,19 +29,15 @@ impl TimeVal {
} }
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct TimeSpec { pub struct TimeSpec {
sec: u64, sec: u64,
nsec: u64, nsec: u64,
} }
impl TimeSpec { impl TimeSpec {
pub fn to_msec(&self) -> u64 { pub fn to_duration(&self) -> Duration {
self.sec * 1000 + self.nsec / 1000_000 Duration::new(self.sec, self.nsec as u32)
}
pub fn to_usec(&self) -> u64 {
self.sec * 1000_000 + self.nsec / 1000
} }
} }

Loading…
Cancel
Save