Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99735
  • 博文数量: 23
  • 博客积分: 516
  • 博客等级: 中士
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-29 16:45
文章分类
文章存档

2012年(11)

2011年(12)

分类: LINUX

2012-03-13 15:31:30

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

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