src/os/unix/ngx_aio_read_chain.c - nginx-1.7.10

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>


  8. ssize_t
  9. ngx_aio_read_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit)
  10. {
  11.     int           n;
  12.     u_char       *buf, *prev;
  13.     size_t        size;
  14.     ssize_t       total;

  15.     if (c->read->pending_eof) {
  16.         c->read->ready = 0;
  17.         return 0;
  18.     }

  19.     total = 0;

  20.     while (cl) {

  21.         /* we can post the single aio operation only */

  22.         if (!c->read->ready) {
  23.             return total ? total : NGX_AGAIN;
  24.         }

  25.         buf = cl->buf->last;
  26.         prev = cl->buf->last;
  27.         size = 0;

  28.         /* coalesce the neighbouring bufs */

  29.         while (cl && prev == cl->buf->last) {
  30.             size += cl->buf->end - cl->buf->last;
  31.             prev = cl->buf->end;
  32.             cl = cl->next;
  33.         }

  34.         n = ngx_aio_read(c, buf, size);

  35.         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_read: %d", n);

  36.         if (n == NGX_AGAIN) {
  37.             return total ? total : NGX_AGAIN;
  38.         }

  39.         if (n == NGX_ERROR) {
  40.             return NGX_ERROR;
  41.         }

  42.         if (n == 0) {
  43.             c->read->pending_eof = 1;
  44.             if (total) {
  45.                 c->read->eof = 0;
  46.                 c->read->ready = 1;
  47.             }
  48.             return total;
  49.         }

  50.         if (n > 0) {
  51.             total += n;
  52.         }

  53.         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
  54.                        "aio_read total: %d", total);
  55.     }

  56.     return total ? total : NGX_AGAIN;
  57. }