DragonOS/kernel/time/timer.c

119 lines
3.3 KiB
C
Raw Normal View History

2022-04-08 12:04:12 +00:00
#include "timer.h"
2022-04-08 13:26:42 +00:00
#include <common/kprint.h>
2022-04-08 12:04:12 +00:00
#include <exception/softirq.h>
2022-04-08 13:26:42 +00:00
#include <mm/slab.h>
2022-05-20 11:37:26 +00:00
#include <driver/timers/HPET/HPET.h>
2022-06-07 15:18:26 +00:00
#include <process/process.h>
2022-04-08 13:26:42 +00:00
struct timer_func_list_t timer_func_head;
// 定时器循环阈值每次最大执行10个定时器任务
#define TIMER_RUN_CYCLE_THRESHOLD 10
2022-04-08 13:26:42 +00:00
void test_timer()
{
printk_color(ORANGE, BLACK, "(test_timer)");
}
2022-04-08 12:04:12 +00:00
void timer_init()
{
timer_jiffies = 0;
2022-04-08 13:26:42 +00:00
timer_func_init(&timer_func_head, NULL, NULL, -1UL);
register_softirq(TIMER_SIRQ, &do_timer_softirq, NULL);
2022-04-08 13:26:42 +00:00
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.");
2022-04-08 12:04:12 +00:00
}
2022-04-08 13:26:42 +00:00
void do_timer_softirq(void *data)
2022-04-08 12:04:12 +00:00
{
2022-07-12 04:01:51 +00:00
// todo: 修改这里以及softirq的部分使得timer具有并行性
2022-04-08 13:26:42 +00:00
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
int cycle_count = 0;
2022-04-08 13:26:42 +00:00
while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies))
{
2022-07-12 05:20:01 +00:00
2022-04-08 13:26:42 +00:00
timer_func_del(tmp);
tmp->func(tmp->data);
kfree(tmp);
++cycle_count;
// 当前定时器达到阈值
2022-07-12 05:20:01 +00:00
if (cycle_count == TIMER_RUN_CYCLE_THRESHOLD)
break;
tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
2022-07-12 05:20:01 +00:00
}
2022-04-08 13:26:42 +00:00
}
/**
* @brief
*
* @param timer_func
* @param func
2022-05-20 11:37:26 +00:00
* @param data
* @param expire_ms (ms)
2022-04-08 13:26:42 +00:00
*/
2022-05-20 11:37:26 +00:00
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms)
2022-04-08 13:26:42 +00:00
{
list_init(&timer_func->list);
timer_func->func = func;
2022-07-12 04:01:51 +00:00
timer_func->data = data;
timer_func->expire_jiffies = cal_next_n_ms_jiffies(expire_ms); // 设置过期的时间片
2022-04-08 13:26:42 +00:00
}
2022-07-12 04:01:51 +00:00
/**
* @brief
*
* @param timer_func
* @param func
* @param data
* @param expire_us (us)
*/
void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us)
{
list_init(&timer_func->list);
timer_func->func = func;
timer_func->data = data;
timer_func->expire_jiffies = cal_next_n_us_jiffies(expire_us); // 设置过期的时间片
// kdebug("timer_func->expire_jiffies=%ld",cal_next_n_us_jiffies(expire_us));
}
2022-04-08 13:26:42 +00:00
/**
* @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);
2022-07-12 05:20:01 +00:00
}
uint64_t sys_clock(struct pt_regs *regs)
{
return timer_jiffies;
}
uint64_t clock()
{
return timer_jiffies;
}