Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153816
  • 博文数量: 28
  • 博客积分: 1476
  • 博客等级: 上尉
  • 技术积分: 356
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-11 11:39
文章分类
文章存档

2011年(1)

2010年(18)

2009年(9)

我的朋友

分类:

2010-05-27 18:12:21


第一种使用schedule_work手动调度:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/time.h>
#include <linux/workqueue.h>
#include <asm/atomic.h>
#include <linux/delay.h>

MODULE_AUTHOR("lcw");
MODULE_LICENSE("GPL");

struct timer_data test_data;
struct timer_data test_data1;
static struct workqueue_struct *test_workqueue;
atomic_t wq_run_times;
unsigned int failed_cnt = 0;


static void do_work(void *);
static void do_work1(void *);

static struct work_struct test_work;
static struct work_struct test_work1;


static void do_work(void *arg)
{
    printk("goodbye\n");
    mdelay(2000);
    schedule_work(&test_work1);
}

static void do_work1(void *arg)
{
    printk("hello world\n");
    mdelay(2000);
    schedule_work(&test_work);
}
int wq_init(void)
{
    INIT_WORK(&test_work, do_work);
    INIT_WORK(&test_work1, do_work1)

    test_workqueue = create_singlethread_workqueue("test-wq");
    schedule_work(&test_work);
    printk("test-wq init success\n");
    printk("jiffies: %lu\n", jiffies);

    return 0;
}

void wq_exit(void)
{
    destroy_workqueue(test_workqueue);
    printk("wq exit success\n");
}

module_init(wq_init);
module_exit(wq_exit);


第二种使用queue_work和定时器自动调度:

#include linux/init.h>
#include linux/module.h>
#include linux/moduleparam.h>
#include linux/time.h>
#include linux/timer.h>
#include linux/workqueue.h>
#include asm/atomic.h>
MODULE_AUTHOR("lcw");
MODULE_LICENSE("GPL");


struct timer_data {
        struct timer_list timer;
        struct workqueue_struct *work_queue;
        unsigned long prev_jiffies;
        unsigned int loops;
};


struct timer_data test_data;
struct timer_data test_data1;
static void do_work(void *);
static DECLARE_WORK(test_work, do_work, NULL);
static DECLARE_WORK(test_work1, do_work, NULL);
static struct workqueue_struct *test_workqueue;
atomic_t wq_run_times;
unsigned int failed_cnt = 0;


void test_timer_fn(unsigned long arg)
{
        struct timer_data *data = (struct timer_data *)arg;


        unsigned long j = jiffies;
        data->timer.expires += 2 * HZ;
        data->prev_jiffies = j;
        add_timer(&data->timer);


        if (queue_work(test_workqueue, &test_work) == 0) {
                printk("Timer (0) add work queue failed\n");
                (*(&failed_cnt))++;
        }
        data->loops++;
        printk("timer-0 loops: %u\n", data->loops);
}


void test_timer_fn1(unsigned long arg)
{
        struct timer_data *data = (struct timer_data *)arg;
        unsigned long j = jiffies;
        data->timer.expires += 3 * HZ;
        data->prev_jiffies = j;
        add_timer(&data->timer);


        //if (queue_work(test_workqueue, &test_work) == 0) {
        if (queue_work(test_workqueue, &test_work1) == 0) {
                printk("Timer (1) add work queue failed\n");
                (*(&failed_cnt))++;
        }
        data->loops++;
        printk("timer-1 loops: %u\n", data->loops);
}


void do_work(void *arg)
{
        atomic_inc(&wq_run_times);
        printk("====work queue run times: %u====\n", atomic_read(&wq_run_times));
        printk("====failed count: %u====\n", *(&failed_cnt));
}


int wq_init(void)
{
        unsigned long j = jiffies;


        printk("jiffies: %lu\n", jiffies);
        atomic_set(&wq_run_times, 0);


        init_timer(&test_data.timer);
        test_data.loops = 0;
        test_data.prev_jiffies = j;
        test_data.timer.function = test_timer_fn;
        test_data.timer.data = (unsigned long)(&test_data);
        test_data.timer.expires = j + 2 * HZ;
        add_timer(&test_data.timer);


        init_timer(&test_data1.timer);
        test_data1.loops = 0;
        test_data1.prev_jiffies = j;
        test_data1.timer.function = test_timer_fn1;
        test_data1.timer.data = (unsigned long)(&test_data1);
        test_data1.timer.expires = j + 3 * HZ;
        add_timer(&test_data1.timer);


        test_workqueue = create_singlethread_workqueue("test-wq");
        printk("test-wq init success\n");
        printk("jiffies: %lu\n", jiffies);
        return 0;
}


void wq_exit(void)
{
        del_timer(&test_data.timer);
        del_timer(&test_data1.timer);
        destroy_workqueue(test_workqueue);
        printk("wq exit success\n");
}


module_init(wq_init);
module_exit(wq_exit)

注意:不能在同一时间内向一个工作队列添加相同的任务
阅读(6952) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

tgvlcw2010-10-11 18:17:51

按我自己的想法,比如定时器每2秒就执行一次工作对列,而如果工作对列的运行时间超过2秒,根据工作对列的定义,一个工作对列中只能有一个任务,具体会出现什么情况,我没有调过,你自己调调看吧

zanget2010-09-25 09:44:36

请问 如果do_work的执行时间比较长,应该如何修改第二种方案呢???