static ngx_int_t ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer) { ngx_epoll_conf_t *epcf; epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module); if (ep == -1) { ep = epoll_create(cycle->connection_n / 2); if (ep == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "epoll_create() failed"); return NGX_ERROR; } #if (NGX_HAVE_EVENTFD) if (ngx_epoll_notify_init(cycle->log) != NGX_OK) { ngx_epoll_module_ctx.actions.notify = NULL; } #endif #if (NGX_HAVE_FILE_AIO) ngx_epoll_aio_init(cycle, epcf); #endif #if (NGX_HAVE_EPOLLRDHUP) ngx_epoll_test_rdhup(cycle); #endif } if (nevents < epcf->events) { if (event_list) { ngx_free(event_list); } event_list = ngx_alloc(sizeof(struct epoll_event) * epcf->events, cycle->log); if (event_list == NULL) { return NGX_ERROR; } } nevents = epcf->events; ngx_io = ngx_os_io; ngx_event_actions = ngx_epoll_module_ctx.actions; #if (NGX_HAVE_CLEAR_EVENT) ngx_event_flags = NGX_USE_CLEAR_EVENT #else ngx_event_flags = NGX_USE_LEVEL_EVENT #endif |NGX_USE_GREEDY_EVENT |NGX_USE_EPOLL_EVENT; return NGX_OK; }
/** * @param [in] cycle * @param [in] timer 定时器精度ngx_timer_resolution * @return NGX_OK|NGX_ERROR * * epoll初始化,创建epollFD */ static ngx_int_t ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer) { ngx_epoll_conf_t *epcf; epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module); if (ep == -1) { ep = epoll_create(cycle->connection_n / 2); //创建epollFD if (ep == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "epoll_create() failed"); return NGX_ERROR; } #if (NGX_HAVE_EVENTFD) //初始化通知notify_fd,将其加入epoll事件中 if (ngx_epoll_notify_init(cycle->log) != NGX_OK) { ngx_epoll_module_ctx.actions.notify = NULL; } #endif #if (NGX_HAVE_FILE_AIO) //异步事件初始化 ngx_epoll_aio_init(cycle, epcf); #endif } if (nevents < epcf->events) { //默认初始化512个 if (event_list) { ngx_free(event_list); } //初始化event_list数组。数组大小是配置项epoll_event的參数 event_list = ngx_alloc(sizeof(struct epoll_event) * epcf->events, cycle->log); if (event_list == NULL) { return NGX_ERROR; } } nevents = epcf->events; //事件个数,epoll_wait用到。 /** * \file ../../os/unix/ngx_os.h|c * 指明客户端连接事件触发时,读写I/O的方法。如消息发送与接受 * * \file ../../os/unix/ngx_linux_init.h|c * linux中等同于变量ngx_linux_io值 */ ngx_io = ngx_os_io; /** * ngx_event_actions是个全局的ngx_event_actions_t结构体 * 用于存储不同事件模块的10个函数接口。(添加|删除|...事件、接受|删除|处理..新连接) */ ngx_event_actions = ngx_epoll_module_ctx.actions; //优先使用边缘触发模式 #if (NGX_HAVE_CLEAR_EVENT) ngx_event_flags = NGX_USE_CLEAR_EVENT //使用epoll的边缘触发模式 ET #else ngx_event_flags = NGX_USE_LEVEL_EVENT //使用epoll的水平触发模式 LT #endif |NGX_USE_GREEDY_EVENT |NGX_USE_EPOLL_EVENT; return NGX_OK; }