int ngx_cdecl
main(int argc, char *const *argv)
|
|___>ngx_single_process_cycle/ngx_master_process_cycle
|
|___>ngx_start_worker_processes
|
|___>ngx_spawn_process
|
|___>ngx_worker_process_cycle
|
|__>ngx_process_events_and_timers
|
|__>ngx_process_events
ngx_event_actions.process_events
ngx_event_actions在每个事件模块中是一个全局变量,在事件模块的初始化函数中赋值,以epoll事件为例
static ngx_event_module_t ngx_epoll_module_ctx = {
&epoll_name,
ngx_epoll_create_conf, /* create configuration */
ngx_epoll_init_conf, /* init configuration */
{
ngx_epoll_add_event, /* add an event */
ngx_epoll_del_event, /* delete an event */
ngx_epoll_add_event, /* enable an event */
ngx_epoll_del_event, /* disable an event */
ngx_epoll_add_connection, /* add an connection */
ngx_epoll_del_connection, /* delete an connection */
#if (NGX_HAVE_EVENTFD)
ngx_epoll_notify, /* trigger a notify */
#else
NULL, /* trigger a notify */
#endif
ngx_epoll_process_events, /* process the events */
ngx_epoll_init, /* init the events */
ngx_epoll_done, /* done the events */
}
};
//ngx_event_actions_t结构中的init函数,在ngx_module_t结构中的init_process函数中调用
static ngx_int_t
ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer){
//ngx_epoll_init还完成了epoll_create与epoll_ctl
…………
//这里的action,就是ngx_event_actions
ngx_event_actions = ngx_epoll_module_ctx.actions;
…………
}
//ngx_module_t结构中的init_process函数,在模块加载时调用
static ngx_int_t
ngx_event_process_init(ngx_cycle_t *cycle){
…………
for (m = 0; cycle->modules[m]; m++) {
if (cycle->modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
…………
module = cycle->modules[m]->ctx;
//ngx_epoll_init
if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
/* fatal */
exit(2);
}
break;
}
…………
}
nginx模块结构ngx_module_t中的ctx指针指向事件模块结构ngx_event_module_t,事件模块结构中包含了事件动作结构ngx_event_actions_t
ngx_module_t->ctx
|
|__>ngx_event_module_t
|
|__>actions(ngx_event_actions_t)
|
|__>process_events(ngx_event_actions.process_events)
上面调用的process_events(ngx_event_actions.process_events)就是ngx_epoll_process_event函数,该函数处理epoll_wait
static ngx_int_t
ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags){
…………
//接收事件
events = epoll_wait(ep, event_list, (int) nevents, timer);
…………
//处理事件,包含了功能模块的读和写,比如http模块的读写函数
for (i = 0; i < events; i++) {
}
}
功能模块比如http通过ngx_handle_read_event将事件(包括事件处理函数)添加到events中,
ngx_int_t
ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags){
}