From 90203803d3d8ddf7d2c9a3a0a74d898b8a4c6672 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Wed, 27 Jul 2022 21:19:59 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E4=BD=BF=E7=94=A8rdtsc=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=B2=BE=E7=A1=AE=E5=AE=9A=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/process/process.c | 1 + kernel/time/sleep.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/kernel/process/process.c b/kernel/process/process.c index 205565b4..36bcadd9 100644 --- a/kernel/process/process.c +++ b/kernel/process/process.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/kernel/time/sleep.c b/kernel/time/sleep.c index 6646c205..05be1685 100644 --- a/kernel/time/sleep.c +++ b/kernel/time/sleep.c @@ -4,6 +4,9 @@ #include #include #include +#include +#include + /** * @brief nanosleep定时事件到期后,唤醒指定的进程 * @@ -24,26 +27,32 @@ void nanosleep_handler(void *pcb) int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { int64_t total_ns = rqtp->tv_nsec; - // kdebug("totalns = %ld", total_ns); + if (total_ns < 0 || total_ns >= 1000000000) return -EINVAL; - // todo: 对于小于500us的时间,使用spin/rdtsc来进行定时 - if (total_ns < 50000) - return 0; - + // 对于小于500us的时间,使用spin/rdtsc来进行定时 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); 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_add(sleep_task); current_pcb->state = PROC_INTERRUPTIBLE;