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.4 by root, Sun Nov 4 15:58:50 2007 UTC vs.
Revision 1.14 by root, Mon Nov 12 20:03:39 2007 UTC

40 40
41static void 41static void
42poll_modify (EV_P_ int fd, int oev, int nev) 42poll_modify (EV_P_ int fd, int oev, int nev)
43{ 43{
44 int idx; 44 int idx;
45
46 if (oev == nev)
47 return;
48
45 array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init); 49 array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init);
46 50
47 idx = pollidxs [fd]; 51 idx = pollidxs [fd];
48 52
49 if (idx < 0) /* need to allocate a new pollfd */ 53 if (idx < 0) /* need to allocate a new pollfd */
50 { 54 {
51 idx = pollcnt++; 55 pollidxs [fd] = idx = pollcnt++;
52 array_needsize (polls, pollmax, pollcnt, ); 56 array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2);
53 polls [idx].fd = fd; 57 polls [idx].fd = fd;
54 } 58 }
59
60 assert (polls [idx].fd == fd);
55 61
56 if (nev) 62 if (nev)
57 polls [idx].events = 63 polls [idx].events =
58 (nev & EV_READ ? POLLIN : 0) 64 (nev & EV_READ ? POLLIN : 0)
59 | (nev & EV_WRITE ? POLLOUT : 0); 65 | (nev & EV_WRITE ? POLLOUT : 0);
60 else /* remove pollfd */ 66 else /* remove pollfd */
61 { 67 {
68 pollidxs [fd] = -1;
69
62 if (idx < pollcnt--) 70 if (idx < --pollcnt)
63 { 71 {
64 pollidxs [fd] = -1;
65 polls [idx] = polls [pollcnt]; 72 polls [idx] = polls [pollcnt];
66 pollidxs [polls [idx].fd] = idx; 73 pollidxs [polls [idx].fd] = idx;
67 } 74 }
68 } 75 }
69} 76}
70 77
71static void 78static void
72poll_poll (EV_P_ ev_tstamp timeout) 79poll_poll (EV_P_ ev_tstamp timeout)
73{ 80{
81 int i;
74 int res = poll (polls, pollcnt, ceil (timeout * 1000.)); 82 int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.));
75 83
76 if (res > 0) 84 if (res < 0)
77 {
78 int i;
79
80 for (i = 0; i < pollcnt; ++i)
81 fd_event (
82 EV_A_
83 polls [i].fd,
84 (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
85 | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
86 );
87 }
88 else if (res < 0)
89 { 85 {
90 if (errno == EBADF) 86 if (errno == EBADF)
91 fd_ebadf (EV_A); 87 fd_ebadf (EV_A);
92 else if (errno == ENOMEM) 88 else if (errno == ENOMEM && !syserr_cb)
93 fd_enomem (EV_A); 89 fd_enomem (EV_A);
90 else if (errno != EINTR)
91 syserr ("(libev) poll");
92
93 return;
94 } 94 }
95
96 for (i = 0; i < pollcnt; ++i)
97 if (polls [i].revents & POLLNVAL)
98 fd_kill (EV_A_ polls [i].fd);
99 else
100 fd_event (
101 EV_A_
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 );
95} 106}
96 107
97static int 108static int
98poll_init (EV_P_ int flags) 109poll_init (EV_P_ int flags)
99{ 110{
100 method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ 111 method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */
101 method_modify = poll_modify; 112 method_modify = poll_modify;
102 method_poll = poll_poll; 113 method_poll = poll_poll;
103 114
104 pollidxs = 0; pollidxmax = 0; 115 pollidxs = 0; pollidxmax = 0;
105 polls = 0; pollsmax = 0; pollscnt = 0; 116 polls = 0; pollmax = 0; pollcnt = 0;
106 117
107 return EVMETHOD_POLL; 118 return EVMETHOD_POLL;
108} 119}
109 120
110static void 121static void
111poll_destroy (EV_P) 122poll_destroy (EV_P)
112{ 123{
113 free (pollidxs); 124 ev_free (pollidxs);
114 free (polls); 125 ev_free (polls);
115} 126}
127

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines