Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4842404
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-01-19 13:19:06

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/list.h>
#include <linux/spinlock.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("wzt");

#define SLEEP_TIME 2

static struct timer_list my_timer;

typedef struct list_test_struct {
        int count;
        struct list_head list;
}node;

struct list_head head_list;
rwlock_t list_lock = RW_LOCK_UNLOCKED;
unsigned int g_count = 0;

void timer_test(unsigned long data)
{
        node *tmp = (node *)data;

        printk("%5d", tmp->count);

        del_timer_sync(&my_timer);
        timer_init(SLEEP_TIME);
}

int timer_init(unsigned int seconds)
{
        node *s = NULL;

        init_timer(&my_timer);

        s = (node *)kmalloc(sizeof(node), GFP_ATOMIC);
        if (!s)
                return 0;

        write_lock(&list_lock);
        s->count = g_count++;
        list_add_tail(&(s->list), &head_list);
        write_lock(&list_lock);

        my_timer.data = (unsigned long)s;
        my_timer.function = timer_test;
        my_timer.expires = jiffies + seconds * 1000;

        add_timer(&my_timer);
        return 0;
}

int test_timer_init(void)
{
        INIT_LIST_HEAD(&head_list);

        timer_init(SLEEP_TIME);

        return 0;
}

void test_timer_exit(void)
{
        del_timer_sync(&my_timer);

        printk("timer test exit.\n");
}

module_init(test_timer_init);
module_exit(test_timer_exit);


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