libevent源码分析--epoll_dispatch()

Datetime:2016-08-23 03:47:14          Topic: Source Code Analysis           Share
static int
192 epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
193 {
194     struct epollop *epollop = arg;
195     struct epoll_event *events = epollop->events;
196     struct evepoll *evep;
197     int i, res, timeout = -1;
198 
199     if (tv != NULL)
200         timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
201 
202     if (timeout > MAX_EPOLL_TIMEOUT_MSEC) {
203         /* Linux kernels can wait forever if the timeout is too big;
204          * see comment on MAX_EPOLL_TIMEOUT_MSEC. */
205         timeout = MAX_EPOLL_TIMEOUT_MSEC;
206     }
207 
208     res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
209 
210     if (res == -1) {
211         if (errno != EINTR) {
212             event_warn("epoll_wait");
213             return (-1);
214         }
215 
216         evsignal_process(base);
217         return (0);
218     } else if (base->sig.evsignal_caught) {
219         evsignal_process(base);
220     }
221 
222     event_debug(("%s: epoll_wait reports %d", __func__, res));
223 
224     for (i = 0; i < res; i++) {
225         int what = events[i].events;
226         struct event *evread = NULL, *evwrite = NULL;
227         int fd = events[i].data.fd;
228 
229         if (fd < 0 || fd >= epollop->nfds)
230             continue;
231         evep = &epollop->fds[fd];
232 
233         if (what & (EPOLLHUP|EPOLLERR)) {
234             evread = evep->evread;
235             evwrite = evep->evwrite;
236         } else {
 237      if (what & EPOLLIN) {
238                 evread = evep->evread;
239             }
240 
241             if (what & EPOLLOUT) {
242                 evwrite = evep->evwrite;
243             }
244         }
245 
246         if (!(evread||evwrite))
247             continue;
248 
249         if (evread != NULL)
250             event_active(evread, EV_READ, 1);
251         if (evwrite != NULL)
252             event_active(evwrite, EV_WRITE, 1);
253     }
254 
255     return (0);
256 }

 




About List