From 232d7936270cf5aec751fe7bdd9989fbc73ecadb Mon Sep 17 00:00:00 2001 From: fslongjin Date: Fri, 8 Apr 2022 21:26:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/driver/timers/HPET/HPET.c | 3 +- kernel/driver/timers/timer.c | 71 +++++++++++++++++++++++++++++++- kernel/driver/timers/timer.h | 40 +++++++++++++++++- kernel/exception/softirq.c | 13 +++--- kernel/main.c | 3 ++ 5 files changed, 118 insertions(+), 12 deletions(-) diff --git a/kernel/driver/timers/HPET/HPET.c b/kernel/driver/timers/HPET/HPET.c index 8ed8bf86..dd987f54 100644 --- a/kernel/driver/timers/HPET/HPET.c +++ b/kernel/driver/timers/HPET/HPET.c @@ -52,7 +52,8 @@ void HPET_handler(uint64_t number, uint64_t param, struct pt_regs *regs) { case 0: // 定时器0中断 ++timer_jiffies; - set_softirq_status(TIMER_SIRQ); + if (container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list)->expire_jiffies <= timer_jiffies) // 若当前时间比定时任务的时间间隔大,则进入中断下半部 + set_softirq_status(TIMER_SIRQ); break; default: diff --git a/kernel/driver/timers/timer.c b/kernel/driver/timers/timer.c index af674634..22998163 100644 --- a/kernel/driver/timers/timer.c +++ b/kernel/driver/timers/timer.c @@ -1,14 +1,81 @@ #include "timer.h" -#include +#include #include +#include + +void test_timer() +{ + printk_color(ORANGE, BLACK, "(test_timer)"); +} void timer_init() { timer_jiffies = 0; + timer_func_init(&timer_func_head, NULL, NULL, -1UL); register_softirq(0, &do_timer_softirq, NULL); + + struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0); + timer_func_init(tmp, &test_timer, NULL, 5); + timer_func_add(tmp); + + kdebug("timer func initialized."); } -void do_timer_softirq(void* data) +void do_timer_softirq(void *data) { + + struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list); + + while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies)) + { + timer_func_del(tmp); + tmp->func(tmp->data); + kfree(tmp); + tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list); + } + printk_color(ORANGE, BLACK, "(HPET%ld)", timer_jiffies); +} + +/** + * @brief 初始化定时功能 + * + * @param timer_func 队列结构体 + * @param func 定时功能处理函数 + * @param expire_jiffies 定时时长 + */ +void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_jiffies) +{ + list_init(&timer_func->list); + timer_func->func = func; + timer_func->data = data, + timer_func->expire_jiffies = timer_jiffies + expire_jiffies; +} + +/** + * @brief 将定时功能添加到列表中 + * + * @param timer_func 待添加的定时功能 + */ +void timer_func_add(struct timer_func_list_t *timer_func) +{ + struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list); + + if (list_empty(&timer_func_head.list) == false) + while (tmp->expire_jiffies < timer_func->expire_jiffies) + tmp = container_of(list_next(&tmp->list), struct timer_func_list_t, list); + + list_add(&tmp->list, &(timer_func->list)); + + +} + +/** + * @brief 将定时功能从列表中删除 + * + * @param timer_func + */ +void timer_func_del(struct timer_func_list_t *timer_func) +{ + list_del(&timer_func->list); } \ No newline at end of file diff --git a/kernel/driver/timers/timer.h b/kernel/driver/timers/timer.h index 5a94fb66..3c57a7e3 100644 --- a/kernel/driver/timers/timer.h +++ b/kernel/driver/timers/timer.h @@ -8,4 +8,42 @@ uint64_t volatile timer_jiffies = 0; // 系统时钟计数 void timer_init(); -void do_timer_softirq(void* data); \ No newline at end of file +void do_timer_softirq(void* data); + +/** + * @brief 定时功能队列 + * + */ +struct timer_func_list_t +{ + struct List list; + uint64_t expire_jiffies; + void (*func)(void* data); + void* data; +}timer_func_head; + +/** + * @brief 初始化定时功能 + * + * @param timer_func 队列结构体 + * @param func 定时功能处理函数 + * @param data 传输的数据 + * @param expire_jiffies 定时时长 + */ +void timer_func_init(struct timer_func_list_t * timer_func, void (*func)(void*data), void*data,uint64_t expire_jiffies); + +/** + * @brief 将定时功能添加到列表中 + * + * @param timer_func 待添加的定时功能 + */ +void timer_func_add(struct timer_func_list_t* timer_func); + +/** + * @brief 将定时功能从列表中删除 + * + * @param timer_func + */ +void timer_func_del(struct timer_func_list_t* timer_func); + + diff --git a/kernel/exception/softirq.c b/kernel/exception/softirq.c index f40373ea..6679b932 100644 --- a/kernel/exception/softirq.c +++ b/kernel/exception/softirq.c @@ -37,21 +37,20 @@ void unregister_softirq(uint32_t irq_num) /** * @brief 软中断处理程序 - * + * */ void do_softirq() { - + sti(); - for(uint32_t i=0;i