1 #include "timer.h"
2 #include <common/kprint.h>
3 #include <driver/timers/HPET/HPET.h>
4 #include <exception/softirq.h>
5 #include <mm/slab.h>
6 #include <process/process.h>
7 #include <sched/sched.h>
8 
9 struct timer_func_list_t timer_func_head;
10 static spinlock_t sched_lock;
11 
12 // 定时器循环阈值,每次最大执行20个定时器任务
13 #define TIMER_RUN_CYCLE_THRESHOLD 20
14 
test_timer()15 void test_timer()
16 {
17     printk_color(ORANGE, BLACK, "(test_timer)");
18 }
19 
timer_init()20 void timer_init()
21 {
22     spin_init(&sched_lock);
23 
24     timer_jiffies = 0;
25     timer_func_init(&timer_func_head, NULL, NULL, -1UL);
26     register_softirq(TIMER_SIRQ, &do_timer_softirq, NULL);
27 
28     struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
29     timer_func_init(tmp, &test_timer, NULL, 5);
30     timer_func_add(tmp);
31 
32     kdebug("timer func initialized.");
33 }
34 
35 /**
36  * @brief 处理时间软中断
37  *
38  * @param data
39  */
do_timer_softirq(void * data)40 void do_timer_softirq(void *data)
41 {
42     // todo: 修改这里以及 softirq 的部分,使得 timer 具有并行性
43     struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
44     int cycle_count = 0;
45     while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies))
46     {
47         spin_lock(&sched_lock);
48 
49         timer_func_del(tmp);
50         tmp->func(tmp->data);
51         kfree(tmp);
52 
53         spin_unlock(&sched_lock);
54 
55         ++cycle_count;
56 
57 
58         // kdebug("SOLVE SOFT IRQ %d", cycle_count);
59 
60         // 当前定时器达到阈值
61         if (cycle_count == TIMER_RUN_CYCLE_THRESHOLD)
62             break;
63         tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
64     }
65 }
66 
67 /**
68  * @brief 初始化定时功能
69  *
70  * @param timer_func 队列结构体
71  * @param func 定时功能处理函数
72  * @param data 传输的数据
73  * @param expire_ms 定时时长(单位:ms)
74  */
timer_func_init(struct timer_func_list_t * timer_func,void (* func)(void * data),void * data,uint64_t expire_ms)75 void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms)
76 {
77     list_init(&timer_func->list);
78     timer_func->func = func;
79     timer_func->data = data;
80     timer_func->expire_jiffies = cal_next_n_ms_jiffies(expire_ms); // 设置过期的时间片
81 }
82 
83 /**
84  * @brief 初始化定时功能
85  *
86  * @param timer_func 队列结构体
87  * @param func 定时功能处理函数
88  * @param data 传输的数据
89  * @param expire_us 定时时长(单位:us)
90  */
timer_func_init_us(struct timer_func_list_t * timer_func,void (* func)(void * data),void * data,uint64_t expire_us)91 void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us)
92 {
93     list_init(&timer_func->list);
94     timer_func->func = func;
95     timer_func->data = data;
96     timer_func->expire_jiffies = cal_next_n_us_jiffies(expire_us); // 设置过期的时间片
97     // kdebug("timer_func->expire_jiffies=%ld",cal_next_n_us_jiffies(expire_us));
98 }
99 
100 /**
101  * @brief 将定时功能添加到列表中
102  *
103  * @param timer_func 待添加的定时功能
104  */
timer_func_add(struct timer_func_list_t * timer_func)105 void timer_func_add(struct timer_func_list_t *timer_func)
106 {
107     struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
108 
109     if (list_empty(&timer_func_head.list) == false)
110         while (tmp->expire_jiffies < timer_func->expire_jiffies)
111             tmp = container_of(list_next(&tmp->list), struct timer_func_list_t, list);
112 
113     list_add(&tmp->list, &(timer_func->list));
114 }
115 
116 /**
117  * @brief 将定时功能从列表中删除
118  *
119  * @param timer_func
120  */
timer_func_del(struct timer_func_list_t * timer_func)121 void timer_func_del(struct timer_func_list_t *timer_func)
122 {
123     list_del(&timer_func->list);
124 }
125 
sys_clock(struct pt_regs * regs)126 uint64_t sys_clock(struct pt_regs *regs)
127 {
128     return timer_jiffies;
129 }
130 
clock()131 uint64_t clock()
132 {
133     return timer_jiffies;
134 }
135 
136 /**
137  * @brief 辅助函数:传进schedule_timeout函数中, 然后时间一到就唤醒 pcb 指向的进程(即自身)
138  *
139  * @param pcb process_control_block
140  */
__wake_up_helper(void * pcb)141 static void __wake_up_helper(void *pcb)
142 {
143     BUG_ON(pcb == NULL);
144 
145     BUG_ON(process_wakeup((struct process_control_block *)pcb) != 0); // 正常唤醒,返回值为0
146 }
147 
148 /**
149  * @brief 睡眠timeout的时间之后唤醒进程/线程
150  *
151  * @param timeout
152  * @return long
153  */
schedule_timeout_ms(long timeout)154 long schedule_timeout_ms(long timeout)
155 {
156     if (timeout == MAX_TIMEOUT) // 无期停止, 意味着不会调用func
157     {
158         sched();
159         return MAX_TIMEOUT;
160     }
161     else if (timeout < 0)
162     {
163         BUG_ON(1);
164         return 0;
165     }
166 
167     spin_lock(&sched_lock);
168     struct timer_func_list_t timer={0};
169     timer_func_init(&timer, &__wake_up_helper, current_pcb, timeout);
170     timer_func_add(&timer);
171     current_pcb->state &= ~(PROC_RUNNING);
172     spin_unlock(&sched_lock);
173     sched();
174 
175     timeout -= timer_jiffies;
176 
177     return timeout < 0 ? 0 : timeout;
178 }