linux核心定時器的使用說明

來源:互聯網
上載者:User

     核心定時器的使用

LINUX核心定時器是核心用來控制在未來某個時間點(基於jiffies)調度執行某個函數的一種機制,其實現位於<linux/timer.h> 和 kernel/timer.c 檔案中。

被調度的函數肯定是非同步執行的,它類似於一種“軟體中斷”,而且是處於非進程的上下文中,所以調度函數必須遵守以下規則:

1) 沒有 current 指標、不允許訪問使用者空間。因為沒有進程上下文,相關代碼和被中斷的進程沒有任何聯絡。

2) 不能執行休眠(或可能引起休眠的函數)和調度。

3) 任何被訪問的資料結構都應該針對並發訪問進行保護,以防止競爭條件。

 

核心定時器的調度函數運行過一次後就不會再被運行了(相當於自動登出),但可以通過在被調度的函數中重新調度自己來周期運行。

 

在SMP系統中,調度函數總是在註冊它的同一CPU上運行,以儘可能獲得緩衝的局域性。

 

定時器API

 

核心定時器的資料結構

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);

 

例子2

static 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);

 

MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");

MODULE_DESCRIPTION("LED IDE Disk Activity Trigger");

MODULE_LICENSE("GPL");

 

利用jiffies計算核心定時時間  見以下網址:有關於延時定時的詳細解釋

http://blog.csdn.net/zhandoushi1982/article/details/5536210

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.