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.2 by root, Fri Nov 2 16:54:35 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 33
34static int epoll_fd = -1; 34static struct pollfd *polls;
35static int pollmax, pollcnt;
36static int *pollidxs; /* maps fds into structure indices */
37static int pollidxmax;
35 38
36static void 39static void
37epoll_modify (int fd, int oev, int nev) 40pollidx_init (int *base, int count)
38{ 41{
39 int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; 42 while (count--)
40 43 *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} 44}
49 45
50static void 46static void
51epoll_postfork_child (void) 47poll_modify (int fd, int oev, int nev)
52{ 48{
53 int fd; 49 int idx;
50 array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init);
54 51
55 epoll_fd = epoll_create (256); 52 idx = pollidxs [fd];
56 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC);
57 53
58 /* re-register interest in fds */ 54 if (idx < 0) /* need to allocate a new pollfd */
59 for (fd = 0; fd < anfdmax; ++fd) 55 {
60 if (anfds [fd].events)//D 56 idx = pollcnt++;
61 epoll_modify (fd, EV_NONE, anfds [fd].events); 57 array_needsize (polls, pollmax, pollcnt, );
62} 58 polls [idx].fd = fd;
59 }
63 60
64static struct epoll_event *events; 61 if (nev)
65static int eventmax; 62 polls [idx].events =
66 63 (nev & EV_READ ? POLLIN : 0)
67static void 64 | (nev & EV_WRITE ? POLLOUT : 0);
68epoll_poll (ev_tstamp timeout) 65 else /* remove pollfd */
69{
70 int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.));
71 int i;
72
73 if (eventcnt < 0)
74 return;
75
76 for (i = 0; i < eventcnt; ++i)
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
83 /* if the receive array was full, increase its size */
84 if (expect_false (eventcnt == eventmax))
85 { 66 {
86 free (events); 67 if (idx < pollcnt--)
87 eventmax = array_roundsize (events, eventmax << 1); 68 {
88 events = malloc (sizeof (struct epoll_event) * eventmax); 69 pollidxs [fd] = -1;
70 polls [idx] = polls [pollcnt];
71 pollidxs [polls [idx].fd] = idx;
72 }
89 } 73 }
90} 74}
91 75
92static void 76static void
93epoll_init (int flags) 77poll_poll (ev_tstamp timeout)
94{ 78{
95 epoll_fd = epoll_create (256); 79 int res = poll (polls, pollcnt, ceil (timeout * 1000.));
96 80
97 if (epoll_fd < 0) 81 if (res > 0)
98 return; 82 {
83 int i;
99 84
100 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 85 for (i = 0; i < pollcnt; ++i)
101 86 fd_event (
102 ev_method = EVMETHOD_EPOLL; 87 polls [i].fd,
103 method_fudge = 1e-3; /* needed to compensate for epoll returning early */ 88 (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
104 method_modify = epoll_modify; 89 | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
105 method_poll = epoll_poll; 90 );
106 91 }
107 eventmax = 64; /* intiial number of events receivable per poll */ 92 else if (res < 0)
108 events = malloc (sizeof (struct epoll_event) * eventmax); 93 {
94 if (errno == EBADF)
95 fd_ebadf ();
96 else if (errno == ENOMEM)
97 fd_enomem ();
98 }
109} 99}
110 100
101static void
102poll_init (int flags)
103{
104 ev_method = EVMETHOD_POLL;
105 method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */
106 method_modify = poll_modify;
107 method_poll = poll_poll;
108}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines