内核定时器的使用

来源:互联网 发布:优酷 mac 编辑:程序博客网 时间:2024/05/16 01:16

1. 包含的头文件:linux/timer.h

2. 数据类型:struct timer_list;

包含的主要成员:a. data:传递到超时处理函数的参数,主要在多个定时器同时使用时,区别是哪个timer超时。b. expires:定时器超时的时间,以linux的jiffies来衡量。c. void (*function)(unsigned long):定时器超时处理函数。
  • 1
  • 2
  • 3
  • 4
  • 5

3. 主要相关的API函数:

a. init_timer(struct timer_list*):定时器初始化函数;b. add_timer(struct timer_list*):往系统添加定时器;c. mod_timer(struct timer_list *, unsigned long jiffier_timerout):修改定时器的超时时间为jiffies_timerout;d. timer_pending(struct timer_list *):定时器状态查询,如果在系统的定时器列表中则返回1,否则返回0;e. del_timer(struct timer_list*):删除定时器。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4. 时间与jiffies的转换函数:

Linux系统中的jiffies类似于Windows里面的TickCount,它是定义在内核里面的一个全局变量,只是它的单位并不是秒或是毫秒。通常是250个jiffies为一秒,在内核里面可以直接使用宏定义:HZ。这里有几个时间和jiffies的相互转换函数:unsigned int jiffies_to_msecs(unsigned long);unsigned int jiffies_to_usecs(unsigned long);unsigned long msecs_to_jiffies(unsigned int);unsigned long usecs_to_jiffies(unsigned int);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5. 使用简例:

步骤:init_timer->[timer.expires=? & timer.function=?]->add_timer->[mod_timer]->del_timer.
  • 1
  • 2
#include <linux/init.h>#include <linux/module.h>#include <linux/timer.h>struct timer_list timer;void timer_handler(unsigned long data) {    printk(KERN_INFO"timer pending:%d\n", timer_pending(&timer));    mod_timer(&timer, jiffies+msecs_to_jiffies(1000));    printk(KERN_INFO"jiffies:%ld, data:%ld\n", jiffies, data);}int timer_init(void) {    printk(KERN_INFO"%s jiffies:%ld\n", __func__, jiffies);    printk(KERN_INFO"ji:%d,HZ:%d\n", jiffies_to_msecs(250), HZ);    init_timer(&timer);    timer.data = 45;    timer.function = timer_handler;    timer.expires = jiffies + HZ;    add_timer(&timer);    printk(KERN_INFO"timer pending:%d\n", timer_pending(&timer));    return 0;}void timer_exit(void) {    printk(KERN_INFO"%s jiffies:%ld\n", __func__, jiffies);    del_timer(&timer);}module_init(timer_init);module_exit(timer_exit);MODULE_LICENSE("GPL");
内核定时器的数据结构struct timer_list {    struct list_head entry;    unsigned long expires;    void (*function)(unsigned long);    unsigned long data;    struct tvec_base *base;    /* ... */};其中 expires 字段表示期望定时器执行的 jiffies 值,到达该 jiffies 值时,将调用 function 函数,并传递 data 作为参数。当一个定时器被注册到内核之后,entry 字段用来连接该定时器到一个内核链表中。base 字段是内核内部实现所用的。需要注意的是 expires 的值是32位的,因为内核定时器并不适用于长的未来时间点。初始化在使用 struct timer_list 之前,需要初始化该数据结构,确保所有的字段都被正确地设置。初始化有两种方法。方法一:DEFINE_TIMER(timer_name, function_name, expires_value, data);该宏会静态创建一个名叫 timer_name 内核定时器,并初始化其 function, expires, name 和 base 字段。方法二:struct timer_list mytimer;setup_timer(&mytimer, (*function)(unsigned long), unsigned long data);mytimer.expires = jiffies + 5*HZ;方法3:struct timer_list mytimer;init_timer(&mytimer);      mytimer ->timer.expires = jiffies + 5*HZ;  mytimer ->timer.data = (unsigned long) dev;  mytimer ->timer.function = &corkscrew_timer; /* timer handler */通过init_timer()动态地定义一个定时器,此后,将处理函数的地址和参数绑定给一个timer_list,注意,无论用哪种方法初始化,其本质都只是给字段赋值,所以只要在运行 add_timer() 之前,expires, function 和 data 字段都可以直接再修改。关于上面这些宏和函数的定义,参见 include/linux/timer.h。注册定时器要生效,还必须被连接到内核专门的链表中,这可以通过 add_timer(struct timer_list *timer) 来实现。重新注册要修改一个定时器的调度时间,可以通过调用 mod_timer(struct timer_list *timer, unsigned long expires)。mod_timer() 会重新注册定时器到内核,而不管定时器函数是否被运行过。注销注销一个定时器,可以通过 del_timer(struct timer_list *timer) 或 del_timer_sync(struct timer_list *timer)。其中 del_timer_sync 是用在 SMP 系统上的(在非SMP系统上,它等于del_timer),当要被注销的定时器函数正在另一个 cpu 上运行时,del_timer_sync() 会等待其运行完,所以这个函数会休眠。另外还应避免它和被调度的函数争用同一个锁。对于一个已经被运行过且没有重新注册自己的定时器而言,注销函数其实也没什么事可做。 int timer_pending(const struct timer_list *timer)这个函数用来判断一个定时器是否被添加到了内核链表中以等待被调度运行。注意,当一个定时器函数即将要被运行前,内核会把相应的定时器从内核链表中删除(相当于注销)一个简单的例子#include <linux/module.h>#include <linux/timer.h>#include <linux/jiffies.h>struct timer_list mytimer;static void myfunc(unsigned long data){        printk("%s/n", (char *)data);        mod_timer(&mytimer, jiffies + 2*HZ);}static int __init mytimer_init(void){        setup_timer(&mytimer, myfunc, (unsigned long)"Hello, world!");        mytimer.expires = jiffies + HZ;        add_timer(&mytimer);        return 0;}static void __exit mytimer_exit(void){        del_timer(&mytimer);} module_init(mytimer_init);module_exit(mytimer_exit);例子2static struct timer_list power_button_poll_timer;static void power_button_poll(unsigned long dummy){ if (gpio_line_get(N2100_POWER_BUTTON) == 0) {  ctrl_alt_del();  return; } power_button_poll_timer.expires = jiffies + (HZ / 10); add_timer(&power_button_poll_timer);}  static void __init n2100_init_machine(void){;; init_timer(&power_button_poll_timer); power_button_poll_timer.function = power_button_poll; power_button_poll_timer.expires = jiffies + (HZ / 10); add_timer(&power_button_poll_timer);} 例子3 设备open时初始化和注册定时器 static int corkscrew_open(struct net_device *dev) {;;  init_timer(&vp->timer);      vp->timer.expires = jiffies + media_tbl[dev->if_port].wait;  vp->timer.data = (unsigned long) dev;  vp->timer.function = &corkscrew_timer; /* timer handler */  add_timer(&vp->timer);:;}定时器超时处理函数,对定时器的超时时间重新赋值 static void corkscrew_timer(unsigned long data){;;    vp->timer.expires = jiffies + media_tbl[dev->if_port].wait;    add_timer(&vp->timer);;;} 设备close时删除定时器static int corkscrew_close(struct net_device *dev){ ;; del_timer(&vp->timer);;;} 例子4本例子用DEFINE_TIMER静态创建定时器#include <linux/module.h>#include <linux/jiffies.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/timer.h>#include <linux/leds.h> static void ledtrig_ide_timerfunc(unsigned long data); DEFINE_LED_TRIGGER(ledtrig_ide);static DEFINE_TIMER(ledtrig_ide_timer, ledtrig_ide_timerfunc, 0, 0);static int ide_activity;static int ide_lastactivity; void ledtrig_ide_activity(void){       ide_activity++;       if (!timer_pending(&ledtrig_ide_timer))              mod_timer(&ledtrig_ide_timer, jiffies + msecs_to_jiffies(10));}EXPORT_SYMBOL(ledtrig_ide_activity); static void ledtrig_ide_timerfunc(unsigned long data){       if (ide_lastactivity != ide_activity) {              ide_lastactivity = ide_activity;              led_trigger_event(ledtrig_ide, LED_FULL);              mod_timer(&ledtrig_ide_timer, jiffies + msecs_to_jiffies(10));       } else {              led_trigger_event(ledtrig_ide, LED_OFF);       }} static int __init ledtrig_ide_init(void){       led_trigger_register_simple("ide-disk", &ledtrig_ide);       return 0;} static void __exit ledtrig_ide_exit(void){       led_trigger_unregister_simple(ledtrig_ide);} module_init(ledtrig_ide_init);module_exit(ledtrig_ide_exit);  ==================================================================================add_timer() -- 将定时器添加到定时器等待队列中2007年08月04日 星期六 15:30 用add_timer()函数来看timer_base的作用static inline void add_timer(struct timer_list *timer){    BUG_ON(timer_pending(timer));    __mod_timer(timer, timer->expires);}int __mod_timer(struct timer_list *timer, unsigned long expires){    tvec_base_t *base, *new_base;    unsigned long flags;    int ret = 0;    timer_stats_timer_set_start_info(timer);    BUG_ON(!timer->function);    base = lock_timer_base(timer, &flags);如果timer已经放到定时链表中,则释放开|--------------------------------||   if (timer_pending(timer)) { -||       detach_timer(timer, 0); -||       ret = 1;                 ||   }                            ||--------------------------------|获取当前CPU的timer base|-----------------------------------------||   new_base = __get_cpu_var(tvec_bases); ||-----------------------------------------|如果当前CPU的timer base不是当前timer中的base, 更新timer的base|----------------------------------------------------||   if (base != new_base) {                          ||       if (likely(base->running_timer != timer)) { -||           timer->base = NULL;                      ||           spin_unlock(&base->lock);                ||           base = new_base;                         ||           spin_lock(&base->lock);                  ||           timer->base = base;                      ||       }                                            ||   }                                                ||----------------------------------------------------|给定时器timer设置超时时间;并添加该时钟|-------------------------------------||   timer->expires = expires;         ||   internal_add_timer(base, timer); -||-------------------------------------|    spin_unlock_irqrestore(&base->lock, flags);    return ret;} MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");MODULE_DESCRIPTION("LED IDE Disk Activity Trigger");MODULE_LICENSE("GPL");  总的来说,timer的用法还是很简单的。主要需要定义一个timer_list变量timer、先初始化timer   init_timer(&timer);   then 对timer的相关参数赋值:   timer.function = fun;   timer.expires = jiffies + TIMER_DELAY;   add_timer(&timer);   在定时器时间到的时候,会执行fun,如果继续定时,可以通过   在fun中执行   mod_timer(&timer, jiffies + TIMER_DELAY);   在不需要的时候通过调用   del_timer(&timer);   删除定时器。   简单吧。这样一个简单的定时器就完成了。   呵呵。   附程序:     #include <linux/module.h>  #include <linux/types.h>  #include <linux/fs.h>  #include <linux/errno.h>  #include <linux/mm.h>  #include <linux/sched.h>  #include <linux/init.h>  #include <linux/cdev.h>  #include <asm/io.h>  #include <asm/system.h>  #include <asm/uaccess.h>  #include <linux/timer.h>  #include <asm/atomic.h>  #define SECOND_MAJOR 0  static int second_major = SECOND_MAJOR;  struct second_dev  {  struct cdev cdev;  atomic_t counter;  struct timer_list s_timer;  };  struct second_dev *second_devp;  static void second_timer_handle(unsigned long arg)  {  mod_timer(&second_devp->s_timer, jiffies + HZ);  atomic_inc(&second_devp->counter);  printk(KERN_ERR "current jiffies is %ld/n",jiffies);  }  int second_open(struct inode *inode, struct file *filp)  {  init_timer(&second_devp->s_timer);  second_devp->s_timer.function = &second_timer_handle;  second_devp->s_timer.expires = jiffies + HZ;  add_timer(&second_devp->s_timer);  atomic_set(&second_devp->counter, 0);  return 0;   }  int second_release(struct inode *inode, struct file *filp)  {  del_timer(&second_devp->s_timer);  return 0;  }  static ssize_t second_read(struct file *filp, char __user *buf, size_t count,   loff_t *ppos)  {  int counter;  counter = atomic_read(&second_devp->counter);  if (put_user(counter, (int *)buf))  {  return -EFAULT;  }else  {  return sizeof(unsigned int);  }  }  static const struct file_operations second_fops =  {  .owner = THIS_MODULE,  .open = second_open,  .release = second_release,  .read = second_read,  };  static void second_setup_cdev(struct second_dev *dev, int index)  {  int err, devno = MKDEV(second_major, index);  cdev_init(&dev->cdev, &second_fops);  dev->cdev.owner = THIS_MODULE;  dev->cdev.ops = &second_fops;  err = cdev_add(&dev->cdev, devno, 1);  if (err)  {  printk(KERN_NOTICE "Error %d add second%d", err, index);  }  }  int second_init(void)  {  int ret;  dev_t devno = MKDEV(second_major, 0);  if (second_major)  {  ret = register_chrdev_region(devno, 1, "second");  }else  {  ret = alloc_chrdev_region(&devno, 0, 1, "second");  second_major = MAJOR(devno);  }  if (ret < 0)  {  return ret;  }  second_devp = kmalloc(sizeof(struct second_dev), GFP_KERNEL);  if (!second_devp)  {  ret = -ENOMEM;  goto fail_malloc;  }  memset(second_devp, 0, sizeof(struct second_dev));  second_setup_cdev(second_devp, 0);  return 0;  fail_malloc:  unregister_chrdev_region(devno, 1);  }  void second_exit(void)  {  cdev_del(&second_devp->cdev);  kfree(second_devp);  unregister_chrdev_region(MKDEV(second_major, 0), 1);  }  MODULE_AUTHOR("Song Baohua");  MODULE_LICENSE("Dual BSD/GPL");  module_param(second_major, int, S_IRUGO);  module_init(second_init);  module_exit(second_exit);  附上用户端的测试程序:  #include <stdio.h>  #include <unistd.h>  #include <fcntl.h>  int main(void)  {  int fd, i;  int data;  fd = open("/dev/second",O_RDONLY);  if (fd < 0)  {  printf("open /dev/second error/n");  }  for(i = 0; i < 20; i++)  {  read(fd, &data, sizeof(data));  printf("read /dev/second is %d/n",data);  sleep(1);  }  close(fd);  }
复制代码

原创粉丝点击