RTC模块产生tick中断和报警。
内核在时钟中断发生(tick)后检测各个定时器是否到期。到期后的定时器处理函数将作为软中断在底半部执行。调用run_local_timers(void)处理TIMER_SOFTIRQ软中断。
/*
* Called by the local, per-CPU timer interrupt on SMP.
*/
void run_local_timers(void)
{
hrtimer_run_queues();
raise_softirq(TIMER_SOFTIRQ);
softlockup_tick();
}
1. 初始化定时器
struct timer_list {
struct list_head entry; //定时器列表
unsigned long expires; //到期时间
void (*function)(unsigned long);//定时器出来函数
unsigned long data;//传入的参数
};
#define init_timer(timer)\
init_timer_key((timer), NULL, NULL)
_init_timer对timer_list结构进行初始化
static void __init_timer(struct timer_list *timer,
const char *name,
struct lock_class_key *key)
{
timer->entry.next = NULL;
timer->base = __raw_get_cpu_var(tvec_bases);//获取定时器的基准时间
#ifdef CONFIG_TIMER_STATS
timer->start_site = NULL;
timer->start_pid = -1;
memset(timer->start_comm, 0, TASK_COMM_LEN);
#endif
lockdep_init_map(&timer->lockdep_map, name, key, 0);
}
2. 增加定时器:
Add_timer(struct timer_list * timer);
添加到内核定时器链表中,先入先出,最终会用到:
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}