… | |
… | |
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 | |
|
|
60 | void inline_size |
|
|
61 | unsigned_char_init (unsigned char *base, int count) |
|
|
62 | { |
|
|
63 | /* memset might be overkill */ |
|
|
64 | while (count--) |
|
|
65 | *base++ = 0; |
|
|
66 | } |
|
|
67 | |
60 | static void |
68 | static void |
61 | epoll_modify (EV_P_ int fd, int oev, int nev) |
69 | epoll_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 | |
|
|
85 | oldmask = anfds [fd].emask; |
|
|
86 | anfds [fd].emask = nev; |
|
|
87 | |
74 | ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */ |
88 | ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */ |
75 | ev.events = (nev & EV_READ ? EPOLLIN : 0) |
89 | ev.events = (nev & EV_READ ? EPOLLIN : 0) |
76 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
90 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
77 | |
91 | |
78 | if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) |
92 | if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) |
79 | return; |
93 | return; |
80 | |
94 | |
81 | if (expect_true (errno == ENOENT)) |
95 | if (expect_true (errno == ENOENT)) |
82 | { |
96 | { |
83 | /* on ENOENT the fd went away, so try to do the right thing */ |
97 | /* if ENOENT then the fd went away, so try to do the right thing */ |
84 | if (!nev) |
98 | if (!nev) |
85 | return; |
99 | return; |
86 | |
100 | |
87 | if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)) |
101 | if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)) |
88 | return; |
102 | return; |
89 | } |
103 | } |
90 | else if (expect_true (errno == EEXIST)) |
104 | else if (expect_true (errno == EEXIST)) |
91 | { |
105 | { |
92 | /* on EEXIST we ignored a previous DEL */ |
106 | /* EEXIST means we ignored a previous DEL, but the fd is still active */ |
|
|
107 | /* if the kernel mask is the same as the new mask, we assume it hasn't changed */ |
93 | if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev)) |
108 | if (oldmask == nev || !epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev)) |
94 | return; |
109 | return; |
95 | } |
110 | } |
96 | |
111 | |
97 | fd_kill (EV_A_ fd); |
112 | fd_kill (EV_A_ fd); |
98 | } |
113 | } |
… | |
… | |
120 | | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); |
135 | | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); |
121 | int want = anfds [fd].events; |
136 | int want = anfds [fd].events; |
122 | |
137 | |
123 | if (expect_false (got & ~want)) |
138 | if (expect_false (got & ~want)) |
124 | { |
139 | { |
|
|
140 | anfds [fd].emask = want; |
|
|
141 | |
125 | /* we received an event but are not interested in it, try mod or del */ |
142 | /* we received an event but are not interested in it, try mod or del */ |
|
|
143 | /* I don't think we ever need MOD, but let's handle it anyways */ |
126 | ev->events = (want & EV_READ ? EPOLLIN : 0) |
144 | ev->events = (want & EV_READ ? EPOLLIN : 0) |
127 | | (want & EV_WRITE ? EPOLLOUT : 0); |
145 | | (want & EV_WRITE ? EPOLLOUT : 0); |
128 | |
146 | |
129 | epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev); |
147 | epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev); |
130 | } |
148 | } |