Chinaunix首页 | 论坛 | 博客
  • 博客访问: 290059
  • 博文数量: 186
  • 博客积分: 1531
  • 博客等级: 上尉
  • 技术积分: 1275
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-06 16:56
文章分类

全部博文(186)

文章存档

2013年(2)

2012年(184)

分类:

2012-11-09 10:15:07

原文地址:linux timer机制 作者:linux_hope

#include
#include
#include
#include
#include


//#define del_work
#define workqueu
//#define hrtime
//#define list_timer

struct timer{
    struct delayed_work delaywork;
    struct workqueue_struct *monitor_wqueue;
    struct hrtimer hrtimer;
    struct work_struct workstruct;
    struct timer_list timerlist;
};

void timer_work(struct work_struct *work)
{
    static int count = 0;
#ifdef del_work
    struct timer *timer_fun = container_of(work, struct timer, delaywork.work);
    schedule_delayed_work(&timer_fun->delaywork, msecs_to_jiffies(1000));
    if(count == 10){
        cancel_delayed_work(&timer_fun->delaywork);
    }
#endif
#ifdef workqueu
    struct timer *timer_fun = container_of(work, struct timer, delaywork.work);
    queue_delayed_work(timer_fun->monitor_wqueue, &timer_fun->delaywork,HZ);
    
    if(count == 10){
        cancel_delayed_work(&timer_fun->delaywork);
    }
#endif
#ifdef hrtime    
    struct timer *timer_fun = container_of(work, struct timer, workstruct);
    if(count < 10){
        hrtimer_start(&timer_fun->hrtimer, ktime_set(1,0), HRTIMER_MODE_REL);
    }
#endif
    printk("count:%d\n",count++);
}
#ifdef hrtime
static enum hrtimer_restart hrtimer_test(struct hrtimer *handle)
{
    printk("=============%s=================\n",__FUNCTION__);
    struct timer *td = container_of(handle, struct timer, hrtimer);
    schedule_work(&td->workstruct);
    return HRTIMER_NORESTART;
}
#endif
#ifdef list_timer
static void timerlist_work(unsigned long _data)
{
    struct timer *data = (struct timer *)_data;
    static int count = 0;
    if(count < 10){
        mod_timer(&data->timerlist, jiffies + msecs_to_jiffies(1000));
    }
    printk("%d seconds\n",count++);
}
#endif
int __init timer_init(void)
{
    struct timer *timer = NULL;
    printk("==========timer_init=============\n");

    timer = kzalloc(sizeof(struct timer), GFP_KERNEL);
    if (!timer) {
        return -ENOMEM;
    }

#ifdef del_work
    INIT_DELAYED_WORK(&timer->delaywork, timer_work);
    schedule_delayed_work(&timer->delaywork, msecs_to_jiffies(1000));
#endif
#ifdef workqueu    
    INIT_DELAYED_WORK(&timer->delaywork, timer_work);
    timer->monitor_wqueue =
        create_singlethread_workqueue("timer_test");
    if (!timer->monitor_wqueue) {
        return -ESRCH;
    }
    queue_delayed_work(timer->monitor_wqueue, &timer->delaywork,HZ);
#endif
#ifdef hrtime
    INIT_WORK(&timer->workstruct, timer_work);
    hrtimer_init(&timer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    timer->hrtimer.function = hrtimer_test;
    hrtimer_start(&timer->hrtimer, ktime_set(1,0), HRTIMER_MODE_REL);
#endif    
#ifdef list_timer
    setup_timer(&timer->timerlist, timerlist_work, (unsigned long)timer);    
    mod_timer(&timer->timerlist, jiffies + msecs_to_jiffies(1000));
#endif
    return 0;
}


void __exit timer_exit(void)
{
    printk("============timer_exit===============\n");
}

module_init(timer_init);
module_exit(timer_exit);

阅读(522) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~