… | |
… | |
286 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
286 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
287 | { |
287 | { |
288 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
288 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
290 | |
290 | |
291 | if (iocb->io.aio_reqprio < 0) |
291 | if (ecb_expect_false (iocb->io.aio_reqprio < 0)) |
292 | { |
292 | { |
293 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* we handed this fd over to epoll, so undo this first */ |
294 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
294 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
296 | anfds [fd].emask = 0; |
296 | anfds [fd].emask = 0; |
297 | iocb->io.aio_reqprio = 0; |
297 | iocb->io.aio_reqprio = 0; |
298 | } |
298 | } |
299 | |
299 | |
300 | if (iocb->io.aio_buf) |
300 | if (ecb_expect_false (iocb->io.aio_buf)) |
301 | { |
301 | { |
|
|
302 | /* iocb active, so cancel it first before resubmit */ |
|
|
303 | for (;;) |
|
|
304 | { |
|
|
305 | /* on all relevant kernels, io_cancel fails with EINPROGRESS on "success" */ |
302 | evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); |
306 | if (ecb_expect_false (evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0) == 0)) |
303 | /* on relevant kernels, io_cancel fails with EINPROGRES if everything is fine */ |
307 | break; |
|
|
308 | |
|
|
309 | if (ecb_expect_true (errno == EINPROGRESS)) |
|
|
310 | break; |
|
|
311 | |
|
|
312 | /* the EINPROGRESS test is for nicer error message. clumsy. */ |
304 | assert (("libev: linuxaio unexpected io_cancel failed", errno == EINPROGRESS || errno == EINTR)); |
313 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS && errno != EINTR)); |
305 | } |
314 | } |
|
|
315 | } |
|
|
316 | |
|
|
317 | iocb->io.aio_buf = |
|
|
318 | (nev & EV_READ ? POLLIN : 0) |
|
|
319 | | (nev & EV_WRITE ? POLLOUT : 0); |
306 | |
320 | |
307 | if (nev) |
321 | if (nev) |
308 | { |
322 | { |
309 | iocb->io.aio_buf = |
|
|
310 | (nev & EV_READ ? POLLIN : 0) |
|
|
311 | | (nev & EV_WRITE ? POLLOUT : 0); |
|
|
312 | |
|
|
313 | /* queue iocb up for io_submit */ |
323 | /* queue iocb up for io_submit */ |
314 | /* this assumes we only ever get one call per fd per loop iteration */ |
324 | /* this assumes we only ever get one call per fd per loop iteration */ |
315 | ++linuxaio_submitcnt; |
325 | ++linuxaio_submitcnt; |
316 | array_needsize (struct iocb *, linuxaio_submits, linuxaio_submitmax, linuxaio_submitcnt, array_needsize_noinit); |
326 | array_needsize (struct iocb *, linuxaio_submits, linuxaio_submitmax, linuxaio_submitcnt, array_needsize_noinit); |
317 | linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
327 | linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
… | |
… | |
397 | int |
407 | int |
398 | linuxaio_ringbuf_valid (EV_P) |
408 | linuxaio_ringbuf_valid (EV_P) |
399 | { |
409 | { |
400 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
410 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
401 | |
411 | |
402 | return expect_true (ring->magic == AIO_RING_MAGIC) |
412 | return ecb_expect_true (ring->magic == AIO_RING_MAGIC) |
403 | && ring->incompat_features == EV_AIO_RING_INCOMPAT_FEATURES |
413 | && ring->incompat_features == EV_AIO_RING_INCOMPAT_FEATURES |
404 | && ring->header_length == sizeof (struct aio_ring); /* TODO: or use it to find io_event[0]? */ |
414 | && ring->header_length == sizeof (struct aio_ring); /* TODO: or use it to find io_event[0]? */ |
405 | } |
415 | } |
406 | |
416 | |
407 | /* read at least one event from kernel, or timeout */ |
417 | /* read at least one event from kernel, or timeout */ |
… | |
… | |
412 | struct timespec ts; |
422 | struct timespec ts; |
413 | struct io_event ioev[8]; /* 256 octet stack space */ |
423 | struct io_event ioev[8]; /* 256 octet stack space */ |
414 | int want = 1; /* how many events to request */ |
424 | int want = 1; /* how many events to request */ |
415 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
425 | int ringbuf_valid = linuxaio_ringbuf_valid (EV_A); |
416 | |
426 | |
417 | if (expect_true (ringbuf_valid)) |
427 | if (ecb_expect_true (ringbuf_valid)) |
418 | { |
428 | { |
419 | /* if the ring buffer has any events, we don't wait or call the kernel at all */ |
429 | /* if the ring buffer has any events, we don't wait or call the kernel at all */ |
420 | if (linuxaio_get_events_from_ring (EV_A)) |
430 | if (linuxaio_get_events_from_ring (EV_A)) |
421 | return; |
431 | return; |
422 | |
432 | |
… | |
… | |
452 | else if (res) |
462 | else if (res) |
453 | { |
463 | { |
454 | /* at least one event available, handle them */ |
464 | /* at least one event available, handle them */ |
455 | linuxaio_parse_events (EV_A_ ioev, res); |
465 | linuxaio_parse_events (EV_A_ ioev, res); |
456 | |
466 | |
457 | if (expect_true (ringbuf_valid)) |
467 | if (ecb_expect_true (ringbuf_valid)) |
458 | { |
468 | { |
459 | /* if we have a ring buffer, handle any remaining events in it */ |
469 | /* if we have a ring buffer, handle any remaining events in it */ |
460 | linuxaio_get_events_from_ring (EV_A); |
470 | linuxaio_get_events_from_ring (EV_A); |
461 | |
471 | |
462 | /* at this point, we should have handled all outstanding events */ |
472 | /* at this point, we should have handled all outstanding events */ |
… | |
… | |
493 | /* which allows us to pinpoint the erroneous iocb */ |
503 | /* which allows us to pinpoint the erroneous iocb */ |
494 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
504 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
495 | { |
505 | { |
496 | int res = evsys_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
506 | int res = evsys_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
497 | |
507 | |
498 | if (expect_false (res < 0)) |
508 | if (ecb_expect_false (res < 0)) |
499 | if (errno == EINVAL) |
509 | if (errno == EINVAL) |
500 | { |
510 | { |
501 | /* This happens for unsupported fds, officially, but in my testing, |
511 | /* This happens for unsupported fds, officially, but in my testing, |
502 | * also randomly happens for supported fds. We fall back to good old |
512 | * also randomly happens for supported fds. We fall back to good old |
503 | * poll() here, under the assumption that this is a very rare case. |
513 | * poll() here, under the assumption that this is a very rare case. |
… | |
… | |
553 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
563 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
554 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
564 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
555 | |
565 | |
556 | res = 1; /* skip this iocb */ |
566 | res = 1; /* skip this iocb */ |
557 | } |
567 | } |
558 | else if (errno == EINTR) |
568 | else if (errno == EINTR) /* not seen in reality, not documented */ |
559 | /* silently ignored */; |
569 | res = 0; /* silently ignore and retry */ |
560 | else |
570 | else |
561 | ev_syserr ("(libev) linuxaio io_submit"); |
571 | ev_syserr ("(libev) linuxaio io_submit"); |
562 | |
572 | |
563 | submitted += res; |
573 | submitted += res; |
564 | } |
574 | } |