src/os/unix/ngx_aio_write.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. extern int  ngx_kqueue;


  9. ssize_t
  10. ngx_aio_write(ngx_connection_t *c, u_char *buf, size_t size)
  11. {
  12.     int           n;
  13.     ngx_event_t  *wev;

  14.     wev = c->write;

  15.     if (!wev->ready) {
  16.         return NGX_AGAIN;
  17.     }

  18.     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, wev->log, 0,
  19.                    "aio: wev->complete: %d", wev->complete);

  20.     if (!wev->complete) {
  21.         ngx_memzero(&wev->aiocb, sizeof(struct aiocb));

  22.         wev->aiocb.aio_fildes = c->fd;
  23.         wev->aiocb.aio_buf = buf;
  24.         wev->aiocb.aio_nbytes = size;

  25. #if (NGX_HAVE_KQUEUE)
  26.         wev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
  27.         wev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
  28.         wev->aiocb.aio_sigevent.sigev_value.sigval_ptr = wev;
  29. #endif

  30.         if (aio_write(&wev->aiocb) == -1) {
  31.             ngx_log_error(NGX_LOG_CRIT, wev->log, ngx_errno,
  32.                           "aio_write() failed");
  33.             return NGX_ERROR;
  34.         }

  35.         ngx_log_debug0(NGX_LOG_DEBUG_EVENT, wev->log, 0, "aio_write: OK");

  36.         wev->active = 1;
  37.         wev->ready = 0;
  38.     }

  39.     wev->complete = 0;

  40.     n = aio_error(&wev->aiocb);
  41.     if (n == -1) {
  42.         ngx_log_error(NGX_LOG_CRIT, wev->log, ngx_errno, "aio_error() failed");
  43.         wev->error = 1;
  44.         return NGX_ERROR;
  45.     }

  46.     if (n != 0) {
  47.         if (n == NGX_EINPROGRESS) {
  48.             if (wev->ready) {
  49.                 ngx_log_error(NGX_LOG_ALERT, wev->log, n,
  50.                               "aio_write() still in progress");
  51.                 wev->ready = 0;
  52.             }
  53.             return NGX_AGAIN;
  54.         }

  55.         ngx_log_error(NGX_LOG_CRIT, wev->log, n, "aio_write() failed");
  56.         wev->error = 1;
  57.         wev->ready = 0;

  58. #if 1
  59.         n = aio_return(&wev->aiocb);
  60.         if (n == -1) {
  61.             ngx_log_error(NGX_LOG_ALERT, wev->log, ngx_errno,
  62.                           "aio_return() failed");
  63.         }

  64.         ngx_log_error(NGX_LOG_CRIT, wev->log, n, "aio_return() %d", n);
  65. #endif

  66.         return NGX_ERROR;
  67.     }

  68.     n = aio_return(&wev->aiocb);
  69.     if (n == -1) {
  70.         ngx_log_error(NGX_LOG_ALERT, wev->log, ngx_errno,
  71.                       "aio_return() failed");

  72.         wev->error = 1;
  73.         wev->ready = 0;
  74.         return NGX_ERROR;
  75.     }


  76.     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, wev->log, 0, "aio_write: %d", n);

  77.     wev->active = 0;
  78.     wev->ready = 1;

  79.     return n;
  80. }