poll_wait

相关结构:

http://lxr.free-electrons.com/source/include/linux/poll.h?v=3.8

 33 /*
 34  * Do not touch the structure directly, use the access functions
 35  * poll_does_not_wait() and poll_requested_events() instead.
 36  */
 37 typedef struct poll_table_struct {
 38         poll_queue_proc _qproc;
 39         unsigned long _key;
 40 } poll_table;
 41 
 42 static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
 43 {
 44         if (p && p->_qproc && wait_address)
 45                 p->_qproc(filp, wait_address, p);
 46 }
 47 
 48 /*
 49  * Return true if it is guaranteed that poll will not wait. This is the case
 50  * if the poll() of another file descriptor in the set got an event, so there
 51  * is no need for waiting.
 52  */
 53 static inline bool poll_does_not_wait(const poll_table *p)
 54 {
 55         return p == NULL || p->_qproc == NULL;
 56 }
 57 
 58 /*
 59  * Return the set of events that the application wants to poll for.
 60  * This is useful for drivers that need to know whether a DMA transfer has
 61  * to be started implicitly on poll(). You typically only want to do that
 62  * if the application is actually polling for POLLIN and/or POLLOUT.
 63  */
 64 static inline unsigned long poll_requested_events(const poll_table *p)
 65 {
 66         return p ? p->_key : ~0UL;
 67 }
 68 
 69 static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
 70 {
 71         pt->_qproc = qproc;
 72         pt->_key   = ~0UL; /* all events enabled */
 73 }
 74 
 75 struct poll_table_entry {
 76         struct file *filp;
 77         unsigned long key;
 78         wait_queue_t wait;
 79         wait_queue_head_t *wait_address;
 80 };
 81 
 82 /*
 83  * Structures and helpers for select/poll syscall
 84  */
 85 struct poll_wqueues {
 86         poll_table pt;
 87         struct poll_table_page *table;
 88         struct task_struct *polling_task;
 89         int triggered;
 90         int error;
 91         int inline_index;
 92         struct poll_table_entry inline_entries[N_INLINE_POLL_ENTRIES];
 93 };
 94 

poll_wait分析:

在用户空间调用select后转入内核会调用do_select。

do_select中初始化poll接口所需的poll_table结构,调用poll。

http://lxr.free-electrons.com/source/fs/select.c?v=3.8

395 int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
396 {
397         ktime_t expire, *to = NULL;
398         struct poll_wqueues table;
399         poll_table *wait;
400         int retval, i, timed_out = 0;
401         unsigned long slack = 0;
402 
403         rcu_read_lock();
404         retval = max_select_fd(n, fds);
405         rcu_read_unlock();
406 
407         if (retval < 0)
408                 return retval;
409         n = retval;
410 
411         poll_initwait(&table); //初始化poll_wqueues结构
412         wait = &table.pt;

 

116 void poll_initwait(struct poll_wqueues *pwq)
117 {
118         init_poll_funcptr(&pwq->pt, __pollwait);
119         pwq->polling_task = current;
120         pwq->triggered = 0;
121         pwq->error = 0;
122         pwq->table = NULL;
123         pwq->inline_index = 0;
124 }

真正的poll_wait调用其实比较简单,其实就是调了__pollwait,虽然叫wait,可是该函数并不阻塞,仅仅是给wait队列增加了个节点。

215 /* Add a new entry */
216 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
217                                 poll_table *p)
218 {
219         struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
220         struct poll_table_entry *entry = poll_get_entry(pwq);
221         if (!entry)
222                 return;
223         entry->filp = get_file(filp);
224         entry->wait_address = wait_address;
225         entry->key = p->_key;
226         init_waitqueue_func_entry(&entry->wait, pollwake);
227         entry->wait.private = pwq;
228         add_wait_queue(wait_address, &entry->wait); //list_add(&entry->wait.task_list, &wait_address->task_list);
229 }

 

posted @ 2014-11-21 14:35  mrzcb  阅读(2409)  评论(0编辑  收藏  举报