mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-09 07:06:47 +00:00
🆕 使用rdtsc进行精确定时
This commit is contained in:
parent
4bc64de8f9
commit
90203803d3
@ -5,6 +5,7 @@
|
|||||||
#include <common/stdio.h>
|
#include <common/stdio.h>
|
||||||
#include <common/compiler.h>
|
#include <common/compiler.h>
|
||||||
#include <common/libELF/elf.h>
|
#include <common/libELF/elf.h>
|
||||||
|
#include <common/time.h>
|
||||||
#include <driver/video/video.h>
|
#include <driver/video/video.h>
|
||||||
#include <driver/usb/usb.h>
|
#include <driver/usb/usb.h>
|
||||||
#include <exception/gate.h>
|
#include <exception/gate.h>
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
#include <process/process.h>
|
#include <process/process.h>
|
||||||
#include <sched/sched.h>
|
#include <sched/sched.h>
|
||||||
#include <mm/slab.h>
|
#include <mm/slab.h>
|
||||||
|
#include <common/cpu.h>
|
||||||
|
#include <common/glib.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief nanosleep定时事件到期后,唤醒指定的进程
|
* @brief nanosleep定时事件到期后,唤醒指定的进程
|
||||||
*
|
*
|
||||||
@ -24,24 +27,30 @@ void nanosleep_handler(void *pcb)
|
|||||||
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
||||||
{
|
{
|
||||||
int64_t total_ns = rqtp->tv_nsec;
|
int64_t total_ns = rqtp->tv_nsec;
|
||||||
// kdebug("totalns = %ld", total_ns);
|
|
||||||
if (total_ns < 0 || total_ns >= 1000000000)
|
if (total_ns < 0 || total_ns >= 1000000000)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
// todo: 对于小于500us的时间,使用spin/rdtsc来进行定时
|
// 对于小于500us的时间,使用spin/rdtsc来进行定时
|
||||||
if (total_ns < 50000)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (total_ns < 500000)
|
if (total_ns < 500000)
|
||||||
total_ns = 500000;
|
{
|
||||||
|
kdebug("use rdtsc to nanosleep");
|
||||||
|
uint64_t expired_tsc = rdtsc() + (total_ns * Cpu_tsc_freq) / 1000000000;
|
||||||
|
while (rdtsc() < expired_tsc)
|
||||||
|
pause();
|
||||||
|
|
||||||
|
if (rmtp != NULL)
|
||||||
|
{
|
||||||
|
rmtp->tv_nsec = 0;
|
||||||
|
rmtp->tv_sec = 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// 增加定时任务
|
// 增加定时任务
|
||||||
struct timer_func_list_t *sleep_task = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
|
struct timer_func_list_t *sleep_task = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
|
||||||
memset(sleep_task, 0, sizeof(struct timer_func_list_t));
|
memset(sleep_task, 0, sizeof(struct timer_func_list_t));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
timer_func_init_us(sleep_task, &nanosleep_handler, (void *)current_pcb, total_ns / 1000);
|
timer_func_init_us(sleep_task, &nanosleep_handler, (void *)current_pcb, total_ns / 1000);
|
||||||
|
|
||||||
timer_func_add(sleep_task);
|
timer_func_add(sleep_task);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user