nginx/src/os/unix/ngx_aio_write.c

113 lines
2.7 KiB
C
Raw Normal View History

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_write(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 *wev;
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
wev = c->write;
2003-05-21 21:28:21 +08:00
2003-10-30 16:51:06 +08:00
if (!wev->ready) {
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, wev->log, 0,
"aio: wev->complete: %d", wev->complete);
2003-05-21 21:28:21 +08:00
2003-10-30 16:51:06 +08:00
if (!wev->complete) {
2003-10-29 16:30:44 +08:00
ngx_memzero(&wev->aiocb, sizeof(struct aiocb));
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
wev->aiocb.aio_fildes = c->fd;
wev->aiocb.aio_buf = buf;
wev->aiocb.aio_nbytes = size;
2003-05-21 21:28:21 +08:00
#if (HAVE_KQUEUE)
2003-10-29 16:30:44 +08:00
wev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
wev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
wev->aiocb.aio_sigevent.sigev_value.sigval_ptr = wev;
2003-05-21 21:28:21 +08:00
#endif
2003-10-29 16:30:44 +08:00
if (aio_write(&wev->aiocb) == -1) {
ngx_log_error(NGX_LOG_CRIT, wev->log, ngx_errno,
2003-05-21 21:28:21 +08:00
"aio_write() failed");
return NGX_ERROR;
}
2004-02-12 01:08:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, wev->log, 0, "aio_write: OK");
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
wev->active = 1;
2003-10-30 16:51:06 +08:00
wev->ready = 0;
2003-05-21 21:28:21 +08:00
}
2003-10-30 16:51:06 +08:00
wev->complete = 0;
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
n = aio_error(&wev->aiocb);
if (n == -1) {
ngx_log_error(NGX_LOG_CRIT, wev->log, ngx_errno, "aio_error() failed");
wev->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 (wev->ready) {
2003-10-29 16:30:44 +08:00
ngx_log_error(NGX_LOG_ALERT, wev->log, n,
2003-05-21 21:28:21 +08:00
"aio_write() still in progress");
2003-10-30 16:51:06 +08:00
wev->ready = 0;
2003-05-21 21:28:21 +08:00
}
return NGX_AGAIN;
}
2003-10-29 16:30:44 +08:00
ngx_log_error(NGX_LOG_CRIT, wev->log, n, "aio_write() failed");
wev->error = 1;
2003-10-30 16:51:06 +08:00
wev->ready = 0;
#if 1
n = aio_return(&wev->aiocb);
if (n == -1) {
ngx_log_error(NGX_LOG_ALERT, wev->log, ngx_errno,
"aio_return() failed");
}
ngx_log_error(NGX_LOG_CRIT, wev->log, n, "aio_return() %d", n);
#endif
2003-05-21 21:28:21 +08:00
return NGX_ERROR;
}
2003-10-29 16:30:44 +08:00
n = aio_return(&wev->aiocb);
if (n == -1) {
ngx_log_error(NGX_LOG_ALERT, wev->log, ngx_errno,
"aio_return() failed");
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
wev->error = 1;
2003-10-30 16:51:06 +08:00
wev->ready = 0;
2003-05-21 21:28:21 +08:00
return NGX_ERROR;
}
2003-10-29 16:30:44 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, wev->log, 0, "aio_write: %d", n);
2003-10-29 16:30:44 +08:00
2003-10-30 16:51:06 +08:00
wev->active = 0;
wev->ready = 1;
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
return n;
2003-05-21 21:28:21 +08:00
}