diff src/event/ngx_event.c @ 91:637625a2acdb

nginx-0.0.1-2003-05-19-20:39:14 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 19 May 2003 16:39:14 +0000
parents 37530da31268
children 19cc647ecd91
line wrap: on
line diff
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -11,7 +11,7 @@
 #include <ngx_event.h>
 #include <ngx_conf_file.h>
 
-#include <ngx_select_module.h>
+extern ngx_event_module_t ngx_select_module_ctx;
 
 #if (HAVE_POLL)
 #include <ngx_poll_module.h>
@@ -22,6 +22,7 @@
 #endif
 
 #if (HAVE_KQUEUE)
+extern ngx_event_module_t ngx_kqueue_module_ctx;
 #include <ngx_kqueue_module.h>
 #endif
 
@@ -36,46 +37,26 @@
 
 
 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
+static char *ngx_event_set_type(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
+static void *ngx_event_create_conf(ngx_pool_t *pool);
+static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf);
 
 
+int                  ngx_event_flags;
+ngx_event_actions_t  ngx_event_actions;
+
 ngx_connection_t    *ngx_connections;
 ngx_event_t         *ngx_read_events, *ngx_write_events;
 
-#if !(USE_KQUEUE)
 
-ngx_event_type_e     ngx_event_type;
-
-int                  ngx_event_flags;
-
-ngx_event_actions_t  ngx_event_actions;
-
-/* ngx_event_type_e order */
-static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
-    ngx_select_init,
-#if (HAVE_POLL)
-    ngx_poll_init,
-#endif
-#if (HAVE_DEVPOLL)
-    ngx_devpoll_init,
-#endif
-#if (HAVE_KQUEUE)
-    ngx_kqueue_init,
-#endif
-#if (HAVE_AIO)
-    ngx_aio_init,
-#endif
-#if (HAVE_IOCP)
-    ngx_iocp_init
-#endif
-};
-
-#endif /* USE_KQUEUE */
+static int  ngx_event_max_module;
 
 
 static int  ngx_event_connections;
 
 
 static ngx_str_t  events_name = ngx_string("events");
+static ngx_str_t  event_name = ngx_string("event");
 
 static ngx_command_t  ngx_events_commands[] = {
 
@@ -106,67 +87,87 @@ static ngx_command_t  ngx_event_commands
      NGX_EVENT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      0,
-     addressof(ngx_event_connections),
+     offsetof(ngx_event_conf_t, connections),
      NULL},
 
-#if 0
     {ngx_string("type"),
      NGX_EVENT_CONF|NGX_CONF_TAKE1,
      ngx_event_set_type,
      0,
      0,
      NULL},
-#endif
+
+    {ngx_string("timer_queues"),
+     NGX_EVENT_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_num_slot,
+     0,
+     offsetof(ngx_event_conf_t, timer_queues),
+     NULL},
 
     {ngx_string(""), 0, NULL, 0, 0, NULL}
 };
 
 
+ngx_event_module_t  ngx_event_module_ctx = {
+    NGX_EVENT_MODULE,
+    &event_name,
+    ngx_event_create_conf,                 /* create configuration */
+    ngx_event_init_conf,                   /* init configuration */
+
+    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
+};
+
+
 ngx_module_t  ngx_event_module = {
-    NULL,                                  /* module context */
+    &ngx_event_module_ctx,                 /* module context */
     0,                                     /* module index */
-    ngx_events_commands,                   /* module directives */
+    ngx_event_commands,                    /* module directives */
     NGX_EVENT_MODULE_TYPE,                 /* module type */
     NULL                                   /* init module */
 };
 
 
 
-void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
+int ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
 {
-    int  i, fd;
+    int  m, i, fd;
 
     ngx_listen_t      *s;
     ngx_event_t       *ev;
     ngx_connection_t  *c;
+    ngx_event_conf_t  *ecf;
+    ngx_event_module_t  *module;
 
-    /* STUB */
-    int max_connections = 512;
+    ecf = ngx_event_get_conf(ngx_event_module_ctx);
+
+ngx_log_debug(log, "CONN: %d" _ ecf->connections);
+ngx_log_debug(log, "TYPE: %d" _ ecf->type);
 
-#if 0
-    ngx_event_type = NGX_POLL_EVENT_N;
-#endif
-#if 1
-    ngx_event_type = NGX_KQUEUE_EVENT_N;
-#endif
-#if 0
-    ngx_event_type = NGX_DEVPOLL_EVENT_N;
-#endif
-#if 0
-    ngx_event_type = NGX_AIO_EVENT_N;
-#endif
-#if 0
-    ngx_event_type = NGX_IOCP_EVENT_N;
-#endif
+    for (m = 0; ngx_modules[m]; m++) {
+        if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
+            continue;
+        }
 
-    if (ngx_init_events(max_connections, log) == NGX_ERROR) {
-        exit(1);
+        module = ngx_modules[m]->ctx;
+        if (module->index == ecf->type) {
+            if (module->actions.init(log) == NGX_ERROR) {
+                return NGX_ERROR;
+            }
+            break;
+        }
     }
 
-    ngx_connections = ngx_alloc(sizeof(ngx_connection_t)
-                                                       * max_connections, log);
-    ngx_read_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
-    ngx_write_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
+    ngx_test_null(ngx_connections,
+                  ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, log),
+                  NGX_ERROR);
+
+    ngx_test_null(ngx_read_events,
+                  ngx_alloc(sizeof(ngx_event_t) * ecf->connections, log),
+                  NGX_ERROR);
+
+    ngx_test_null(ngx_write_events,
+                  ngx_alloc(sizeof(ngx_event_t) * ecf->connections, log),
+                  NGX_ERROR);
 
     /* for each listening socket */
     s = (ngx_listen_t *) ls->elts;
