| 152 | } |
| 153 | |
| 154 | static void test_dispatch(int fd_read, int fd_write) |
| 155 | { |
| 156 | #ifdef EV_DISPATCH |
| 157 | int kqfd; |
| 158 | struct kevent changes[1]; |
| 159 | struct kevent events[2]; |
| 160 | int n; |
| 161 | char buf[8]; |
| 162 | |
| 163 | kqfd = kqueue(); |
| 164 | if (kqfd < 0) { |
| 165 | test_fail("kqueue() failed (dispatch)"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | EV_SET(&changes[0], fd_read, EVFILT_READ, |
| 170 | EV_ADD | EV_ENABLE | EV_DISPATCH, 0, 0, (void *) 0x33); |
| 171 | if (kevent(kqfd, changes, 1, NULL, 0, NULL) < 0) { |
| 172 | test_fail("kevent add dispatch failed"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (write(fd_write, "y", 1) != 1) { |
| 177 | test_fail("write failed (dispatch)"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | memset(events, 0, sizeof(events)); |
| 182 | n = kevent(kqfd, NULL, 0, events, 2, NULL); |
| 183 | if (n != 1 || events[0].filter != EVFILT_READ) { |
| 184 | test_fail("dispatch: first read event missing"); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | (void) read(fd_read, buf, sizeof(buf)); |
| 189 | |
| 190 | /* The event should be disabled after the first delivery. */ |
| 191 | memset(events, 0, sizeof(events)); |
| 192 | n = kevent(kqfd, NULL, 0, events, 2, &(struct timespec){0, 0}); |
| 193 | if (n != 0) { |
| 194 | test_fail("dispatch: event should be disabled"); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | /* Re-enable and verify it fires again. */ |
| 199 | EV_SET(&changes[0], fd_read, EVFILT_READ, EV_ENABLE, 0, 0, NULL); |
| 200 | if (kevent(kqfd, changes, 1, NULL, 0, NULL) < 0) { |
| 201 | test_fail("dispatch: enable failed"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (write(fd_write, "z", 1) != 1) { |
| 206 | test_fail("write failed (dispatch re-enable)"); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | memset(events, 0, sizeof(events)); |
| 211 | n = kevent(kqfd, NULL, 0, events, 2, NULL); |