nginx/src/os/unix/ngx_aio_write_chain.c

81 lines
1.6 KiB
C
Raw Normal View History

2003-02-27 04:21:43 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2003-10-13 00:49:16 +08:00
#include <ngx_event.h>
2003-05-21 21:28:21 +08:00
#include <ngx_aio.h>
2003-02-27 04:21:43 +08:00
ngx_chain_t *ngx_aio_write_chain(ngx_connection_t *c, ngx_chain_t *in)
{
2003-10-29 16:30:44 +08:00
int n;
char *buf, *prev;
off_t sent;
size_t size;
ngx_err_t err;
ngx_chain_t *cl;
2003-02-27 04:21:43 +08:00
sent = 0;
2003-10-29 16:30:44 +08:00
cl = in;
2003-02-27 04:21:43 +08:00
2003-10-29 16:30:44 +08:00
while (cl) {
if (cl->hunk->last - cl->hunk->pos == 0) {
cl = cl->next;
continue;
}
2003-10-13 00:49:16 +08:00
/* we can post the single aio operation only */
if (c->write->active) {
2003-10-29 16:30:44 +08:00
return cl;
2003-10-13 00:49:16 +08:00
}
2003-10-29 16:30:44 +08:00
buf = cl->hunk->pos;
prev = buf;
2003-02-27 04:21:43 +08:00
size = 0;
2003-10-29 16:30:44 +08:00
/* coalesce the neighbouring hunks */
2003-10-13 00:49:16 +08:00
2003-10-29 16:30:44 +08:00
while (cl && prev == cl->hunk->pos) {
size += cl->hunk->last - cl->hunk->pos;
prev = cl->hunk->last;
cl = cl->next;
2003-02-27 04:21:43 +08:00
}
2003-10-29 16:30:44 +08:00
n = ngx_aio_write(c, buf, size);
2003-02-27 04:21:43 +08:00
2003-05-23 19:53:01 +08:00
#if (NGX_DEBUG_WRITE_CHAIN)
2003-10-29 16:30:44 +08:00
ngx_log_debug(c->log, "aio_write: %d" _ n);
2003-05-23 19:53:01 +08:00
#endif
2003-02-27 04:21:43 +08:00
2003-10-29 16:30:44 +08:00
if (n == NGX_ERROR) {
2003-10-13 00:49:16 +08:00
return NGX_CHAIN_ERROR;
}
2003-10-29 16:30:44 +08:00
if (n > 0) {
sent += n;
c->sent += n;
2003-02-27 04:21:43 +08:00
}
#if (NGX_DEBUG_WRITE_CHAIN)
2003-10-13 00:49:16 +08:00
ngx_log_debug(c->log, "aio_write sent: " OFF_FMT _ c->sent);
2003-02-27 04:21:43 +08:00
#endif
2003-10-29 16:30:44 +08:00
for (cl = in; cl; cl = cl->next) {
2003-02-27 04:21:43 +08:00
2003-10-29 16:30:44 +08:00
if (sent >= cl->hunk->last - cl->hunk->pos) {
sent -= cl->hunk->last - cl->hunk->pos;
cl->hunk->pos = cl->hunk->last;
2003-02-27 04:21:43 +08:00
2003-10-13 00:49:16 +08:00
continue;
}
2003-02-27 04:21:43 +08:00
2003-10-29 16:30:44 +08:00
cl->hunk->pos += sent;
2003-02-27 04:21:43 +08:00
2003-10-13 00:49:16 +08:00
break;
}
2003-02-27 04:21:43 +08:00
}
2003-10-29 16:30:44 +08:00
return cl;
2003-02-27 04:21:43 +08:00
}