mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-22 02:53:23 +00:00
@ -3,6 +3,7 @@ use core::{fmt, ops};
|
||||
use self::timekeep::ktime_get_real_ns;
|
||||
|
||||
pub mod sleep;
|
||||
pub mod syscall;
|
||||
pub mod timekeep;
|
||||
pub mod timer;
|
||||
|
||||
@ -18,8 +19,9 @@ absolute and relative time.
|
||||
[Duration]: struct.Duration.html
|
||||
*/
|
||||
|
||||
/// 表示时间的结构体
|
||||
/// 表示时间的结构体,符合POSIX标准。
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[repr(C)]
|
||||
pub struct TimeSpec {
|
||||
pub tv_sec: i64,
|
||||
pub tv_nsec: i64,
|
||||
|
@ -1,18 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <common/glib.h>
|
||||
#include <process/ptrace.h>
|
||||
#include <common/time.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief 休眠指定时间
|
||||
*
|
||||
* @param rqtp 指定休眠的时间
|
||||
* @param rmtp 返回的剩余休眠时间
|
||||
* @return int
|
||||
*/
|
||||
int rs_nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
|
||||
#include <process/ptrace.h>
|
||||
|
||||
/**
|
||||
* @brief 睡眠指定时间
|
||||
|
@ -1,14 +1,15 @@
|
||||
use core::{arch::x86_64::_rdtsc, hint::spin_loop, ptr::null_mut};
|
||||
use core::{arch::x86_64::_rdtsc, hint::spin_loop};
|
||||
|
||||
use alloc::{boxed::Box, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
arch::{
|
||||
asm::current::current_pcb,
|
||||
interrupt::{cli, sti},
|
||||
sched::sched,
|
||||
CurrentIrqArch,
|
||||
},
|
||||
include::bindings::bindings::{timespec, useconds_t, Cpu_tsc_freq},
|
||||
exception::InterruptArch,
|
||||
include::bindings::bindings::{useconds_t, Cpu_tsc_freq},
|
||||
syscall::SystemError,
|
||||
};
|
||||
|
||||
@ -24,7 +25,7 @@ use super::{
|
||||
/// @return Ok(TimeSpec) 剩余休眠时间
|
||||
///
|
||||
/// @return Err(SystemError) 错误码
|
||||
pub fn nano_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
pub fn nanosleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
if sleep_time.tv_nsec < 0 || sleep_time.tv_nsec >= 1000000000 {
|
||||
return Err(SystemError::EINVAL);
|
||||
}
|
||||
@ -48,12 +49,13 @@ pub fn nano_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
next_n_us_timer_jiffies((sleep_time.tv_nsec / 1000) as u64),
|
||||
);
|
||||
|
||||
cli();
|
||||
let irq_guard: crate::exception::IrqFlagsGuard =
|
||||
unsafe { CurrentIrqArch::save_and_disable_irq() };
|
||||
timer.activate();
|
||||
unsafe {
|
||||
current_pcb().mark_sleep_interruptible();
|
||||
}
|
||||
sti();
|
||||
drop(irq_guard);
|
||||
|
||||
sched();
|
||||
|
||||
@ -72,8 +74,8 @@ pub fn nano_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
/// @return Ok(TimeSpec) 剩余休眠时间
|
||||
///
|
||||
/// @return Err(SystemError) 错误码
|
||||
pub fn us_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
match nano_sleep(sleep_time) {
|
||||
pub fn usleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
match nanosleep(sleep_time) {
|
||||
Ok(value) => return Ok(value),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
@ -81,40 +83,6 @@ pub fn us_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
|
||||
|
||||
//===== 以下为提供给C的接口 =====
|
||||
|
||||
/// @brief 休眠指定时间(单位:纳秒)(提供给C的接口)
|
||||
///
|
||||
/// @param sleep_time 指定休眠的时间
|
||||
///
|
||||
/// @param rm_time 剩余休眠时间(传出参数)
|
||||
///
|
||||
/// @return Ok(i32) 0
|
||||
///
|
||||
/// @return Err(SystemError) 错误码
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rs_nanosleep(sleep_time: *const timespec, rm_time: *mut timespec) -> i32 {
|
||||
if sleep_time == null_mut() {
|
||||
return SystemError::EINVAL.to_posix_errno();
|
||||
}
|
||||
let slt_spec = TimeSpec {
|
||||
tv_sec: unsafe { *sleep_time }.tv_sec,
|
||||
tv_nsec: unsafe { *sleep_time }.tv_nsec,
|
||||
};
|
||||
|
||||
match nano_sleep(slt_spec) {
|
||||
Ok(value) => {
|
||||
if rm_time != null_mut() {
|
||||
unsafe { *rm_time }.tv_sec = value.tv_sec;
|
||||
unsafe { *rm_time }.tv_nsec = value.tv_nsec;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Err(err) => {
|
||||
return err.to_posix_errno();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief 休眠指定时间(单位:微秒)(提供给C的接口)
|
||||
///
|
||||
/// @param usec 微秒
|
||||
@ -128,7 +96,7 @@ pub extern "C" fn rs_usleep(usec: useconds_t) -> i32 {
|
||||
tv_sec: (usec / 1000000) as i64,
|
||||
tv_nsec: ((usec % 1000000) * 1000) as i64,
|
||||
};
|
||||
match us_sleep(sleep_time) {
|
||||
match usleep(sleep_time) {
|
||||
Ok(_) => {
|
||||
return 0;
|
||||
}
|
||||
|
47
kernel/src/time/syscall.rs
Normal file
47
kernel/src/time/syscall.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use core::ptr::null_mut;
|
||||
|
||||
use crate::{
|
||||
syscall::{Syscall, SystemError},
|
||||
time::{sleep::nanosleep, TimeSpec},
|
||||
};
|
||||
|
||||
impl Syscall {
|
||||
/// @brief 休眠指定时间(单位:纳秒)(提供给C的接口)
|
||||
///
|
||||
/// @param sleep_time 指定休眠的时间
|
||||
///
|
||||
/// @param rm_time 剩余休眠时间(传出参数)
|
||||
///
|
||||
/// @return Ok(i32) 0
|
||||
///
|
||||
/// @return Err(SystemError) 错误码
|
||||
pub fn nanosleep(
|
||||
sleep_time: *const TimeSpec,
|
||||
rm_time: *mut TimeSpec,
|
||||
) -> Result<usize, SystemError> {
|
||||
if sleep_time == null_mut() {
|
||||
return Err(SystemError::EFAULT);
|
||||
}
|
||||
let slt_spec = TimeSpec {
|
||||
tv_sec: unsafe { *sleep_time }.tv_sec,
|
||||
tv_nsec: unsafe { *sleep_time }.tv_nsec,
|
||||
};
|
||||
|
||||
let r: Result<usize, SystemError> = nanosleep(slt_spec).map(|slt_spec| {
|
||||
if rm_time != null_mut() {
|
||||
unsafe { *rm_time }.tv_sec = slt_spec.tv_sec;
|
||||
unsafe { *rm_time }.tv_nsec = slt_spec.tv_nsec;
|
||||
}
|
||||
0
|
||||
});
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/// 获取cpu时间
|
||||
///
|
||||
/// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
|
||||
pub fn clock() -> Result<usize, SystemError> {
|
||||
return Ok(super::timer::clock() as usize);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user