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.19 by root, Sun Dec 9 03:42:43 2007 UTC

1/* 1/*
2 * libev epoll fd activity backend 2 * libev poll fd activity backend
3 * 3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
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 33
34static int epoll_fd = -1; 34void inline_size
35 35pollidx_init (int *base, int count)
36static void
37epoll_modify (int fd, int oev, int nev)
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 (int, 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 pollidxs [fd] = idx = pollcnt++;
87 eventmax = array_roundsize (events, eventmax << 1); 56 array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2);
88 events = malloc (sizeof (struct epoll_event) * eventmax); 57 polls [idx].fd = fd;
58 }
59
60 assert (polls [idx].fd == fd);
61
62 if (nev)
63 polls [idx].events =
64 (nev & EV_READ ? POLLIN : 0)
65 | (nev & EV_WRITE ? POLLOUT : 0);
66 else /* remove pollfd */
67 {
68 pollidxs [fd] = -1;
69
70 if (expect_true (idx < --pollcnt))
71 {
72 polls [idx] = polls [pollcnt];
73 pollidxs [polls [idx].fd] = idx;
74 }
89 } 75 }
90} 76}
91 77
92static void 78static void
93epoll_init (int flags) 79poll_poll (EV_P_ ev_tstamp timeout)
94{ 80{
95 epoll_fd = epoll_create (256); 81 int i;
82 int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.));
96 83
97 if (epoll_fd < 0) 84 if (expect_false (res < 0))
98 return; 85 {
86 if (errno == EBADF)
87 fd_ebadf (EV_A);
88 else if (errno == ENOMEM && !syserr_cb)
89 fd_enomem (EV_A);
90 else if (errno != EINTR)
91 syserr ("(libev) poll");
99 92
100 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 93 return;
94 }
101 95
102 ev_method = EVMETHOD_EPOLL; 96 for (i = 0; i < pollcnt; ++i)
103 method_fudge = 1e-3; /* needed to compensate for epoll returning early */ 97 if (expect_false (polls [i].revents & POLLNVAL))
104 method_modify = epoll_modify; 98 fd_kill (EV_A_ polls [i].fd);
105 method_poll = epoll_poll; 99 else
106 100 fd_event (
107 eventmax = 64; /* intiial number of events receivable per poll */ 101 EV_A_
108 events = malloc (sizeof (struct epoll_event) * eventmax); 102 polls [i].fd,
103 (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
104 | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
105 );
109} 106}
110 107
108int inline_size
109poll_init (EV_P_ int flags)
110{
111 backend_fudge = 0; /* needed to compensate for select returning early, very conservative */
112 backend_modify = poll_modify;
113 backend_poll = poll_poll;
114
115 pollidxs = 0; pollidxmax = 0;
116 polls = 0; pollmax = 0; pollcnt = 0;
117
118 return EVBACKEND_POLL;
119}
120
121void inline_size
122poll_destroy (EV_P)
123{
124 ev_free (pollidxs);
125 ev_free (polls);
126}
127

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines