Linux内核等待队列的使用方法总结

    如果应用程序准备设计成阻塞方式来存取设备的话,在内核底层驱动设计时需要考虑增加wait_queue_t来实现多进程的存取访问。

 

 struct __wait_queue_head {
   spinlock_t lock;
   struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;//等待队列头数据结构定义

 

struct __wait_queue {
 unsigned int flags;
#define WQ_FLAG_EXCLUSIVE 0x01
 void *private;//用来存放task_struct结构的指针
 wait_queue_func_t func;
 struct list_head task_list;
};//等待队列数据结构定义

 

    以Linux内核nand flash驱动为例说明其用法,

/**
 * struct nand_hw_control - Control structure for hardware controller (e.g ECC generator) shared among independent devices
 * @lock:               protection lock
 * @active:  the mtd device which holds the controller currently
 * @wq:   wait queue to sleep on if a NAND operation is in progress
 *                      used instead of the per chip wait queue when a hw controller is available
 */
struct nand_hw_control {
 spinlock_t  lock;
 struct nand_chip *active;
 wait_queue_head_t wq;//nand flash驱动程序等待队列,整个nand flash芯片只有这个队列,而不是按照分区有多个等待队列头。
};

下面这个函数是获取nand flash操作权函数,里面使用了等待队列进行等待nand flash可以操作。

static int
nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
{
 spinlock_t *lock = &chip->controller->lock;
 wait_queue_head_t *wq = &chip->controller->wq;//获取nand flash等待队列头
 DECLARE_WAITQUEUE(wait, current);//声明定义等待队列wait,并把wait->private=current。
 retry:
 spin_lock(lock);

 /* Hardware controller shared among independend devices */
 /* Hardware controller shared among independend devices */
 if (!chip->controller->active)
  chip->controller->active = chip;

 if (chip->controller->active == chip && chip->state == FL_READY) {
  chip->state = new_state;
  spin_unlock(lock);
  return 0;
 }
 if (new_state == FL_PM_SUSPENDED) {
  spin_unlock(lock);
  return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
 }
 set_current_state(TASK_UNINTERRUPTIBLE);
 add_wait_queue(wq, &wait);//当前nand flash不可用,添加wait到wq等待队列头。
 spin_unlock(lock);
 schedule();//释放处理器
 remove_wait_queue(wq, &wait);//移除等待队列,进行重试。
 goto retry;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值