除了上一篇文章提到的一些方法,内核还提供了定时器API。其相关内容定义在<linux/timer.h>和kernel/time/timer.c中。
内核定时器可以在将来的某个时间点调度执行某个动作,同时在该点到达之前不会阻塞当前进程。它可以在未来的某个特定时间点(基于时钟滴答)调度执行某个函数,从而可用于完成许多任务。例如,在硬件无法产生中断时,可以周期性的轮询设备状态。
之前提到的schedule_timeout内部就使用了内核定时器的API实现。
其具体数据结构如下:
struct timer_list {
/*
* All fields that change during normal runtime grouped to the
* same cacheline
*/
struct list_head entry;
/* 超时的时间
* 在当前时间大于此时间值时执行function
* 如, jiffies + n*HZ
*/
unsigned long expires;
struct tvec_base *base;
/* 超时后执行的函数 */
void (*function)(unsigned long);
/* function的参数 */
unsigned long data;
int slack;
#ifdef CONFIG_TIMER_STATS
int start_pid;
void *start_site;
char start_comm[16];
#endif
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
};
定时器中调度运行的函数,其运行方式类似于硬件中断发生时的情景。实际上,内核定时器常常是作为"软件中断"的结构而运行的。在这种原子性的上下文中