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

Comparing libev/ev_epoll.c (file contents):
Revision 1.34 by root, Fri May 23 16:37:38 2008 UTC vs.
Revision 1.36 by root, Mon Oct 27 11:08:29 2008 UTC

55 * epoll_ctl syscalls for common usage patterns. 55 * epoll_ctl syscalls for common usage patterns.
56 */ 56 */
57 57
58#include <sys/epoll.h> 58#include <sys/epoll.h>
59 59
60void inline_size
61unsigned_char_init (unsigned char *base, int count)
62{
63 /* memset might be overkill */
64 while (count--)
65 *base++ = 0;
66}
67
60static void 68static void
61epoll_modify (EV_P_ int fd, int oev, int nev) 69epoll_modify (EV_P_ int fd, int oev, int nev)
62{ 70{
63 struct epoll_event ev; 71 struct epoll_event ev;
72 unsigned char oldmask;
64 73
65 /* 74 /*
66 * we handle EPOLL_CTL_DEL by ignoring it here 75 * we handle EPOLL_CTL_DEL by ignoring it here
67 * on the assumption that the fd is gone anyways 76 * on the assumption that the fd is gone anyways
68 * if that is wrong, we have to handle the spurious 77 * if that is wrong, we have to handle the spurious
69 * event in epoll_poll. 78 * event in epoll_poll.
79 * the fd is later added, we try to ADD it, and, if that
80 * fails, we assume it still has the same eventmask.
70 */ 81 */
71 if (!nev) 82 if (!nev)
72 return; 83 return;
73 84
74 ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */ 85 oldmask = anfds [fd].emask;
86 anfds [fd].emask = nev;
87
88 /* store the generation counter in the upper 32 bits */
89 ev.data.u64 = fd | ((uint64_t)++anfds [fd].egen << 32);
75 ev.events = (nev & EV_READ ? EPOLLIN : 0) 90 ev.events = (nev & EV_READ ? EPOLLIN : 0)
76 | (nev & EV_WRITE ? EPOLLOUT : 0); 91 | (nev & EV_WRITE ? EPOLLOUT : 0);
77 92
78 if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) 93 if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev)))
79 return; 94 return;
80 95
81 if (expect_true (errno == ENOENT)) 96 if (expect_true (errno == ENOENT))
82 { 97 {
83 /* on ENOENT the fd went away, so try to do the right thing */ 98 /* if ENOENT then the fd went away, so try to do the right thing */
84 if (!nev) 99 if (!nev)
85 return; 100 goto dec_egen;
86 101
87 if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)) 102 if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev))
88 return; 103 return;
89 } 104 }
90 else if (expect_true (errno == EEXIST)) 105 else if (expect_true (errno == EEXIST))
91 { 106 {
92 /* on EEXIST we ignored a previous DEL */ 107 /* EEXIST means we ignored a previous DEL, but the fd is still active */
108 /* if the kernel mask is the same as the new mask, we assume it hasn't changed */
109 if (oldmask == nev)
110 goto dec_egen;
111
93 if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev)) 112 if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev))
94 return; 113 return;
95 } 114 }
96 115
97 fd_kill (EV_A_ fd); 116 fd_kill (EV_A_ fd);
117
118dec_egen:
119 /* we didn't successfully call epoll_ctl, so decrement the generation counter again */
120 --anfds [fd].egen;
98} 121}
99 122
100static void 123static void
101epoll_poll (EV_P_ ev_tstamp timeout) 124epoll_poll (EV_P_ ev_tstamp timeout)
102{ 125{
113 136
114 for (i = 0; i < eventcnt; ++i) 137 for (i = 0; i < eventcnt; ++i)
115 { 138 {
116 struct epoll_event *ev = epoll_events + i; 139 struct epoll_event *ev = epoll_events + i;
117 140
118 int fd = ev->data.u64; 141 int fd = (uint32_t)ev->data.u64; /* mask out the lower 32 bits */
119 int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) 142 int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
120 | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); 143 | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0);
121 int want = anfds [fd].events; 144 int want = anfds [fd].events;
122 145
146 if (anfds [fd].egen != (unsigned char)(ev->data.u64 >> 32))
147 /*fprintf (stderr, "spurious notification fd %d, %d vs %d\n", fd, (int)(ev->data.u64 >> 32), anfds [fd].egen);*/
148 continue;
149
123 if (expect_false (got & ~want)) 150 if (expect_false (got & ~want))
124 { 151 {
152 anfds [fd].emask = want;
153
125 /* we received an event but are not interested in it, try mod or del */ 154 /* we received an event but are not interested in it, try mod or del */
155 /* I don't think we ever need MOD, but let's handle it anyways */
126 ev->events = (want & EV_READ ? EPOLLIN : 0) 156 ev->events = (want & EV_READ ? EPOLLIN : 0)
127 | (want & EV_WRITE ? EPOLLOUT : 0); 157 | (want & EV_WRITE ? EPOLLOUT : 0);
128 158
129 epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev); 159 epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev);
130 } 160 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines