1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10
11
12 ngx_chain_t *
13 ngx_aio_write_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
14 {
15 u_char *buf, *prev;
16 off_t send, sent;
17 size_t len;
18 ssize_t n, size;
19 ngx_chain_t *cl;
20
21 /* the maximum limit size is the maximum size_t value - the page size */
22
23 if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) {
24 limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize;
25 }
26
27 send = 0;
28 sent = 0;
29 cl = in;
30
31 while (cl) {
32
33 if (cl->buf->pos == cl->buf->last) {
34 cl = cl->next;
35 continue;
36 }
37
38 /* we can post the single aio operation only */
39
40 if (!c->write->ready) {
41 return cl;
42 }
43
44 buf = cl->buf->pos;
45 prev = buf;
46 len = 0;
47
48 /* coalesce the neighbouring bufs */
49
50 while (cl && prev == cl->buf->pos && send < limit) {
51 if (ngx_buf_special(cl->buf)) {
52 continue;
53 }
54
55 size = cl->buf->last - cl->buf->pos;
56
57 if (send + size > limit) {
58 size = limit - send;
59 }
60
61 len += size;
62 prev = cl->buf->pos + size;
63 send += size;
64 cl = cl->next;
65 }
66
67 n = ngx_aio_write(c, buf, len);
68
69 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_write: %z", n);
70
71 if (n == NGX_ERROR) {
72 return NGX_CHAIN_ERROR;
73 }
74
75 if (n > 0) {
76 sent += n;
77 c->sent += n;
78 }
79
80 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
81 "aio_write sent: %O", c->sent);
82
83 for (cl = in; cl; cl = cl->next) {
84
85 if (sent >= cl->buf->last - cl->buf->pos) {
86 sent -= cl->buf->last - cl->buf->pos;
87 cl->buf->pos = cl->buf->last;
88
89 continue;
90 }
91
92 cl->buf->pos += sent;
93
94 break;
95 }
96 }
97
98 return cl;
99 }
100
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.