nginx/src/os/unix/ngx_aio_write.c

99 lines
2.3 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
ssize_t ngx_aio_write(ngx_connection_t *c, char *buf, size_t size)
{
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-29 16:30:44 +08:00
if (wev->active) {
return NGX_AGAIN;
2003-05-21 21:28:21 +08:00
}
2003-10-29 16:30:44 +08:00
ngx_log_debug(wev->log, "aio: wev->aio_complete: %d" _ wev->aio_complete);
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
if (!wev->aio_complete) {
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;
}
2003-10-29 16:30:44 +08:00
ngx_log_debug(wev->log, "aio_write: OK");
2003-05-21 21:28:21 +08:00
2003-10-29 16:30:44 +08:00
wev->active = 1;
2003-05-21 21:28:21 +08:00
}
2003-10-29 16:30:44 +08:00
wev->aio_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) {
if (!wev->active) {
ngx_log_error(NGX_LOG_ALERT, wev->log, n,
2003-05-21 21:28:21 +08:00
"aio_write() still in progress");
}
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-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-05-21 21:28:21 +08:00
return NGX_ERROR;
}
2003-10-29 16:30:44 +08:00
wev->active = 0;
ngx_log_debug(wev->log, "aio_write: %d" _ n);
if (n == 0) {
wev->eof = 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
}