ngx_event.c

来自「Nginx是一个高性能的HTTP和反向代理服务器」· C语言 代码 · 共 1,266 行 · 第 1/3 页

C
1,266
字号
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_event.h>#define DEFAULT_CONNECTIONS  512extern ngx_module_t ngx_kqueue_module;extern ngx_module_t ngx_eventport_module;extern ngx_module_t ngx_devpoll_module;extern ngx_module_t ngx_epoll_module;extern ngx_module_t ngx_rtsig_module;extern ngx_module_t ngx_select_module;static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle);static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle);static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,    void *conf);static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,    void *conf);static void *ngx_event_create_conf(ngx_cycle_t *cycle);static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);static ngx_uint_t     ngx_timer_resolution;sig_atomic_t          ngx_event_timer_alarm;static ngx_uint_t     ngx_event_max_module;ngx_uint_t            ngx_event_flags;ngx_event_actions_t   ngx_event_actions;ngx_atomic_t          connection_counter = 1;ngx_atomic_t         *ngx_connection_counter = &connection_counter;ngx_atomic_t         *ngx_accept_mutex_ptr;ngx_shmtx_t           ngx_accept_mutex;ngx_uint_t            ngx_use_accept_mutex;ngx_uint_t            ngx_accept_events;ngx_uint_t            ngx_accept_mutex_held;ngx_msec_t            ngx_accept_mutex_delay;ngx_int_t             ngx_accept_disabled;ngx_file_t            ngx_accept_mutex_lock_file;#if (NGX_STAT_STUB)ngx_atomic_t   ngx_stat_accepted0;ngx_atomic_t  *ngx_stat_accepted = &ngx_stat_accepted0;ngx_atomic_t   ngx_stat_handled0;ngx_atomic_t  *ngx_stat_handled = &ngx_stat_handled0;ngx_atomic_t   ngx_stat_requests0;ngx_atomic_t  *ngx_stat_requests = &ngx_stat_requests0;ngx_atomic_t   ngx_stat_active0;ngx_atomic_t  *ngx_stat_active = &ngx_stat_active0;ngx_atomic_t   ngx_stat_reading0;ngx_atomic_t  *ngx_stat_reading = &ngx_stat_reading0;ngx_atomic_t   ngx_stat_writing0;ngx_atomic_t  *ngx_stat_writing = &ngx_stat_writing0;#endifstatic ngx_command_t  ngx_events_commands[] = {    { ngx_string("events"),      NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,      ngx_events_block,      0,      0,      NULL },      ngx_null_command};static ngx_core_module_t  ngx_events_module_ctx = {    ngx_string("events"),    NULL,    NULL};ngx_module_t  ngx_events_module = {    NGX_MODULE_V1,    &ngx_events_module_ctx,                /* module context */    ngx_events_commands,                   /* module directives */    NGX_CORE_MODULE,                       /* module type */    NULL,                                  /* init master */    NULL,                                  /* init module */    NULL,                                  /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};static ngx_str_t  event_core_name = ngx_string("event_core");static ngx_command_t  ngx_event_core_commands[] = {    { ngx_string("worker_connections"),      NGX_EVENT_CONF|NGX_CONF_TAKE1,      ngx_event_connections,      0,      0,      NULL },    { ngx_string("connections"),      NGX_EVENT_CONF|NGX_CONF_TAKE1,      ngx_event_connections,      0,      0,      NULL },    { ngx_string("use"),      NGX_EVENT_CONF|NGX_CONF_TAKE1,      ngx_event_use,      0,      0,      NULL },    { ngx_string("multi_accept"),      NGX_EVENT_CONF|NGX_CONF_FLAG,      ngx_conf_set_flag_slot,      0,      offsetof(ngx_event_conf_t, multi_accept),      NULL },    { ngx_string("accept_mutex"),      NGX_EVENT_CONF|NGX_CONF_FLAG,      ngx_conf_set_flag_slot,      0,      offsetof(ngx_event_conf_t, accept_mutex),      NULL },    { ngx_string("accept_mutex_delay"),      NGX_EVENT_CONF|NGX_CONF_TAKE1,      ngx_conf_set_msec_slot,      0,      offsetof(ngx_event_conf_t, accept_mutex_delay),      NULL },    { ngx_string("debug_connection"),      NGX_EVENT_CONF|NGX_CONF_TAKE1,      ngx_event_debug_connection,      0,      0,      NULL },      ngx_null_command};ngx_event_module_t  ngx_event_core_module_ctx = {    &event_core_name,    ngx_event_create_conf,                 /* create configuration */    ngx_event_init_conf,                   /* init configuration */    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }};ngx_module_t  ngx_event_core_module = {    NGX_MODULE_V1,    &ngx_event_core_module_ctx,            /* module context */    ngx_event_core_commands,               /* module directives */    NGX_EVENT_MODULE,                      /* module type */    NULL,                                  /* init master */    ngx_event_module_init,                 /* init module */    ngx_event_process_init,                /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};voidngx_process_events_and_timers(ngx_cycle_t *cycle){    ngx_uint_t  flags;    ngx_msec_t  timer, delta;    if (ngx_timer_resolution) {        timer = NGX_TIMER_INFINITE;        flags = 0;    } else {        timer = ngx_event_find_timer();        flags = NGX_UPDATE_TIME;#if (NGX_THREADS)        if (timer == NGX_TIMER_INFINITE || timer > 500) {            timer = 500;        }#endif    }    if (ngx_use_accept_mutex) {        if (ngx_accept_disabled > 0) {            ngx_accept_disabled--;        } else {            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {                return;            }            if (ngx_accept_mutex_held) {                flags |= NGX_POST_EVENTS;            } else {                if (timer == NGX_TIMER_INFINITE                    || timer > ngx_accept_mutex_delay)                {                    timer = ngx_accept_mutex_delay;                }            }        }    }    delta = ngx_current_msec;    (void) ngx_process_events(cycle, timer, flags);    delta = ngx_current_msec - delta;    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,                   "timer delta: %M", delta);    if (ngx_posted_accept_events) {        ngx_event_process_posted(cycle, &ngx_posted_accept_events);    }    if (ngx_accept_mutex_held) {        ngx_shmtx_unlock(&ngx_accept_mutex);    }    if (delta) {        ngx_event_expire_timers();    }    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,                   "posted events %p", ngx_posted_events);    if (ngx_posted_events) {        if (ngx_threaded) {            ngx_wakeup_worker_thread(cycle);        } else {            ngx_event_process_posted(cycle, &ngx_posted_events);        }    }}ngx_int_tngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags){    if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {        /* kqueue, epoll */        if (!rev->active && !rev->ready) {            if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT)                == NGX_ERROR)            {                return NGX_ERROR;            }        }        return NGX_OK;    } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {        /* select, poll, /dev/poll */        if (!rev->active && !rev->ready) {            if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT)                == NGX_ERROR)            {                return NGX_ERROR;            }            return NGX_OK;        }        if (rev->active && (rev->ready || (flags & NGX_CLOSE_EVENT))) {            if (ngx_del_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT | flags)                == NGX_ERROR)            {                return NGX_ERROR;            }            return NGX_OK;        }    } else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {        /* event ports */        if (!rev->active && !rev->ready) {            if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {                return NGX_ERROR;            }            return NGX_OK;        }        if (rev->oneshot && !rev->ready) {            if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {                return NGX_ERROR;            }            return NGX_OK;        }    }    /* aio, iocp, rtsig */    return NGX_OK;}ngx_int_tngx_handle_write_event(ngx_event_t *wev, size_t lowat){    ngx_connection_t  *c;    if (lowat) {        c = wev->data;        if (ngx_send_lowat(c, lowat) == NGX_ERROR) {            return NGX_ERROR;        }    }    if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {        /* kqueue, epoll */        if (!wev->active && !wev->ready) {            if (ngx_add_event(wev, NGX_WRITE_EVENT,                              NGX_CLEAR_EVENT | (lowat ? NGX_LOWAT_EVENT : 0))                == NGX_ERROR)            {                return NGX_ERROR;            }        }        return NGX_OK;    } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {        /* select, poll, /dev/poll */        if (!wev->active && !wev->ready) {            if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)                == NGX_ERROR)            {                return NGX_ERROR;            }            return NGX_OK;        }        if (wev->active && wev->ready) {            if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)                == NGX_ERROR)            {                return NGX_ERROR;            }            return NGX_OK;        }    } else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {        /* event ports */        if (!wev->active && !wev->ready) {            if (ngx_add_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {                return NGX_ERROR;            }            return NGX_OK;        }        if (wev->oneshot && wev->ready) {            if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {                return NGX_ERROR;            }            return NGX_OK;        }    }    /* aio, iocp, rtsig */    return NGX_OK;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?