2003-05-21 21:28:21 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-06-11 23:28:34 +08:00
|
|
|
#include <ngx_event.h>
|
2003-05-21 21:28:21 +08:00
|
|
|
#include <ngx_aio.h>
|
|
|
|
|
|
|
|
#if (HAVE_KQUEUE)
|
|
|
|
#include <ngx_kqueue_module.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-10-29 16:30:44 +08:00
|
|
|
* the ready data requires 3 syscalls:
|
|
|
|
* aio_write(), aio_error(), aio_return()
|
|
|
|
* the non-ready data requires 4 (kqueue) or 5 syscalls:
|
|
|
|
* aio_write(), aio_error(), notifiction, aio_error(), aio_return()
|
|
|
|
* timeout, aio_cancel(), aio_error()
|
|
|
|
*/
|
2003-05-21 21:28:21 +08:00
|
|
|
|
|
|
|
ssize_t ngx_aio_read(ngx_connection_t *c, char *buf, size_t size)
|
|
|
|
{
|
2003-10-29 16:30:44 +08:00
|
|
|
int n;
|
|
|
|
ngx_event_t *rev;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
rev = c->read;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
if (!rev->ready) {
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "SECOND AIO POST");
|
2003-10-29 16:30:44 +08:00
|
|
|
return NGX_AGAIN;
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|
|
|
|
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_debug(c->log, "rev->complete: %d" _ rev->complete);
|
|
|
|
ngx_log_debug(c->log, "aio size: %d" _ size);
|
2003-10-30 16:51:06 +08:00
|
|
|
|
|
|
|
if (!rev->complete) {
|
2003-10-29 16:30:44 +08:00
|
|
|
ngx_memzero(&rev->aiocb, sizeof(struct aiocb));
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->aiocb.aio_fildes = c->fd;
|
|
|
|
rev->aiocb.aio_buf = buf;
|
|
|
|
rev->aiocb.aio_nbytes = size;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
|
|
|
#if (HAVE_KQUEUE)
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
|
|
|
|
rev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
|
|
|
|
rev->aiocb.aio_sigevent.sigev_value.sigval_ptr = rev;
|
2003-05-21 21:28:21 +08:00
|
|
|
#endif
|
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
if (aio_read(&rev->aiocb) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, rev->log, ngx_errno,
|
2003-05-21 21:28:21 +08:00
|
|
|
"aio_read() failed");
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->error = 1;
|
2003-05-21 21:28:21 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_debug(c->log, "aio_read: #%d OK" _ c->fd);
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->active = 1;
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->ready = 0;
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->complete = 0;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
n = aio_error(&rev->aiocb);
|
|
|
|
if (n == -1) {
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, "aio_error() failed");
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->error = 1;
|
2003-05-21 21:28:21 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
if (n != 0) {
|
|
|
|
if (n == NGX_EINPROGRESS) {
|
2003-10-30 16:51:06 +08:00
|
|
|
if (rev->ready) {
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, c->log, n,
|
2003-05-21 21:28:21 +08:00
|
|
|
"aio_read() still in progress");
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->ready = 0;
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, c->log, n, "aio_read() failed");
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->error = 1;
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->ready = 0;
|
2003-05-21 21:28:21 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
n = aio_return(&rev->aiocb);
|
|
|
|
if (n == -1) {
|
2003-11-14 15:20:34 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
2003-10-29 16:30:44 +08:00
|
|
|
"aio_return() failed");
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
rev->error = 1;
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->ready = 0;
|
2003-05-21 21:28:21 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
ngx_log_debug(rev->log, "aio_read: #%d %d" _ c->fd _ n);
|
2003-10-29 16:30:44 +08:00
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
rev->eof = 1;
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->ready = 0;
|
|
|
|
} else {
|
|
|
|
rev->ready = 1;
|
2003-10-29 16:30:44 +08:00
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
rev->active = 0;
|
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
return n;
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|