nginx/src/os/unix/ngx_aio_read.c

118 lines
2.8 KiB
C
Raw Normal View History

2003-05-21 21:28:21 +08:00
/*
* Copyright (C) 2002-2004 Igor Sysoev
*/
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
2004-03-30 14:27:36 +08:00
ssize_t ngx_aio_read(ngx_connection_t *c, u_char *buf, size_t size)
2003-05-21 21:28:21 +08:00
{
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) {
2004-03-04 15:04:55 +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
}
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"rev->complete: %d", rev->complete);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"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;
}
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"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;
}
2004-02-12 01:08:49 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, rev->log, 0,
"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
}