ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_poll.c
(Generate patch)

Comparing libev/ev_poll.c (file contents):
Revision 1.1 by root, Fri Nov 2 15:56:19 2007 UTC vs.
Revision 1.6 by root, Sun Nov 4 23:14:11 2007 UTC

27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/epoll.h> 32#include <poll.h>
33
34static int epoll_fd = -1;
35 33
36static void 34static void
37epoll_modify (int fd, int oev, int nev) 35pollidx_init (int *base, int count)
38{ 36{
39 int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; 37 while (count--)
40 38 *base++ = -1;
41 struct epoll_event ev;
42 ev.data.fd = fd;
43 ev.events =
44 (nev & EV_READ ? EPOLLIN : 0)
45 | (nev & EV_WRITE ? EPOLLOUT : 0);
46
47 epoll_ctl (epoll_fd, mode, fd, &ev);
48} 39}
49 40
50static void 41static void
51epoll_postfork_child (void) 42poll_modify (EV_P_ int fd, int oev, int nev)
52{ 43{
53 int fd; 44 int idx;
54 45
55 epoll_fd = epoll_create (256); 46 if (oev == nev)
56 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC);
57
58 /* re-register interest in fds */
59 for (fd = 0; fd < anfdmax; ++fd)
60 if (anfds [fd].events)//D
61 epoll_modify (fd, EV_NONE, anfds [fd].events);
62}
63
64static struct epoll_event *events;
65static int eventmax;
66
67static void
68epoll_poll (ev_tstamp timeout)
69{
70 int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.));
71 int i;
72
73 if (eventcnt < 0)
74 return; 47 return;
75 48
76 for (i = 0; i < eventcnt; ++i) 49 array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init);
77 fd_event (
78 events [i].data.fd,
79 (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
80 | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0)
81 );
82 50
83 /* if the receive array was full, increase its size */ 51 idx = pollidxs [fd];
84 if (expect_false (eventcnt == eventmax)) 52
53 if (idx < 0) /* need to allocate a new pollfd */
85 { 54 {
86 free (events); 55 idx = pollcnt++;
87 eventmax = array_roundsize (events, eventmax << 1); 56 array_needsize (polls, pollmax, pollcnt, );
88 events = malloc (sizeof (struct epoll_event) * eventmax); 57 polls [idx].fd = fd;
58 }
59
60 if (nev)
61 polls [idx].events =
62 (nev & EV_READ ? POLLIN : 0)
63 | (nev & EV_WRITE ? POLLOUT : 0);
64 else /* remove pollfd */
65 {
66 if (idx < pollcnt--)
67 {
68 pollidxs [fd] = -1;
69 polls [idx] = polls [pollcnt];
70 pollidxs [polls [idx].fd] = idx;
71 }
89 } 72 }
90} 73}
91 74
92static void 75static void
93epoll_init (int flags) 76poll_poll (EV_P_ ev_tstamp timeout)
94{ 77{
95 epoll_fd = epoll_create (256); 78 int res = poll (polls, pollcnt, ceil (timeout * 1000.));
96 79
97 if (epoll_fd < 0) 80 if (res > 0)
98 return; 81 {
82 int i;
99 83
100 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 84 for (i = 0; i < pollcnt; ++i)
101 85 fd_event (
102 ev_method = EVMETHOD_EPOLL; 86 EV_A_
103 method_fudge = 1e-3; /* needed to compensate for epoll returning early */ 87 polls [i].fd,
104 method_modify = epoll_modify; 88 (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
105 method_poll = epoll_poll; 89 | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
106 90 );
107 eventmax = 64; /* intiial number of events receivable per poll */ 91 }
108 events = malloc (sizeof (struct epoll_event) * eventmax); 92 else if (res < 0)
93 {
94 if (errno == EBADF)
95 fd_ebadf (EV_A);
96 else if (errno == ENOMEM)
97 fd_enomem (EV_A);
98 }
109} 99}
110 100
101static int
102poll_init (EV_P_ int flags)
103{
104 method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */
105 method_modify = poll_modify;
106 method_poll = poll_poll;
107
108 pollidxs = 0; pollidxmax = 0;
109 polls = 0; pollmax = 0; pollcnt = 0;
110
111 return EVMETHOD_POLL;
112}
113
114static void
115poll_destroy (EV_P)
116{
117 free (pollidxs);
118 free (polls);
119}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines