int epoll_recalc(void *arg, int max) { struct epollop *epollop = arg; if (max > epollop->nfds) { struct evepoll *fds; int nfds; nfds = epollop->nfds; while (nfds < max) nfds <<= 1; fds = realloc(epollop->fds, nfds * sizeof(struct evepoll)); if (fds == NULL) { log_error("realloc"); return (-1); } epollop->fds = fds; memset(fds + epollop->nfds, 0, (nfds - epollop->nfds) * sizeof(struct evepoll)); epollop->nfds = nfds; } return (evsignal_recalc(&epollop->evsigmask)); }
int devpoll_recalc(struct event_base *base, void *arg, int max) { struct devpollop *devpollop = arg; if (max > devpollop->nfds) { struct evdevpoll *fds; int nfds; nfds = devpollop->nfds; while (nfds < max) nfds <<= 1; fds = realloc(devpollop->fds, nfds * sizeof(struct evdevpoll)); if (fds == NULL) { event_warn("realloc"); return (-1); } devpollop->fds = fds; memset(fds + devpollop->nfds, 0, (nfds - devpollop->nfds) * sizeof(struct evdevpoll)); devpollop->nfds = nfds; } return (evsignal_recalc(&devpollop->evsigmask)); }
int epoll_dispatch(void *arg, struct timeval *tv) { struct epollop *epollop = arg; struct epoll_event *events = epollop->events; struct evepoll *evep; struct event *ev; int i, res, timeout; if (evsignal_deliver(&epollop->evsigmask) == -1) return (-1); timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000; res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout); if (evsignal_recalc(&epollop->evsigmask) == -1) return (-1); if (res == -1) { if (errno != EINTR) { log_error("epoll_wait"); return (-1); } evsignal_process(); return (0); } else if (evsignal_caught) evsignal_process(); LOG_DBG((LOG_MISC, 80, "%s: epoll_wait reports %d", __func__, res)); for (i = 0; i < res; i++) { int which = 0, what; evep = (struct evepoll *)events[i].data.ptr; what = events[i].events; if (what & EPOLLIN) { which |= EV_READ; } if (what & EPOLLOUT) { which |= EV_WRITE; } if (!which) continue; if (which & EV_READ) { ev = evep->evread; if (!(ev->ev_events & EV_PERSIST)) event_del(ev); event_active(ev, EV_READ, 1); } if (which & EV_WRITE) { ev = evep->evwrite; if (!(ev->ev_events & EV_PERSIST)) event_del(ev); event_active(ev, EV_WRITE, 1); } } return (0); }
int epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv) { struct epollop *epollop = arg; struct epoll_event *events = epollop->events; struct evepoll *evep; int i, res, timeout; if (evsignal_deliver(&epollop->evsigmask) == -1) return (-1); timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; benchmark_stop_sample(); event_mutex_unlock(base); res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout); event_mutex_lock(base); benchmark_start_sample(); if (evsignal_recalc(&epollop->evsigmask) == -1) return (-1); if (res == -1) { if (errno != EINTR) { event_warn("epoll_wait"); return (-1); } evsignal_process(); return (0); } else if (evsignal_caught) evsignal_process(); event_debug(("%s: epoll_wait reports %d", __func__, res)); for (i = 0; i < res; i++) { int which = 0; int what = events[i].events; struct event *evread = NULL, *evwrite = NULL; evep = (struct evepoll *)events[i].data.ptr; if (what & EPOLLHUP) what |= EPOLLIN | EPOLLOUT; else if (what & EPOLLERR) what |= EPOLLIN | EPOLLOUT; if (what & EPOLLIN) { evread = evep->evread; which |= EV_READ; } if (what & EPOLLOUT) { evwrite = evep->evwrite; which |= EV_WRITE; } if (!which) continue; if (evread != NULL && !(evread->ev_events & EV_PERSIST)) event_del(evread); if (evwrite != NULL && evwrite != evread && !(evwrite->ev_events & EV_PERSIST)) event_del(evwrite); if (evread != NULL) event_active(evread, EV_READ, 1); if (evwrite != NULL) event_active(evwrite, EV_WRITE, 1); } return (0); }
int devpoll_dispatch(struct event_base *base, void *arg, struct timeval *tv) { struct devpollop *devpollop = arg; struct pollfd *events = devpollop->events; struct dvpoll dvp; struct evdevpoll *evdp; int i, res, timeout; if (evsignal_deliver(&devpollop->evsigmask) == -1) return (-1); if (devpollop->nchanges) devpoll_commit(devpollop); timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; dvp.dp_fds = devpollop->events; dvp.dp_nfds = devpollop->nevents; dvp.dp_timeout = timeout; res = ioctl(devpollop->dpfd, DP_POLL, &dvp); if (evsignal_recalc(&devpollop->evsigmask) == -1) return (-1); if (res == -1) { if (errno != EINTR) { event_warn("ioctl: DP_POLL"); return (-1); } evsignal_process(); return (0); } else if (evsignal_caught) evsignal_process(); event_debug(("%s: devpoll_wait reports %d", __func__, res)); for (i = 0; i < res; i++) { int which = 0; int what = events[i].revents; struct event *evread = NULL, *evwrite = NULL; assert(events[i].fd < devpollop->nfds); evdp = &devpollop->fds[events[i].fd]; if (what & POLLHUP) what |= POLLIN | POLLOUT; else if (what & POLLERR) what |= POLLIN | POLLOUT; if (what & POLLIN) { evread = evdp->evread; which |= EV_READ; } if (what & POLLOUT) { evwrite = evdp->evwrite; which |= EV_WRITE; } if (!which) continue; if (evread != NULL && !(evread->ev_events & EV_PERSIST)) event_del(evread); if (evwrite != NULL && evwrite != evread && !(evwrite->ev_events & EV_PERSIST)) event_del(evwrite); if (evread != NULL) event_active(evread, EV_READ, 1); if (evwrite != NULL) event_active(evwrite, EV_WRITE, 1); } return (0); }