123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include "timer.h"
- #include <common/kprint.h>
- #include <driver/timers/HPET/HPET.h>
- #include <exception/softirq.h>
- #include <mm/slab.h>
- #include <process/process.h>
- #include <sched/sched.h>
- struct timer_func_list_t timer_func_head;
- static spinlock_t sched_lock;
- #define TIMER_RUN_CYCLE_THRESHOLD 20
- void test_timer()
- {
- printk_color(ORANGE, BLACK, "(test_timer)");
- }
- void timer_init()
- {
- spin_init(&sched_lock);
- timer_jiffies = 0;
- timer_func_init(&timer_func_head, NULL, NULL, -1UL);
- register_softirq(TIMER_SIRQ, &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)
- {
-
- struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
- int cycle_count = 0;
- while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies))
- {
- spin_lock(&sched_lock);
- timer_func_del(tmp);
- tmp->func(tmp->data);
- kfree(tmp);
- spin_unlock(&sched_lock);
- ++cycle_count;
-
-
- if (cycle_count == TIMER_RUN_CYCLE_THRESHOLD)
- break;
- tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
- }
- }
- void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms)
- {
- list_init(&timer_func->list);
- timer_func->func = func;
- timer_func->data = data;
- timer_func->expire_jiffies = cal_next_n_ms_jiffies(expire_ms);
- }
- 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);
-
- }
- 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));
- }
- void timer_func_del(struct timer_func_list_t *timer_func)
- {
- list_del(&timer_func->list);
- }
- uint64_t sys_clock(struct pt_regs *regs)
- {
- return timer_jiffies;
- }
- uint64_t clock()
- {
- return timer_jiffies;
- }
- static void __wake_up_helper(void *pcb)
- {
- BUG_ON(pcb == NULL);
- BUG_ON(process_wakeup((struct process_control_block *)pcb) != 0);
- }
- long schedule_timeout_ms(long timeout)
- {
- if (timeout == MAX_TIMEOUT)
- {
- sched();
- return MAX_TIMEOUT;
- }
- else if (timeout < 0)
- {
- BUG_ON(1);
- return 0;
- }
- spin_lock(&sched_lock);
- struct timer_func_list_t timer={0};
- timer_func_init(&timer, &__wake_up_helper, current_pcb, timeout);
- timer_func_add(&timer);
- current_pcb->state &= ~(PROC_RUNNING);
- spin_unlock(&sched_lock);
- sched();
- timeout -= timer_jiffies;
- return timeout < 0 ? 0 : timeout;
- }
|