Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092246
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2009-05-18 13:50:19

#include
#include
#include
#include
#include
#include
#include
#define err(msg) printk(KERN_ALERT "%s\n", msg)
#define DATAALIGN (sizeof(unsigned long) - 1)
static void sendwork(struct work_struct *arg);
static void recvwork(struct work_struct *arg);
struct buff_struct_head {
 struct buff_struct *prev;
 struct buff_struct *next;
 spinlock_t lock;
};
struct buff_struct {
 struct buff_struct *prev;
 struct buff_struct *next;
 
 char data[0]; 
};
static struct buff_struct_head head = {
 .prev = (struct buff_struct *)&head,
 .next = (struct buff_struct *)&head,
 .lock = SPIN_LOCK_UNLOCKED,
};
static struct workqueue_struct *sender;
static struct workqueue_struct *recver;
static DECLARE_WORK(sender_work, sendwork);
static DECLARE_WORK(recver_work, recvwork);
static void buff_queue_tail(struct buff_struct_head *list, struct buff_struct *newsk)
{
 struct buff_struct *prev, *next;
 unsigned long flags;
 spin_lock_irqsave(&list->lock, flags);
 next = (struct buff_struct *)list;
 prev = next->prev;
 newsk->next = next;
 newsk->prev = prev;
 next->prev  = prev->next = newsk;
 spin_unlock_irqrestore(&list->lock, flags);
}
static struct buff_struct * buff_dequeue(struct buff_struct_head *list)
{
 struct buff_struct *next, *prev, *result;
 unsigned long flags;
 spin_lock_irqsave(&list->lock, flags);
 prev = (struct buff_struct *) list;
 next = prev->next;
 result = NULL;
 if (next != prev) {
  result      = next;
  next      = next->next;
  next->prev   = prev;
  prev->next   = next;
  result->next = result->prev = NULL;
 }
 spin_unlock_irqrestore(&list->lock, flags);
 return result;
}
static void sendwork(struct work_struct *arg)
{
 struct buff_struct *t;
 int i;
 char data[] = "abcdefg";
 int len = (sizeof(struct buff_struct) + strlen(data) + DATAALIGN) & ~DATAALIGN;
 
 for (i = 0; i < 10; i++) {
  t = kmalloc(len, GFP_KERNEL);
  if (!t) {
   err("kmalloc");
   return;
  }
  memset(t, '\0', len);
  memcpy(t->data, data, strlen(data));
 
  buff_queue_tail(&head, t); 
 }
}
static void recvwork(struct work_struct *arg)
{
 struct buff_struct *t;
 int count = 0;
 while (1) {
  t = buff_dequeue(&head);
  if (t) {
   printk(KERN_ALERT "%s\n", t->data);
   kfree(t);
   if (++count == 10)
    break;
  }
  
  msleep(10);
 }
}
static int workqueue_init(void)
{
 sender = create_workqueue("sender");
 recver = create_workqueue("recver");
 queue_work(sender, &sender_work);
 queue_work(recver, &recver_work);
 return 0;
}
static void workqueue_exit(void)

 flush_workqueue(sender);
 flush_workqueue(recver);
 printk(KERN_ALERT "workqueue_exit\n");
}
module_init(workqueue_init);
module_exit(workqueue_exit);
MODULE_LICENSE("GPL");
阅读(778) | 评论(0) | 转发(0) |
0

上一篇:2.6内核Makefile

下一篇:改进的工作队列

给主人留下些什么吧!~~