如果应用程序准备设计成阻塞方式来存取设备的话,在内核底层驱动设计时需要考虑增加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;
}