nginx 0.1.9
[nginx-catap.git] / src / os / unix / ngx_aio_read.c
blob2a66eb83c3fc57e36be276982221cb29dbdccb7e
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_aio.h>
12 #if (NGX_HAVE_KQUEUE)
13 #include <ngx_kqueue_module.h>
14 #endif
18 * the ready data requires 3 syscalls:
19 * aio_write(), aio_error(), aio_return()
20 * the non-ready data requires 4 (kqueue) or 5 syscalls:
21 * aio_write(), aio_error(), notifiction, aio_error(), aio_return()
22 * timeout, aio_cancel(), aio_error()
25 ssize_t ngx_aio_read(ngx_connection_t *c, u_char *buf, size_t size)
27 int n;
28 ngx_event_t *rev;
30 rev = c->read;
32 if (!rev->ready) {
33 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "second aio post");
34 return NGX_AGAIN;
37 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
38 "rev->complete: %d", rev->complete);
39 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
40 "aio size: %d", size);
42 if (!rev->complete) {
43 ngx_memzero(&rev->aiocb, sizeof(struct aiocb));
45 rev->aiocb.aio_fildes = c->fd;
46 rev->aiocb.aio_buf = buf;
47 rev->aiocb.aio_nbytes = size;
49 #if (NGX_HAVE_KQUEUE)
50 rev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
51 rev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
52 rev->aiocb.aio_sigevent.sigev_value.sigval_ptr = rev;
53 #endif
55 if (aio_read(&rev->aiocb) == -1) {
56 ngx_log_error(NGX_LOG_CRIT, rev->log, ngx_errno,
57 "aio_read() failed");
58 rev->error = 1;
59 return NGX_ERROR;
62 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
63 "aio_read: #%d OK", c->fd);
65 rev->active = 1;
66 rev->ready = 0;
69 rev->complete = 0;
71 n = aio_error(&rev->aiocb);
72 if (n == -1) {
73 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, "aio_error() failed");
74 rev->error = 1;
75 return NGX_ERROR;
78 if (n != 0) {
79 if (n == NGX_EINPROGRESS) {
80 if (rev->ready) {
81 ngx_log_error(NGX_LOG_ALERT, c->log, n,
82 "aio_read() still in progress");
83 rev->ready = 0;
85 return NGX_AGAIN;
88 ngx_log_error(NGX_LOG_CRIT, c->log, n, "aio_read() failed");
89 rev->error = 1;
90 rev->ready = 0;
91 return NGX_ERROR;
94 n = aio_return(&rev->aiocb);
95 if (n == -1) {
96 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
97 "aio_return() failed");
99 rev->error = 1;
100 rev->ready = 0;
101 return NGX_ERROR;
104 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, rev->log, 0,
105 "aio_read: #%d %d", c->fd, n);
107 if (n == 0) {
108 rev->eof = 1;
109 rev->ready = 0;
110 } else {
111 rev->ready = 1;
114 rev->active = 0;
116 return n;