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

Comparing libev/ev_linuxaio.c (file contents):
Revision 1.10 by root, Sun Jun 23 02:02:24 2019 UTC vs.
Revision 1.15 by root, Mon Jun 24 00:04:26 2019 UTC

219static int 219static int
220linuxaio_get_events_from_ring (EV_P) 220linuxaio_get_events_from_ring (EV_P)
221{ 221{
222 struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; 222 struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx;
223 223
224 unsigned head = ring->head; 224 /* the kernel reads and writes both of these variables, */
225 /* as a C extension, we assume that volatile use here */
226 /* both makes reads atomic and once-only */
227 unsigned head = *(volatile unsigned *)&ring->head;
225 unsigned tail = *(volatile unsigned *)&ring->tail; 228 unsigned tail = *(volatile unsigned *)&ring->tail;
226 229
227 if (head == tail) 230 if (head == tail)
228 return 0; 231 return 0;
229 232
231 if (ecb_expect_false (ring->magic != AIO_RING_MAGIC) 234 if (ecb_expect_false (ring->magic != AIO_RING_MAGIC)
232 || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES 235 || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES
233 || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ 236 || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */
234 return 0; 237 return 0;
235 238
239 /* make sure the events up to tail are visible */
236 ECB_MEMORY_FENCE_ACQUIRE; 240 ECB_MEMORY_FENCE_ACQUIRE;
237 241
238 /* parse all available events, but only once, to avoid starvation */ 242 /* parse all available events, but only once, to avoid starvation */
239 if (tail > head) /* normal case around */ 243 if (tail > head) /* normal case around */
240 linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); 244 linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head);
242 { 246 {
243 linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); 247 linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head);
244 linuxaio_parse_events (EV_A_ ring->io_events, tail); 248 linuxaio_parse_events (EV_A_ ring->io_events, tail);
245 } 249 }
246 250
247 ring->head = tail; 251 /* as an extension to C, we hope that the volatile will makethis atomic and once-only */
252 *(volatile unsigned *)&ring->head = tail;
253 /* make sure kernel can see our new head value - probably not required */
254 ECB_MEMORY_FENCE_RELEASE;
248 255
249 return 1; 256 return 1;
250} 257}
251 258
252/* read at least one event from kernel, or timeout */ 259/* read at least one event from kernel, or timeout */
329 break; 336 break;
330 } 337 }
331#if EPOLL_FALLBACK 338#if EPOLL_FALLBACK
332 else if (errno == EINVAL) 339 else if (errno == EINVAL)
333 { 340 {
334 /* This hapΓΌpens for unsupported fds, officially, but in my testing, 341 /* This happens for unsupported fds, officially, but in my testing,
335 * also randomly happens for supported fds. We fall back to good old 342 * also randomly happens for supported fds. We fall back to good old
336 * poll() here, under the assumption that this is a very rare case. 343 * poll() here, under the assumption that this is a very rare case.
344 * See https://lore.kernel.org/patchwork/patch/1047453/ for evidence
345 * that the problem is known, but ignored.
337 */ 346 */
338 struct iocb *iocb = linuxaio_submits [submitted]; 347 struct iocb *iocb = linuxaio_submits [submitted];
339 res = 1; /* skip this iocb */ 348 res = 1; /* skip this iocb */
340 349
341 linuxaio_rearm_epoll (EV_A_ iocb, EPOLL_CTL_ADD); 350 linuxaio_rearm_epoll (EV_A_ iocb, EPOLL_CTL_ADD);
397int 406int
398linuxaio_init (EV_P_ int flags) 407linuxaio_init (EV_P_ int flags)
399{ 408{
400 /* would be great to have a nice test for IOCB_CMD_POLL instead */ 409 /* would be great to have a nice test for IOCB_CMD_POLL instead */
401 /* also: test some semi-common fd types, such as files and ttys in recommended_backends */ 410 /* also: test some semi-common fd types, such as files and ttys in recommended_backends */
402 if (ev_linux_version () < 0x041200) /* 4.18 introduced IOCB_CMD_POLL */ 411#if EPOLL_FALLBACK
412 /* 4.19 made epoll work */
413 if (ev_linux_version () < 0x041300)
403 return 0; 414 return 0;
415#else
416 /* 4.18 introduced IOCB_CMD_POLL */
417 if (ev_linux_version () < 0x041200)
418 return 0;
419#endif
404 420
405 linuxaio_ctx = 0; 421 linuxaio_ctx = 0;
406 if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) 422 if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0)
407 return 0; 423 return 0;
408 424
414 return 0; 430 return 0;
415 } 431 }
416 432
417 ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); 433 ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ);
418 ev_io_start (EV_A_ &linuxaio_epoll_w); 434 ev_io_start (EV_A_ &linuxaio_epoll_w);
435 ev_unref (EV_A); /* watcher should not keep loop alive */
419#endif 436#endif
420 437
421 backend_modify = linuxaio_modify; 438 backend_modify = linuxaio_modify;
422 backend_poll = linuxaio_poll; 439 backend_poll = linuxaio_poll;
423 440
459 ev_syserr ("(libev) linuxaio epoll_create"); 476 ev_syserr ("(libev) linuxaio epoll_create");
460 477
461 ev_io_stop (EV_A_ &linuxaio_epoll_w); 478 ev_io_stop (EV_A_ &linuxaio_epoll_w);
462 ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); 479 ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ);
463 ev_io_start (EV_A_ &linuxaio_epoll_w); 480 ev_io_start (EV_A_ &linuxaio_epoll_w);
464 ev_unref (EV_A); /* watcher should not keep loop alive */
465#endif 481#endif
466 482
467 fd_rearm_all (EV_A); 483 fd_rearm_all (EV_A);
468} 484}
469 485

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines