January 14th Tuesday 2010
来源:互联网 发布:东莞网络推广招聘 编辑:程序博客网 时间:2024/05/18 01:30
Nginx (九) 事件
结构
struct ngx_event_s {
void *data;
unsigned write:1;
unsigned accept:1;
/* used to detect the stale events in kqueue, rtsig, and epoll */
unsigned instance:1;
/*
* the event was passed or would be passed to a kernel;
* in aio mode - operation was posted.
*/
unsigned active:1;
unsigned disabled:1;
/* the ready event; in aio mode 0 means that no operation can be posted */
unsigned ready:1;
unsigned oneshot:1;
/* aio operation is complete */
unsigned complete:1;
unsigned eof:1;
unsigned error:1;
unsigned timedout:1;
unsigned timer_set:1;
unsigned delayed:1;
unsigned read_discarded:1;
unsigned unexpected_eof:1;
unsigned deferred_accept:1;
/* the pending eof reported by kqueue or in aio chain operation */
unsigned pending_eof:1;
#if !(NGX_THREADS)
unsigned posted_ready:1;
#endif
#if (NGX_WIN32)
/* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was succesfull */
unsigned accept_context_updated:1;
#endif
#if (NGX_HAVE_KQUEUE)
unsigned kq_vnode:1;
/* the pending errno reported by kqueue */
int kq_errno;
#endif
/*
* kqueue only:
* accept: number of sockets that wait to be accepted
* read: bytes to read when event is ready
* or lowat when event is set with NGX_LOWAT_EVENT flag
* write: available space in buffer when event is ready
* or lowat when event is set with NGX_LOWAT_EVENT flag
*
* iocp: TODO
*
* otherwise:
* accept: 1 if accept many, 0 otherwise
*/
#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
int available;
#else
unsigned available:1;
#endif
ngx_event_handler_pt handler;
#if (NGX_HAVE_AIO)
#if (NGX_HAVE_IOCP)
ngx_event_ovlp_t ovlp;
#else
struct aiocb aiocb;
#endif
#endif
ngx_uint_t index;
ngx_log_t *log;
ngx_rbtree_node_t timer;
unsigned closed:1;
/* to test on worker exit */
unsigned channel:1;
unsigned resolver:1;
#if (NGX_THREADS)
unsigned locked:1;
unsigned posted_ready:1;
unsigned posted_timedout:1;
unsigned posted_eof:1;
#if (NGX_HAVE_KQUEUE)
/* the pending errno reported by kqueue */
int posted_errno;
#endif
#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
int posted_available;
#else
unsigned posted_available:1;
#endif
ngx_atomic_t *lock;
ngx_atomic_t *own_lock;
#endif
/* the links of the posted queue */
ngx_event_t *next;
ngx_event_t **prev;
#if 0
/* the threads support */
/*
* the event thread context, we store it here
* if $(CC) does not understand __thread declaration
* and pthread_getspecific() is too costly
*/
void *thr_ctx;
#if (NGX_EVENT_T_PADDING)
/* event should not cross cache line in SMP */
uint32_t padding[NGX_EVENT_T_PADDING];
#endif
#endif
};
typedef struct {
ngx_uint_t lock;
ngx_event_t *events;
ngx_event_t *last;
} ngx_event_mutex_t;
typedef struct {
ngx_int_t (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*add_conn)(ngx_connection_t *c);
ngx_int_t (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);
ngx_int_t (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
ngx_int_t (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
ngx_uint_t flags);
ngx_int_t (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
void (*done)(ngx_cycle_t *cycle);
} ngx_event_actions_t;
相关函数
具体以epoll事件模块为例。其中ngx_epoll_process_events()函数在前面工作进程的逻辑分析部份已经有很清楚的伪代码说明。
ngx_epoll_init()函数。这个函数中的NGX_HAVE_FILE_AIO宏有效部份中包括起来的那部份代码,暂死放在一边;因为他对我们理解epoll事件模块没有影响,过份关心反而不利于理解。
剩下的代码就好理解了,首先取得ngx_conf_t对象。根据cycle中的最大的connection对象数的一半创建epoll用于监听的epoll socket。
接着根据ngx_conf_t对象中的events(整数)创建epoll监听用的event_list。
后面三行很容易理解。
nevents = epcf->events;
ngx_io = ngx_os_io;
ngx_event_actions = ngx_epoll_module_ctx.actions;
最后设定ngx_event_flags标志。
#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;
返回 NGX_OK。
ngx_epoll_done()函数。同样也不关心NGX_HAVE_FILE_AIO宏包括的代码。
于是这个函数就只是关闭epoll监听的socket;释放event_list。
ngx_epoll_add_event()函数。
1. 从待添加的ev对象(ngx_event_t)中取出相关c对象(ngx_connection_t);
2.如果要添加NGX_READ_EVENT类型的ev对象;
那么看c对象中的write事件对象是否是active状态;
active状态下用EPOLL_CTL_MOD方式调用epoll_ctl()加到epoll监听的事件中;
如果是NGX_WRITE_EVENT类型的ev对象;c对象中read事件是active状态;
通样用EPOLL_CTL_MOD方式调用epoll_ctl()加到epoll监听的事件中;
不是上述情况,那就仅仅用EPOLL_CTL_ADD方式调用epoll_ctl()加到epoll监听的事件中;
(nginx中一个ngx_connection_t对象含有write和read两个事件对象,分别代表了读、写事件。同时ngx_event_t对象中有个data成员指向与之相关的ngx_connection_t对象。
所以在这个函数中实际上是对一个ngx_connection_t对象操作,待添加的ev对象只可能是某个ngx_connection_t对象中的一个。如果说这个ngx_connection_t对象中write和read事件都没有激活,只需注册目前这个就ok了;另一种可能就是如添加一个read类型事件时,原来write事件要保留下来,因此用EPOLL_CTL_MOD方式调用epoll_ctl()加到epoll监听的事件中。)
1. 最后将在epoll监听事件后,将ev对象激活。
ngx_epoll_del_event()函数。
1. 先检查ev对象相关的文件描术符是否已经关了?如果是ev对象的activty设为0即可返回;(因为文件描术符关了,epoll自动从监听队列中删除。所以没有必要明显删除。)
2. 后面逻辑与ngx_epoll_add_event()函数的差不多,只不过是删除操作。
ngx_epoll_add_connection()函数。
用EPOLL_CTL_ADD方式调用epoll_ctl()注册某个ngx_connection_t对象中的read与write事件对象,并激活之。
ngx_epoll_del_connection()函数。
同样先检查相关的文件描术符是否已经关了?如果是ngx_connection_t对象中read与write事件的activty设为0即可返回;(因为文件描术符关了,epoll自动从监听队列中删除。所以没有必要明显删除。)
用EPOLL_CTL_DEL方式调用epoll_ctl()注销某个ngx_connection_t对象中的read与write事件对象,并设active为0。
ngx_enable_accept_events()函数。
将cycle中监听的ngx_listening_t对象中添加监听事件,主要对accept事件的发生。
如果ngx_event_flags标志中表明了使用了实时信号(即RTSIG),调用ngx_add_conn()为一个ngx_listening_t对象中ngx_connection_t对象添加读、写事件;否则仅对这个ngx_connection_t对象添加读取事件。(客户端一有请求,服务器端accept事件发生时当然也是可读了。)
- January 14th Tuesday 2010
- Tuesday 5th January 2010
- January 7th Tuesday 2010
- January 12th Tuesday 2010
- January 19th Tuesday 2010
- January 26th Tuesday 2010
- January 28th Tuesday 2010
- 2008 January 8th Tuesday (一月 八日 火曜日)
- 2008 January 15th Tuesday (一月 十五日 火曜日)
- 2008 January 22th Tuesday (一月 二十二日 火曜日)
- January 6th Tuesday 2009 (一月 六日 火曜日)
- January 13th Tuesday 2009 (一月 十三日 火曜日)
- February 4th Tuesday 2010
- February 23th Tuesday 2010
- March 9th Tuesday 2010
- March 23th Tuesday 2010
- March 25th Tuesday 2010
- March 30th Tuesday 2010
- 唐骏等谈谷歌考虑退出中国:国外互联网无一成功
- 关键词选择与维护教程
- [EVC] char與WCHAR與的互轉
- Oracle体系结构:内存结构和进程结构
- vlan相关
- January 14th Tuesday 2010
- Read out Exif via Python
- 解决Struts2中forward问题!
- Speex Codec VS recent CELP Codecs
- 如何拉伸图片至整个屏幕
- C#调用DLL (strmagic)
- 给硕士生的经验
- 第二章:我的大学(下篇)
- CRichEditCtrl 获取单行字符串长度