src/event/modules/ngx_aio_module.c - nginx-1.7.10

Global variables defined

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 ngx_event_module_t  ngx_kqueue_module_ctx;


  9. static ngx_int_t ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer);
  10. static void ngx_aio_done(ngx_cycle_t *cycle);
  11. static ngx_int_t ngx_aio_add_event(ngx_event_t *ev, ngx_int_t event,
  12.     ngx_uint_t flags);
  13. static ngx_int_t ngx_aio_del_event(ngx_event_t *ev, ngx_int_t event,
  14.     ngx_uint_t flags);
  15. static ngx_int_t ngx_aio_del_connection(ngx_connection_t *c, ngx_uint_t flags);
  16. static ngx_int_t ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
  17.     ngx_uint_t flags);


  18. ngx_os_io_t ngx_os_aio = {
  19.     ngx_aio_read,
  20.     ngx_aio_read_chain,
  21.     NULL,
  22.     ngx_aio_write,
  23.     ngx_aio_write_chain,
  24.     0
  25. };


  26. static ngx_str_t      aio_name = ngx_string("aio");

  27. ngx_event_module_t  ngx_aio_module_ctx = {
  28.     &aio_name,
  29.     NULL,                                  /* create configuration */
  30.     NULL,                                  /* init configuration */

  31.     {
  32.         ngx_aio_add_event,                 /* add an event */
  33.         ngx_aio_del_event,                 /* delete an event */
  34.         NULL,                              /* enable an event */
  35.         NULL,                              /* disable an event */
  36.         NULL,                              /* add an connection */
  37.         ngx_aio_del_connection,            /* delete an connection */
  38.         NULL,                              /* process the changes */
  39.         ngx_aio_process_events,            /* process the events */
  40.         ngx_aio_init,                      /* init the events */
  41.         ngx_aio_done                       /* done the events */
  42.     }

  43. };

  44. ngx_module_t  ngx_aio_module = {
  45.     NGX_MODULE_V1,
  46.     &ngx_aio_module_ctx,                   /* module context */
  47.     NULL,                                  /* module directives */
  48.     NGX_EVENT_MODULE,                      /* module type */
  49.     NULL,                                  /* init master */
  50.     NULL,                                  /* init module */
  51.     NULL,                                  /* init process */
  52.     NULL,                                  /* init thread */
  53.     NULL,                                  /* exit thread */
  54.     NULL,                                  /* exit process */
  55.     NULL,                                  /* exit master */
  56.     NGX_MODULE_V1_PADDING
  57. };


  58. #if (NGX_HAVE_KQUEUE)

  59. static ngx_int_t
  60. ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer)
  61. {
  62.     if (ngx_kqueue_module_ctx.actions.init(cycle, timer) == NGX_ERROR) {
  63.         return NGX_ERROR;
  64.     }

  65.     ngx_io = ngx_os_aio;

  66.     ngx_event_flags = NGX_USE_AIO_EVENT;
  67.     ngx_event_actions = ngx_aio_module_ctx.actions;


  68.     return NGX_OK;
  69. }


  70. static void
  71. ngx_aio_done(ngx_cycle_t *cycle)
  72. {
  73.     ngx_kqueue_module_ctx.actions.done(cycle);
  74. }


  75. /* the event adding and deleting are needed for the listening sockets */

  76. static ngx_int_t
  77. ngx_aio_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
  78. {
  79.     return ngx_kqueue_module_ctx.actions.add(ev, event, flags);
  80. }


  81. static ngx_int_t
  82. ngx_aio_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
  83. {
  84.     return ngx_kqueue_module_ctx.actions.del(ev, event, flags);
  85. }


  86. static ngx_int_t
  87. ngx_aio_del_connection(ngx_connection_t *c, ngx_uint_t flags)
  88. {
  89.     int  rc;

  90.     if (c->read->active == 0 && c->write->active == 0) {
  91.         return NGX_OK;
  92.     }

  93.     if (flags & NGX_CLOSE_EVENT) {
  94.         return NGX_OK;
  95.     }

  96.     rc = aio_cancel(c->fd, NULL);

  97.     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_cancel: %d", rc);

  98.     if (rc == AIO_CANCELED) {
  99.         c->read->active = 0;
  100.         c->write->active = 0;
  101.         return NGX_OK;
  102.     }

  103.     if (rc == AIO_ALLDONE) {
  104.         c->read->active = 0;
  105.         c->write->active = 0;
  106.         ngx_log_error(NGX_LOG_ALERT, c->log, 0,
  107.                       "aio_cancel() returned AIO_ALLDONE");
  108.         return NGX_OK;
  109.     }

  110.     if (rc == -1) {
  111.         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
  112.                       "aio_cancel() failed");
  113.         return NGX_ERROR;
  114.     }

  115.     if (rc == AIO_NOTCANCELED) {
  116.         ngx_log_error(NGX_LOG_ALERT, c->log, 0,
  117.                       "aio_cancel() returned AIO_NOTCANCELED");

  118.         return NGX_ERROR;
  119.     }

  120.     return NGX_OK;
  121. }


  122. static ngx_int_t
  123. ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
  124. {
  125.     return ngx_kqueue_module_ctx.actions.process_events(cycle, timer, flags);
  126. }

  127. #endif /* NGX_HAVE_KQUEUE */