… | |
… | |
243 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
243 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
244 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
244 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
245 | anfds [fd].emask = 0; |
245 | anfds [fd].emask = 0; |
246 | iocb->io.aio_reqprio = 0; |
246 | iocb->io.aio_reqprio = 0; |
247 | } |
247 | } |
248 | |
|
|
249 | if (ecb_expect_false (iocb->io.aio_buf)) |
248 | else if (ecb_expect_false (iocb->io.aio_buf)) |
250 | { |
249 | { |
251 | /* iocb active, so cancel it first before resubmit */ |
250 | /* iocb active, so cancel it first before resubmit */ |
252 | for (;;) |
251 | for (;;) |
253 | { |
252 | { |
254 | /* on all relevant kernels, io_cancel fails with EINPROGRESS on "success" */ |
253 | /* on all relevant kernels, io_cancel fails with EINPROGRESS on "success" */ |
… | |
… | |
257 | |
256 | |
258 | if (ecb_expect_true (errno == EINPROGRESS)) |
257 | if (ecb_expect_true (errno == EINPROGRESS)) |
259 | break; |
258 | break; |
260 | |
259 | |
261 | /* the EINPROGRESS test is for nicer error message. clumsy. */ |
260 | /* the EINPROGRESS test is for nicer error message. clumsy. */ |
|
|
261 | if (errno != EINTR) |
|
|
262 | { |
262 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS && errno != EINTR)); |
263 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINTR && errno != EINPROGRESS)); |
|
|
264 | break; |
|
|
265 | } |
263 | } |
266 | } |
264 | } |
267 | } |
265 | |
268 | |
266 | iocb->io.aio_buf = |
269 | iocb->io.aio_buf = |
267 | (nev & EV_READ ? POLLIN : 0) |
270 | (nev & EV_READ ? POLLIN : 0) |
… | |
… | |
321 | /* get any events from ring buffer, return true if any were handled */ |
324 | /* get any events from ring buffer, return true if any were handled */ |
322 | static int |
325 | static int |
323 | linuxaio_get_events_from_ring (EV_P) |
326 | linuxaio_get_events_from_ring (EV_P) |
324 | { |
327 | { |
325 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
328 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
|
|
329 | unsigned head, tail; |
326 | |
330 | |
327 | /* the kernel reads and writes both of these variables, */ |
331 | /* the kernel reads and writes both of these variables, */ |
328 | /* as a C extension, we assume that volatile use here */ |
332 | /* as a C extension, we assume that volatile use here */ |
329 | /* both makes reads atomic and once-only */ |
333 | /* both makes reads atomic and once-only */ |
330 | unsigned head = *(volatile unsigned *)&ring->head; |
334 | head = *(volatile unsigned *)&ring->head; |
|
|
335 | ECB_MEMORY_FENCE_ACQUIRE; |
331 | unsigned tail = *(volatile unsigned *)&ring->tail; |
336 | tail = *(volatile unsigned *)&ring->tail; |
332 | |
337 | |
333 | if (head == tail) |
338 | if (head == tail) |
334 | return 0; |
339 | return 0; |
335 | |
340 | |
336 | /* make sure the events up to tail are visible */ |
|
|
337 | ECB_MEMORY_FENCE_ACQUIRE; |
|
|
338 | |
|
|
339 | /* parse all available events, but only once, to avoid starvation */ |
341 | /* parse all available events, but only once, to avoid starvation */ |
340 | if (tail > head) /* normal case around */ |
342 | if (ecb_expect_true (tail > head)) /* normal case around */ |
341 | linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
343 | linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
342 | else /* wrapped around */ |
344 | else /* wrapped around */ |
343 | { |
345 | { |
344 | linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
346 | linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
345 | linuxaio_parse_events (EV_A_ ring->io_events, tail); |
347 | linuxaio_parse_events (EV_A_ ring->io_events, tail); |
… | |
… | |
394 | { |
396 | { |
395 | int res; |
397 | int res; |
396 | |
398 | |
397 | EV_RELEASE_CB; |
399 | EV_RELEASE_CB; |
398 | |
400 | |
399 | ts.tv_sec = (long)timeout; |
401 | EV_TS_SET (ts, timeout); |
400 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
|
|
401 | |
|
|
402 | res = evsys_io_getevents (linuxaio_ctx, 1, want, ioev, &ts); |
402 | res = evsys_io_getevents (linuxaio_ctx, 1, want, ioev, &ts); |
403 | |
403 | |
404 | EV_ACQUIRE_CB; |
404 | EV_ACQUIRE_CB; |
405 | |
405 | |
406 | if (res < 0) |
406 | if (res < 0) |
… | |
… | |
492 | } |
492 | } |
493 | |
493 | |
494 | ++linuxaio_iteration; |
494 | ++linuxaio_iteration; |
495 | if (linuxaio_io_setup (EV_A) < 0) |
495 | if (linuxaio_io_setup (EV_A) < 0) |
496 | { |
496 | { |
|
|
497 | /* TODO: rearm all and recreate epoll backend from scratch */ |
|
|
498 | /* TODO: might be more prudent? */ |
|
|
499 | |
497 | /* to bad, we can't get a new aio context, go 100% epoll */ |
500 | /* to bad, we can't get a new aio context, go 100% epoll */ |
498 | linuxaio_free_iocbp (EV_A); |
501 | linuxaio_free_iocbp (EV_A); |
499 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
502 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
500 | ev_ref (EV_A); |
503 | ev_ref (EV_A); |
501 | linuxaio_ctx = 0; |
504 | linuxaio_ctx = 0; |
|
|
505 | |
|
|
506 | backend = EVBACKEND_EPOLL; |
502 | backend_modify = epoll_modify; |
507 | backend_modify = epoll_modify; |
503 | backend_poll = epoll_poll; |
508 | backend_poll = epoll_poll; |
504 | } |
509 | } |
505 | |
510 | |
506 | timeout = 0; |
511 | timeout = 0; |
… | |
… | |
515 | res = 1; /* skip this iocb */ |
520 | res = 1; /* skip this iocb */ |
516 | } |
521 | } |
517 | else if (errno == EINTR) /* not seen in reality, not documented */ |
522 | else if (errno == EINTR) /* not seen in reality, not documented */ |
518 | res = 0; /* silently ignore and retry */ |
523 | res = 0; /* silently ignore and retry */ |
519 | else |
524 | else |
|
|
525 | { |
520 | ev_syserr ("(libev) linuxaio io_submit"); |
526 | ev_syserr ("(libev) linuxaio io_submit"); |
|
|
527 | res = 0; |
|
|
528 | } |
521 | |
529 | |
522 | submitted += res; |
530 | submitted += res; |
523 | } |
531 | } |
524 | |
532 | |
525 | linuxaio_submitcnt = 0; |
533 | linuxaio_submitcnt = 0; |
… | |
… | |
553 | ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); |
561 | ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); |
554 | ev_set_priority (&linuxaio_epoll_w, EV_MAXPRI); |
562 | ev_set_priority (&linuxaio_epoll_w, EV_MAXPRI); |
555 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
563 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
556 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
564 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
557 | |
565 | |
558 | backend_modify = linuxaio_modify; |
566 | backend_modify = linuxaio_modify; |
559 | backend_poll = linuxaio_poll; |
567 | backend_poll = linuxaio_poll; |
560 | |
568 | |
561 | linuxaio_iocbpmax = 0; |
569 | linuxaio_iocbpmax = 0; |
562 | linuxaio_iocbps = 0; |
570 | linuxaio_iocbps = 0; |
563 | |
571 | |
564 | linuxaio_submits = 0; |
572 | linuxaio_submits = 0; |
… | |
… | |
575 | epoll_destroy (EV_A); |
583 | epoll_destroy (EV_A); |
576 | linuxaio_free_iocbp (EV_A); |
584 | linuxaio_free_iocbp (EV_A); |
577 | evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */ |
585 | evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */ |
578 | } |
586 | } |
579 | |
587 | |
580 | inline_size |
588 | ecb_cold |
581 | void |
589 | static void |
582 | linuxaio_fork (EV_P) |
590 | linuxaio_fork (EV_P) |
583 | { |
591 | { |
584 | /* this frees all iocbs, which is very heavy-handed */ |
592 | /* this frees all iocbs, which is very heavy-handed */ |
585 | linuxaio_destroy (EV_A); |
593 | linuxaio_destroy (EV_A); |
586 | linuxaio_submitcnt = 0; /* all pointers were invalidated */ |
594 | linuxaio_submitcnt = 0; /* all pointers were invalidated */ |
… | |
… | |
590 | while (linuxaio_io_setup (EV_A) < 0) |
598 | while (linuxaio_io_setup (EV_A) < 0) |
591 | ev_syserr ("(libev) linuxaio io_setup"); |
599 | ev_syserr ("(libev) linuxaio io_setup"); |
592 | |
600 | |
593 | /* forking epoll should also effectively unregister all fds from the backend */ |
601 | /* forking epoll should also effectively unregister all fds from the backend */ |
594 | epoll_fork (EV_A); |
602 | epoll_fork (EV_A); |
|
|
603 | /* epoll_fork already did this. hopefully */ |
|
|
604 | /*fd_rearm_all (EV_A);*/ |
595 | |
605 | |
596 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
606 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
597 | ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); |
607 | ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); |
598 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
608 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
599 | |
|
|
600 | /* epoll_fork already did this. hopefully */ |
|
|
601 | /*fd_rearm_all (EV_A);*/ |
|
|
602 | } |
609 | } |
603 | |
610 | |