Linux驱动之等待队列和poll使用
2015-02-06 15:17
337 查看
参考:http://blog.csdn.net/luoqindong/article/details/17840095
等待队列一般用于数据的同步,以及异步事件通知等。在中断处理,定时器,进程同步的场合用于处理阻塞进程的唤醒。
在wait.c中可以看到wait_queue_t和wait_queue_head_t这两个数据结构:
wait_queue_t和wait_queue_head_t之间的关系:wait_queue_t是wait_queue_head_t所代表队列中的一个元素。
一般来说使用wait_queue_head即可,不过在有些场合需要使用wait_queue。
等待队列使用:
声明:
睡眠操作:
其他如wait_event_timeout,wait_event_interruptible,wait_event_interruptible_timeout,wait_event_killable
一般来说,与其使用wait_event,不如使用wait_event_interruptible。不过当信号中断时,需要返回-ERESTARTSYS。
相对睡眠,另外一半就是唤醒操作,唤醒操作需要其他执行线程(可能是另外一个进程或者中断服务函数)。
其他如: wake_up_interruptible则只唤醒等待队列上所有可中断的休眠的进程。
整个流程可以简单地总结为:
休眠:
wait_event(queue,condition==1);
condition=0;
唤醒:
condition=1;
wake_up(queue);
若是线程A、B同时休眠等待condition。线程C唤醒线程A、B。在condition=0之前的这个节点,线程A和线程B都有可能看到condition=1的条件满足。
另外还有wait_queue_t,若需要使用wait_queue_t,一般的使用过程:
1、声明wait_queue_t和wait_queue_head_t,其中wait_queue_t的声明:
2、调用add_wait_queue
set_current_state(TASK_INTERRUPTIBLE);
4、调用schedule()
当等待的条件满足时:
1、设置当前进程状态为RUNNING
__set_current_state(TASK_RUNNING);
2、remove_wait_queue
上述技术在设备的阻塞和非阻塞操作中十分常见,对于应用层来说,open时默认使用阻塞方式打开,O_NONBLOCK或者O_NDELAY(system V)可以改变为非阻塞方式。
对于像FIFO或者被锁住的磁盘文件,打开它可能需要很长的时间进行初始化,非阻塞方式可以直接返回-EAGAIN。
对于读写操作,阻塞操作的流程比较常见,我们需要注意非阻塞操作的流程。非阻塞读写需要支持select和poll这两个系统调用,这两个系统调用最终会调用到内核的poll。因此,要实现设备的非阻塞操作,就必须实现内核的poll函数。
典型的poll函数的实现如下:
1、将等待队列加入到poll_table中,这样一旦,等待队列状态发生变换,上层能够知道,从而能够马上重新再次调用poll函数
2、返回设备状态标志。
对于poll_wait,定义如下:
等待队列一般用于数据的同步,以及异步事件通知等。在中断处理,定时器,进程同步的场合用于处理阻塞进程的唤醒。
在wait.c中可以看到wait_queue_t和wait_queue_head_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; wait_queue_func_t func; struct list_head task_list; };
wait_queue_t和wait_queue_head_t之间的关系:wait_queue_t是wait_queue_head_t所代表队列中的一个元素。
一般来说使用wait_queue_head即可,不过在有些场合需要使用wait_queue。
等待队列使用:
声明:
DECLARE_WAIT_QUEUE_HEAD(name); wait_queue_head_t my_queue; init_waitqueue_head(&my_queue);
睡眠操作:
/** * wait_event - sleep until a condition gets true * @wq: the waitqueue to wait on * @condition: a C expression for the event to wait for * * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the * @condition evaluates to true. The @condition is checked each time * the waitqueue @wq is woken up. * * wake_up() has to be called after changing any variable that could * change the result of the wait condition. */ #define wait_event(wq, condition) \ do { \ if (condition) \ break; \ __wait_event(wq, condition); \ } while (0)
其他如wait_event_timeout,wait_event_interruptible,wait_event_interruptible_timeout,wait_event_killable
一般来说,与其使用wait_event,不如使用wait_event_interruptible。不过当信号中断时,需要返回-ERESTARTSYS。
相对睡眠,另外一半就是唤醒操作,唤醒操作需要其他执行线程(可能是另外一个进程或者中断服务函数)。
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
/** * __wake_up - wake up threads blocked on a waitqueue. * @q: the waitqueue * @mode: which threads * @nr_exclusive: how many wake-one or wake-many threads to wake up * @key: is directly passed to the wakeup function * * It may be assumed that this function implies a write memory barrier before * changing the task state if and only if any tasks are woken up. */ void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr_exclusive, void *key) { unsigned long flags; spin_lock_irqsave(&q->lock, flags); __wake_up_common(q, mode, nr_exclusive, 0, key); spin_unlock_irqrestore(&q->lock, flags); }
其他如: wake_up_interruptible则只唤醒等待队列上所有可中断的休眠的进程。
整个流程可以简单地总结为:
休眠:
wait_event(queue,condition==1);
condition=0;
唤醒:
condition=1;
wake_up(queue);
若是线程A、B同时休眠等待condition。线程C唤醒线程A、B。在condition=0之前的这个节点,线程A和线程B都有可能看到condition=1的条件满足。
另外还有wait_queue_t,若需要使用wait_queue_t,一般的使用过程:
1、声明wait_queue_t和wait_queue_head_t,其中wait_queue_t的声明:
#define DECLARE_WAITQUEUE(name, tsk) \ wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
#define __WAITQUEUE_INITIALIZER(name, tsk) { \ .private = tsk, \ .func = default_wake_function, \ .task_list = { NULL, NULL } }
2、调用add_wait_queue
void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait) { unsigned long flags; wait->flags &= ~WQ_FLAG_EXCLUSIVE; spin_lock_irqsave(&q->lock, flags); __add_wait_queue(q, wait); spin_unlock_irqrestore(&q->lock, flags); }
static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) { list_add(&new->task_list, &head->task_list); }3、将当前的进程设置为TASK_INTERRUPTIBLE或者TASK_UNINTERRUPTIBLE
set_current_state(TASK_INTERRUPTIBLE);
4、调用schedule()
当等待的条件满足时:
1、设置当前进程状态为RUNNING
__set_current_state(TASK_RUNNING);
2、remove_wait_queue
void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait) { unsigned long flags; spin_lock_irqsave(&q->lock, flags); __remove_wait_queue(q, wait); spin_unlock_irqrestore(&q->lock, flags); } static inline void __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old) { list_del(&old->task_list); }
上述技术在设备的阻塞和非阻塞操作中十分常见,对于应用层来说,open时默认使用阻塞方式打开,O_NONBLOCK或者O_NDELAY(system V)可以改变为非阻塞方式。
对于像FIFO或者被锁住的磁盘文件,打开它可能需要很长的时间进行初始化,非阻塞方式可以直接返回-EAGAIN。
对于读写操作,阻塞操作的流程比较常见,我们需要注意非阻塞操作的流程。非阻塞读写需要支持select和poll这两个系统调用,这两个系统调用最终会调用到内核的poll。因此,要实现设备的非阻塞操作,就必须实现内核的poll函数。
典型的poll函数的实现如下:
static unsigned int ic_id_poll(struct file *filep, struct poll_table_struct *wait) { unsigned int mask = 0; //add poll_wait to info application to poll again if wait_queue_head status has any change. poll_wait(filp, &devp->wait_r, wait); poll_wait(filp, &devp->wait_w, wait); if(devp->bufferlen != 0) { mask |= POLLIN | POLLRDNORM;//readable } if(devp->bufferlen != BUFFSIZE) { mask |= POLLOUT | POLLWRNORM; //writeable } return mask; }poll函数需要实现两个过程:
1、将等待队列加入到poll_table中,这样一旦,等待队列状态发生变换,上层能够知道,从而能够马上重新再次调用poll函数
2、返回设备状态标志。
对于poll_wait,定义如下:
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p) { if (p && wait_address) p->qproc(filp, wait_address, p); }最终p->qproc的调用
/* Add a new entry */ static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p) { struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt); struct poll_table_entry *entry = poll_get_entry(pwq); if (!entry) return; get_file(filp); entry->filp = filp; entry->wait_address = wait_address; entry->key = p->key; init_waitqueue_func_entry(&entry->wait, pollwake); entry->wait.private = pwq; add_wait_queue(wait_address, &entry->wait); }
相关文章推荐
- linux设备驱动--内核等待队列知识点---结合中断使用
- Linux驱动阻塞与非阻塞IO之等待队列
- 4-3 等待队列与poll驱动实例讲解(3个实例要好好看看)
- linux之等待队列--阻塞型驱动学习---学习笔记
- linux poll 和 等待队列休眠的关系(基于kernel 3.10.0)
- linux等待队列使用步骤
- linux高级字符设备驱动之 二 内核等待队列
- linux驱动编程--等待队列浅析
- Linux使用wake_up_interruptible()唤醒注册到等待队列上的进程
- Linux_设备驱动阻塞/非阻塞IO_等待队列
- linux驱动之等待队列
- linux等待队列使用步骤
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- linux 等待队列 与linux 等待队列头关系-不懂linux驱动阻塞操作可以看看
- 驱动学习之——内核等待队列的使用
- Linux 设备驱动--- 内核等待队列 --- wait_queue_head --- wait_event_interruptible --- 按键驱动程序优化
- linux等待队列使用步骤
- 基于等待队列及poll机制的按键驱动代码分析和测试代码
- linux 内核下等待队列的使用
- linux 等待队列 与linux 等待队列头关系-不懂linux驱动阻塞操作可以看看