aboutsummaryrefslogtreecommitdiff
path: root/src/os/unix/ngx_aio_write.c
blob: 5eded37b727429702f8c56b4d11b3b34b93966b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#include <ngx_aio.h>

#if (HAVE_KQUEUE)
#include <ngx_kqueue_module.h>
#endif


/*
    The data is ready - 3 syscalls:
        aio_write(), aio_error(), aio_return()
    The data is not ready - 4 (kqueue) or 5 syscalls:
        aio_write(), aio_error(), notifiction,
                                             aio_error(), aio_return()
                                             aio_cancel(), aio_error()
*/

ssize_t ngx_aio_write(ngx_connection_t *c, char *buf, size_t size)
{
    int           rc, first, canceled;
    ngx_event_t  *ev;

    ev = c->write;

    canceled = 0;

ngx_log_debug(ev->log, "aio: ev->ready: %d" _ ev->ready);
ngx_log_debug(ev->log, "aio: aiocb: %08x" _ &ev->aiocb);

#if 0
    if (ev->timedout) {
        ngx_set_socket_errno(NGX_ETIMEDOUT);
        ngx_log_error(NGX_LOG_ERR, ev->log, 0, "aio_write() timed out");

        rc = aio_cancel(c->fd, &ev->aiocb);
        if (rc == -1) {
            ngx_log_error(NGX_LOG_CRIT, ev->log, ngx_errno,
                          "aio_cancel() failed");
            return NGX_ERROR;
        }

        ngx_log_debug(ev->log, "aio_cancel: %d" _ rc);

        canceled = 1;

        ev->ready = 1;
    }
#endif

    first = 0;

    if (!ev->ready) {
        ngx_memzero(&ev->aiocb, sizeof(struct aiocb));

        ev->aiocb.aio_fildes = c->fd;
        ev->aiocb.aio_buf = buf;
        ev->aiocb.aio_nbytes = size;

#if (HAVE_KQUEUE)
        ev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
        ev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
        ev->aiocb.aio_sigevent.sigev_value.sigval_ptr = ev;
#endif

        if (aio_write(&ev->aiocb) == -1) {
            ngx_log_error(NGX_LOG_CRIT, ev->log, ngx_errno,
                          "aio_write() failed");
            return NGX_ERROR;
        }

        ngx_log_debug(ev->log, "aio_write: OK");

        ev->active = 1;
        first = 1;
    }

    ev->ready = 0;

    rc = aio_error(&ev->aiocb);
    if (rc == -1) {
        ngx_log_error(NGX_LOG_CRIT, ev->log, ngx_errno, "aio_error() failed");
        return NGX_ERROR;
    }

    if (rc != 0) {
        if (rc == NGX_EINPROGRESS) {
            if (!first) {
                ngx_log_error(NGX_LOG_CRIT, ev->log, rc,
                              "aio_write() still in progress");
            }
            return NGX_AGAIN;
        }

        if (rc == NGX_ECANCELED && canceled) {
            return NGX_ERROR;
        }

        ngx_log_error(NGX_LOG_CRIT, ev->log, rc, "aio_write() failed");
        return NGX_ERROR;
    }

    rc = aio_return(&ev->aiocb);
    if (rc == -1) {
        ngx_log_error(NGX_LOG_CRIT, ev->log, ngx_errno, "aio_return() failed");

        return NGX_ERROR;
    }

    ev->active = 0;
    ngx_log_debug(ev->log, "aio_write: %d" _ rc);

    return rc;
}