time unmodified

This commit is contained in:
Tateisi 2025-08-03 15:42:14 +08:00
parent cc1a9a4751
commit 58fa67414e
7 changed files with 89 additions and 12 deletions

View file

@ -6,13 +6,19 @@ extern crate user_lib;
use user_lib::{get_time, yield_};
/// 正确输出:(无报错信息)
/// get_time OK! {...}
/// Test sleep OK!
#[no_mangle]
fn main() -> i32 {
let current_timer = get_time();
let wait_for = current_timer + 3000;
let current_time = get_time();
assert!(current_time > 0);
println!("get_time OK! {}", current_time);
let wait_for = current_time + 3000;
while get_time() < wait_for {
yield_();
}
println!("Test sleep OK!");
0
}
}

22
user/src/bin/03sleep1.rs Normal file
View file

@ -0,0 +1,22 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{get_time, sleep};
#[no_mangle]
pub fn main() -> i32 {
let start = get_time();
println!("current time_msec = {}", start);
sleep(100);
let end = get_time();
println!(
"time_msec = {} after sleeping 100 ticks, delta = {}ms!",
end,
end - start
);
println!("Test sleep1 passed!");
0
}

View file

@ -20,6 +20,19 @@ fn main() -> i32 {
panic!("Cannot find main!");
}
#[repr(C)]
#[derive(Debug, Default)]
pub struct TimeVal {
pub sec: usize,
pub usec: usize,
}
impl TimeVal {
pub fn new() -> Self {
Self::default()
}
}
use syscall::*;
pub fn write(fd: usize, buf: &[u8]) -> isize {
@ -32,7 +45,11 @@ pub fn yield_() -> isize {
sys_yield()
}
pub fn get_time() -> isize {
sys_get_time()
let time = TimeVal::new();
match sys_get_time(&time, 0) {
0 => ((time.sec & 0xffff) * 1000 + time.usec / 1000) as isize,
_ => -1,
}
}
pub fn sbrk(size: i32) -> isize {
@ -46,3 +63,10 @@ pub fn munmap(start: usize, len: usize) -> isize {
pub fn mmap(start: usize, len: usize, prot: usize) -> isize {
sys_mmap(start, len, prot)
}
pub fn sleep(period_ms: usize) {
let start = get_time();
while get_time() < start + period_ms as isize {
sys_yield();
}
}

View file

@ -1,12 +1,15 @@
use core::arch::asm;
use crate::TimeVal;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETTIMEOFDAY: usize = 169;
const SYSCALL_SBRK: usize = 214;
pub const SYSCALL_MMAP: usize = 222;
pub const SYSCALL_MUNMAP: usize = 215;
const SYSCALL_MMAP: usize = 222;
const SYSCALL_MUNMAP: usize = 215;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
@ -34,8 +37,8 @@ pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time() -> isize {
syscall(SYSCALL_GET_TIME, [0, 0, 0])
pub fn sys_get_time(time: &TimeVal, tz: usize) -> isize {
syscall(SYSCALL_GETTIMEOFDAY, [time as *const _ as usize, tz, 0])
}
pub fn sys_sbrk(size: i32) -> isize {