… | |
… | |
124 | * our syscalls return < 0, not == -1, on error. which is good |
124 | * our syscalls return < 0, not == -1, on error. which is good |
125 | * enough for linux aio. |
125 | * enough for linux aio. |
126 | * TODO: arm is also common nowadays, maybe even mips and x86 |
126 | * TODO: arm is also common nowadays, maybe even mips and x86 |
127 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
127 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
128 | */ |
128 | */ |
129 | #if __GNUC__ && __linux && ECB_AMD64 |
129 | #if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__ |
|
|
130 | /* the costly errno access probably kills this for size optimisation */ |
130 | |
131 | |
131 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \ |
132 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \ |
132 | ({ \ |
133 | ({ \ |
133 | long res; \ |
134 | long res; \ |
134 | register unsigned long r5 __asm__ ("r8" ); \ |
135 | register unsigned long r5 __asm__ ("r8" ); \ |
… | |
… | |
288 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | ANIOCBP iocb = linuxaio_iocbps [fd]; |
289 | |
290 | |
290 | if (iocb->io.aio_reqprio < 0) |
291 | if (iocb->io.aio_reqprio < 0) |
291 | { |
292 | { |
292 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* we handed this fd over to epoll, so undo this first */ |
293 | /* we do it manually because the optimisations on epoll_modfy won't do us any good */ |
294 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
294 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
295 | anfds [fd].emask = 0; |
296 | anfds [fd].emask = 0; |
296 | iocb->io.aio_reqprio = 0; |
297 | iocb->io.aio_reqprio = 0; |
297 | } |
298 | } |
298 | |
299 | |
299 | if (iocb->io.aio_buf) |
300 | if (iocb->io.aio_buf) |
300 | /* io_cancel always returns some error on relevant kernels, but works */ |
301 | { |
301 | evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); |
302 | evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); |
|
|
303 | /* on relevant kernels, io_cancel fails with EINPROGRES if everything is fine */ |
|
|
304 | assert (("libev: linuxaio unexpected io_cancel failed", errno != EINPROGRESS)); |
|
|
305 | } |
302 | |
306 | |
303 | if (nev) |
307 | if (nev) |
304 | { |
308 | { |
305 | iocb->io.aio_buf = |
309 | iocb->io.aio_buf = |
306 | (nev & EV_READ ? POLLIN : 0) |
310 | (nev & EV_READ ? POLLIN : 0) |
… | |
… | |
318 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
322 | linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
319 | { |
323 | { |
320 | epoll_poll (EV_A_ 0); |
324 | epoll_poll (EV_A_ 0); |
321 | } |
325 | } |
322 | |
326 | |
323 | static void |
327 | inline_speed |
|
|
328 | void |
324 | linuxaio_fd_rearm (EV_P_ int fd) |
329 | linuxaio_fd_rearm (EV_P_ int fd) |
325 | { |
330 | { |
326 | anfds [fd].events = 0; |
331 | anfds [fd].events = 0; |
327 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
332 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
328 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
333 | fd_change (EV_A_ fd, EV_ANFD_REIFY); |
… | |
… | |
344 | fd, |
349 | fd, |
345 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
350 | (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
346 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
351 | | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
347 | ); |
352 | ); |
348 | |
353 | |
349 | /* linux aio is oneshot: rearm fd. TODO: this does more work than needed */ |
354 | /* linux aio is oneshot: rearm fd. TODO: this does more work than strictly needed */ |
350 | linuxaio_fd_rearm (EV_A_ fd); |
355 | linuxaio_fd_rearm (EV_A_ fd); |
351 | |
356 | |
352 | --nr; |
357 | --nr; |
353 | ++ev; |
358 | ++ev; |
354 | } |
359 | } |
… | |
… | |
424 | /* ignored */; |
429 | /* ignored */; |
425 | else |
430 | else |
426 | ev_syserr ("(libev) linuxaio io_getevents"); |
431 | ev_syserr ("(libev) linuxaio io_getevents"); |
427 | else if (res) |
432 | else if (res) |
428 | { |
433 | { |
429 | /* at least one event received, handle it and any remaining ones in the ring buffer */ |
434 | /* at least one event available, handle it and any remaining ones in the ring buffer */ |
430 | linuxaio_parse_events (EV_A_ ioev, res); |
435 | linuxaio_parse_events (EV_A_ ioev, res); |
431 | linuxaio_get_events_from_ring (EV_A); |
436 | linuxaio_get_events_from_ring (EV_A); |
432 | } |
437 | } |
433 | } |
438 | } |
434 | |
439 | |
435 | static int |
440 | inline_size |
|
|
441 | int |
436 | linuxaio_io_setup (EV_P) |
442 | linuxaio_io_setup (EV_P) |
437 | { |
443 | { |
438 | linuxaio_ctx = 0; |
444 | linuxaio_ctx = 0; |
439 | return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); |
445 | return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); |
440 | } |
446 | } |
… | |
… | |
506 | /* it's easiest to handle this mess in another iteration */ |
512 | /* it's easiest to handle this mess in another iteration */ |
507 | return; |
513 | return; |
508 | } |
514 | } |
509 | else if (errno == EBADF) |
515 | else if (errno == EBADF) |
510 | { |
516 | { |
|
|
517 | assert (("libev: event loop rejected bad fd", errno != EBADF)); |
511 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
518 | fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes); |
512 | |
519 | |
513 | res = 1; /* skip this iocb */ |
520 | res = 1; /* skip this iocb */ |
514 | } |
521 | } |
515 | else |
522 | else |
… | |
… | |
568 | void |
575 | void |
569 | linuxaio_destroy (EV_P) |
576 | linuxaio_destroy (EV_P) |
570 | { |
577 | { |
571 | epoll_destroy (EV_A); |
578 | epoll_destroy (EV_A); |
572 | linuxaio_free_iocbp (EV_A); |
579 | linuxaio_free_iocbp (EV_A); |
573 | evsys_io_destroy (linuxaio_ctx); |
580 | evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */ |
574 | } |
581 | } |
575 | |
582 | |
576 | inline_size |
583 | inline_size |
577 | void |
584 | void |
578 | linuxaio_fork (EV_P) |
585 | linuxaio_fork (EV_P) |
… | |
… | |
584 | linuxaio_iteration = 0; /* we start over in the child */ |
591 | linuxaio_iteration = 0; /* we start over in the child */ |
585 | |
592 | |
586 | while (linuxaio_io_setup (EV_A) < 0) |
593 | while (linuxaio_io_setup (EV_A) < 0) |
587 | ev_syserr ("(libev) linuxaio io_setup"); |
594 | ev_syserr ("(libev) linuxaio io_setup"); |
588 | |
595 | |
|
|
596 | /* forking epoll should also effectively unregister all fds from the backend */ |
589 | epoll_fork (EV_A); |
597 | epoll_fork (EV_A); |
590 | |
598 | |
591 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
599 | ev_io_stop (EV_A_ &linuxaio_epoll_w); |
592 | ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); |
600 | ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); |
593 | ev_io_start (EV_A_ &linuxaio_epoll_w); |
601 | ev_io_start (EV_A_ &linuxaio_epoll_w); |