@@ -196,7 +197,9 @@ void ngx_pre_thread(ngx_array_t *ls, ngx
         c->pool_size = s[i].pool_size;
 
         ngx_test_null(ev->log,
-                      ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; );
+                      ngx_palloc(pool, sizeof(ngx_log_t)),
+                      NGX_ERROR);
+
         ngx_memcpy(ev->log, c->log, sizeof(ngx_log_t));
         c->read = ev;
         ev->data = c;
@@ -235,6 +238,8 @@ void ngx_pre_thread(ngx_array_t *ls, ngx
 
 #endif
     }
+
+    return NGX_OK;
 }
 
 
@@ -248,35 +253,50 @@ void ngx_worker(ngx_log_t *log)
 }
 
 
-static char *ngx_events_init(ngx_pool_t *pool)
+static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
 {
-    ngx_event_connections = -1;
-    ngx_event_type = -1;
+    int                    m;
+    char                  *rv;
+    void               ***ctx;
+    ngx_conf_t            pcf;
+    ngx_event_conf_t     *ecf;
+    ngx_event_module_t   *module;
 
-    return NGX_CONF_OK;
-}
-
+    /* count the number of the event modules and set up their indices */
 
-static char *ngx_events_postconf(ngx_pool_t *pool)
-{
-    if (ngx_event_connections == -1) {
-        ngx_event_connections = 512;
+    ngx_event_max_module = 0;
+    for (m = 0; ngx_modules[m]; m++) {
+        if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
+            continue;
+        }
+
+        module = ngx_modules[m]->ctx;
+        module->index = ngx_event_max_module++;
     }
 
-    return NGX_CONF_OK;
-}
+    ngx_test_null(ctx, ngx_pcalloc(cf->pool, sizeof(void *)), NGX_CONF_ERROR);
 
+    ngx_test_null(*ctx,
+                  ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *)),
+                  NGX_CONF_ERROR);
+
+    *(void **) conf = ctx;
 
-static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
-{
-    char        *rv;
-    ngx_conf_t   pcf;
+    for (m = 0; ngx_modules[m]; m++) {
+        if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
+            continue;
+        }
 
-#if 0
-    *(ngx_events_conf_ctx_t **) conf = ctx;
-#endif
+        module = ngx_modules[m]->ctx;
+
+        if (module->create_conf) {
+            ngx_test_null((*ctx)[module->index], module->create_conf(cf->pool),
+                          NGX_CONF_ERROR);
+        }
+    }
 
     pcf = *cf;
+    cf->ctx = ctx;
     cf->module_type = NGX_EVENT_MODULE_TYPE;
     cf->cmd_type = NGX_EVENT_CONF;
     rv = ngx_conf_parse(cf, NULL);
@@ -285,5 +305,90 @@ static char *ngx_events_block(ngx_conf_t
     if (rv != NGX_CONF_OK)
         return rv;
 
+    for (m = 0; ngx_modules[m]; m++) {
+        if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
+            continue;
+        }
+
+        module = ngx_modules[m]->ctx;
+
+        if (module->init_conf) {
+            rv = module->init_conf(cf->pool, (*ctx)[module->index]);
+            if (rv != NGX_CONF_OK) {
+                return rv;
+            }
+        }
+    }
+
     return NGX_CONF_OK;
 }
+
+
+static char *ngx_event_set_type(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
+{
+    ngx_event_conf_t *ecf = (ngx_event_conf_t *) conf;
+
+    int                   m;
+    ngx_str_t            *args;
+    ngx_event_module_t   *module;
+
+    if (ecf->type != NGX_CONF_UNSET) {
+        return "duplicate event type" ;
+    }
+
+    args = cf->args->elts;
+
+    for (m = 0; ngx_modules[m]; m++) {
+        if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
+            continue;
+        }
+
+        module = ngx_modules[m]->ctx;
+        if (module->name->len == args[1].len) {
+            if (ngx_strcmp(module->name->data, args[1].data) == 0) {
+                ecf->type = module->index;
+                return NGX_CONF_OK;
+            }
+        }
+    }
+
+    return "invalid event type";
+}
+
+
+static void *ngx_event_create_conf(ngx_pool_t *pool)
+{
+    ngx_event_conf_t  *ecf;
+
+    ngx_test_null(ecf, ngx_palloc(pool, sizeof(ngx_event_conf_t)),
+                  NGX_CONF_ERROR);
+
+    ecf->connections = NGX_CONF_UNSET;
+    ecf->type = NGX_CONF_UNSET;
+
+    return ecf;
+}
+
+
+static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf)
+{
+    ngx_event_conf_t *ecf = conf;
+
+#if (HAVE_KQUEUE)
+
+    ngx_conf_init_value(ecf->connections, 1024);
+    ngx_conf_init_value(ecf->type, ngx_kqueue_module_ctx.index);
+
+#else /* HAVE_SELECT */
+
+    ngx_conf_init_value(ecf->connections,
+                        FD_SETSIZE < 1024 ? FD_SETSIZE : 1024);
+
+    ngx_conf_init_value(ecf->type, ngx_select_module_ctx.index);
+
+#endif
+
+    ngx_conf_init_value(ecf->timer_queues, 10);
+
+    return NGX_CONF_OK;
+}