ae事件库


aeEventLoop是一个记录记录程序事件状态的结构:

[cpp]  view plain copy
  1. /* State of an event based program */  
  2. typedef struct aeEventLoop {  
  3.     int maxfd;  
  4.     long long timeEventNextId;  
  5.     aeFileEvent events[AE_SETSIZE]; /* Registered events */  
  6.     aeFiredEvent fired[AE_SETSIZE]; /* Fired events */  
  7.     aeTimeEvent *timeEventHead;  
  8.     int stop;  
  9.     void *apidata; /* This is used for polling API specific data */  
  10.     aeBeforeSleepProc *beforesleep;  
  11. } aeEventLoop;  

该结构在aeCreateEventLoop()函数中得到初始化。

EventLoop中的事件类型包括时间事件:

[cpp]  view plain copy
  1. /* Time event structure */  
  2. typedef struct aeTimeEvent {  
  3.     long long id; /* time event identifier. */  
  4.     long when_sec; /* seconds */  
  5.     long when_ms; /* milliseconds */  
  6.     aeTimeProc *timeProc;  
  7.     aeEventFinalizerProc *finalizerProc;  
  8.     void *clientData;  
  9.     struct aeTimeEvent *next;  
  10. } aeTimeEvent;  
以及文件事件:

[cpp]  view plain copy
  1. /* File event structure */  
  2. typedef struct aeFileEvent {  
  3.     int mask; /* one of AE_(READABLE|WRITABLE) */  
  4.     aeFileProc *rfileProc;  
  5.     aeFileProc *wfileProc;  
  6.     void *clientData;  
  7. } aeFileEvent;  

分别在aeCreateTimeEvent函数和aeCreateTimeEvent函数中得到初始化。

当事件循环EventLoop结构在initServer()中创建并初始化完成之后,主函数就调用aeMain()函数开始处理事件:

[cpp]  view plain copy
  1. void aeMain(aeEventLoop *eventLoop) {  
  2.     eventLoop->stop = 0;  
  3.     while (!eventLoop->stop) {  
  4.         if (eventLoop->beforesleep != NULL)  
  5.             eventLoop->beforesleep(eventLoop);  
  6.         aeProcessEvents(eventLoop, AE_ALL_EVENTS);  
  7.     }  
  8. }  

我们看到,只要eventLoop结构中停止处理的标识stop不为1,事件循环就不断地调用aeProcessEvents来处理事件。

[cpp]  view plain copy
  1. int aeProcessEvents(aeEventLoop *eventLoop, int flags);  

该函数处理每个待处理的时间事件和文件事件,传入的flag决定处理的方式:

[plain]  view plain copy
  1. * If flags is 0, the function does nothing and returns.  
  2. * if flags has AE_ALL_EVENTS set, all the kind of events are processed.  
  3. * if flags has AE_FILE_EVENTS set, file events are processed.  
  4. * if flags has AE_TIME_EVENTS set, time events are processed.  
  5. * if flags has AE_DONT_WAIT set the function returns ASAP until all   
  6. * the events that's possible to process without to wait are processed.  

函数返回完成处理的事件的数量。

函数首先调用aeSearchNearestTimer函数选择最近要fire的计时器,然后更新tvp,这一步不展开。

在函数中,调用aeApiPoll来监控事件,该函数封装了select、kqueue、epoll三种机制:

[cpp]  view plain copy
  1. numevents = aeApiPoll(eventLoop, tvp);  

在select模型下,实现为:

[cpp]  view plain copy
  1. retval = select(eventLoop->maxfd+1,  
  2.                 &state->_rfds,&state->_wfds,NULL,tvp);  

在kqueue模型下,实现为

[cpp]  view plain copy
  1. if (tvp != NULL) {  
  2.         struct timespec timeout;  
  3.         timeout.tv_sec = tvp->tv_sec;  
  4.         timeout.tv_nsec = tvp->tv_usec * 1000;  
  5.         retval = kevent(state->kqfd, NULL, 0, state->events, AE_SETSIZE, &timeout);  
  6.     } else {  
  7.         retval = kevent(state->kqfd, NULL, 0, state->events, AE_SETSIZE, NULL);  
  8.     }      

在epoll模型下,实现为:

[cpp]  view plain copy
  1. retval = epoll_wait(state->epfd,state->events,AE_SETSIZE,  
  2.             tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);  

关于这三种处理机制的分析比较可以参考:http://www.kegel.com/c10k.html

完成事件的监控调度之后,把这些事件加入eventLoop的fired记录中,然后返回numevents:

[cpp]  view plain copy
  1. if (retval > 0) {  
  2.         int j;  
  3.   
  4.         numevents = retval;  
  5.         for (j = 0; j < numevents; j++) {  
  6.             int mask = 0;  
  7.             struct epoll_event *e = state->events+j;  
  8.   
  9.             if (e->events & EPOLLIN) mask |= AE_READABLE;  
  10.             if (e->events & EPOLLOUT) mask |= AE_WRITABLE;  
  11.             eventLoop->fired[j].fd = e->data.fd;  
  12.             eventLoop->fired[j].mask = mask;  
  13.         }  
  14.     }  
  15.     return numevents;  

aeFiredEvent定义为:

[cpp]  view plain copy
  1. /* A fired event */  
  2. typedef struct aeFiredEvent {  
  3.     int fd;  
  4.     int mask;  
  5. } aeFiredEvent;  

回到aeProcessEvents函数,处理标记好的firedEvents:

[cpp]  view plain copy
  1. for (j = 0; j < numevents; j++) {  
  2.             aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];  
  3.             int mask = eventLoop->fired[j].mask;  
  4.             int fd = eventLoop->fired[j].fd;  
  5.             int rfired = 0;  
  6.   
  7. /* note the fe->mask & mask & ... code: maybe an already processed 
  8.              * event removed an element that fired and we still didn't 
  9.              * processed, so we check if the event is still valid. */  
  10.             if (fe->mask & mask & AE_READABLE) {  
  11.                 rfired = 1;  
  12.                 fe->rfileProc(eventLoop,fd,fe->clientData,mask);  
  13.             }  
  14.             if (fe->mask & mask & AE_WRITABLE) {  
  15.                 if (!rfired || fe->wfileProc != fe->rfileProc)  
  16.                     fe->wfileProc(eventLoop,fd,fe->clientData,mask);  
  17.             }  
  18.             processed++;  
  19.         }  
aeFileEvent中定义了两种文件处理的回调函数:rfileProc和wfileProc,分别对应读写。

处理完文件事件之后,才处理时间事件:

[cpp]  view plain copy
  1. /* Check time events */  
  2.    if (flags & AE_TIME_EVENTS)  
  3.        processed += processTimeEvents(eventLoop);  
其实现如下:

[cpp]  view plain copy
  1. /* Process time events */  
  2. static int processTimeEvents(aeEventLoop *eventLoop) {  
  3.     int processed = 0;  
  4.     aeTimeEvent *te;  
  5.     long long maxId;  
  6.   
  7.     te = eventLoop->timeEventHead;  
  8.     maxId = eventLoop->timeEventNextId-1;  
  9.     while(te) {  
  10.         long now_sec, now_ms;  
  11.         long long id;  
  12.   
  13.         if (te->id > maxId) {  
  14.             te = te->next;  
  15.             continue;  
  16.         }  
  17.         aeGetTime(&now_sec, &now_ms);  
  18.         if (now_sec > te->when_sec ||  
  19.             (now_sec == te->when_sec && now_ms >= te->when_ms))  
  20.         {  
  21.             int retval;  
  22.   
  23.             id = te->id;  
  24.             retval = te->timeProc(eventLoop, id, te->clientData);  
  25.             processed++;  
  26.             /* After an event is processed our time event list may 
  27.              * no longer be the same, so we restart from head. 
  28.              * Still we make sure to don't process events registered 
  29.              * by event handlers itself in order to don't loop forever. 
  30.              * To do so we saved the max ID we want to handle. 
  31.              * 
  32.              * FUTURE OPTIMIZATIONS: 
  33.              * Note that this is NOT great algorithmically. Redis uses 
  34.              * a single time event so it's not a problem but the right 
  35.              * way to do this is to add the new elements on head, and 
  36.              * to flag deleted elements in a special way for later 
  37.              * deletion (putting references to the nodes to delete into 
  38.              * another linked list). */  
  39.             if (retval != AE_NOMORE) {  
  40.                 aeAddMillisecondsToNow(retval,&te->when_sec,&te->when_ms);  
  41.             } else {  
  42.                 aeDeleteTimeEvent(eventLoop, id);  
  43.             }  
  44.             te = eventLoop->timeEventHead;  
  45.         } else {  
  46.             te = te->next;  
  47.         }  
  48.     }  
  49.     return processed;  
  50. }  
我们看到,也是通过调用aeTimeProc *类型的回调函数来处理事件。在遍历时间事件链表的过程中,对每个事件重复:如果事件已经处理完,retval的值标记为AE_NOMORE,则调用aeDeleteTimeEvent删除时间事件,否则,调用adAddMillisecondsToNow更新计时器。

最后返回已处理的事件数量:

[cpp]  view plain copy
  1. return processed; /* return the number of processed file/time events */  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值