redis ae.c/acMain()






  1. int aeProcessEvents(aeEventLoop *eventLoop, int flags)  
  2. {  
  3.     int processed = 0, numevents;  
  4.   
  5.     /* Nothing to do? return ASAP */  
  6.     if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0;  
  7.   
  8.     /* Note that we want call select() even if there are no 
  9.      * file events to process as long as we want to process time 
  10.      * events, in order to sleep until the next time event is ready 
  11.      * to fire. */  
  12.     if (eventLoop->maxfd != -1 ||  
  13.         ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {  
  14.         int j;  
  15.         aeTimeEvent *shortest = NULL;  
  16.         struct timeval tv, *tvp;  
  17.   
  18.         // 获取最近的时间事件  
  19.         if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT))  
  20.             shortest = aeSearchNearestTimer(eventLoop);  
  21.         if (shortest) {  
  22.             // 如果时间事件存在的话  
  23.             // 那么根据最近可执行时间事件和现在时间的时间差来决定文件事件的阻塞时间  
  24.             long now_sec, now_ms;  
  25.   
  26.             /* Calculate the time missing for the nearest 
  27.              * timer to fire. */  
  28.             // 计算距今最近的时间事件还要多久才能达到  
  29.             // 并将该时间距保存在 tv 结构中  
  30.             aeGetTime(&now_sec, &now_ms);  
  31.             tvp = &tv;  
  32.             tvp->tv_sec = shortest->when_sec - now_sec;  
  33.             if (shortest->when_ms < now_ms) {  
  34.                 tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000;  
  35.                 tvp->tv_sec --;  
  36.             } else {  
  37.                 tvp->tv_usec = (shortest->when_ms - now_ms)*1000;  
  38.             }  
  39.   
  40.             // 时间差小于 0 ,说明事件已经可以执行了,将秒和毫秒设为 0 (不阻塞)  
  41.             if (tvp->tv_sec < 0) tvp->tv_sec = 0;  
  42.             if (tvp->tv_usec < 0) tvp->tv_usec = 0;  
  43.         } else {  
  44.               
  45.             // 执行到这一步,说明没有时间事件  
  46.             // 那么根据 AE_DONT_WAIT 是否设置来决定是否阻塞,以及阻塞的时间长度  
  47.   
  48.             /* If we have to check for events but need to return 
  49.              * ASAP because of AE_DONT_WAIT we need to se the timeout 
  50.              * to zero */  
  51.             if (flags & AE_DONT_WAIT) {  
  52.                 // 设置文件事件不阻塞  
  53.                 tv.tv_sec = tv.tv_usec = 0;  
  54.                 tvp = &tv;  
  55.             } else {  
  56.                 /* Otherwise we can block */  
  57.                 // 文件事件可以阻塞直到有事件到达为止  
  58.                 tvp = NULL; /* wait forever */  
  59.             }  
  60.         }  
  61.   
  62.         // 处理文件事件,阻塞时间由 tvp 决定  
  63.         numevents = aeApiPoll(eventLoop, tvp);  
  64.         for (j = 0; j < numevents; j++) {  
  65.             // 从已就绪数组中获取事件  
  66.             aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];  
  67.   
  68.             int mask = eventLoop->fired[j].mask;  
  69.             int fd = eventLoop->fired[j].fd;  
  70.             int rfired = 0;  
  71.   
  72.             /* note the fe->mask & mask & ... code: maybe an already processed 
  73.              * event removed an element that fired and we still didn't 
  74.              * processed, so we check if the event is still valid. */  
  75.             if (fe->mask & mask & AE_READABLE) {  
  76.                 // 读事件  
  77.                 rfired = 1; // 确保读/写事件只能执行其中一个  
  78.                 fe->rfileProc(eventLoop,fd,fe->clientData,mask);  
  79.             }  
  80.             if (fe->mask & mask & AE_WRITABLE) {  
  81.                 // 写事件  
  82.                 if (!rfired || fe->wfileProc != fe->rfileProc)  
  83.                     fe->wfileProc(eventLoop,fd,fe->clientData,mask);  
  84.             }  
  85.   
  86.             processed++;  
  87.         }  
  88.     }  
  89.   
  90.     /* Check time events */  
  91.     // 执行时间事件  
  92.     if (flags & AE_TIME_EVENTS)  
  93.         processed += processTimeEvents(eventLoop);  
  94.   
  95.     return processed; /* return the number of processed file/time events */  
  96. }  

  1. /* Process time events 
  2.  * 
  3.  * 处理所有已到达的时间事件 
  4.  */  
  5. static int processTimeEvents(aeEventLoop *eventLoop) {  
  6.     int processed = 0;  
  7.     aeTimeEvent *te;  
  8.     long long maxId;  
  9.     time_t now = time(NULL);  
  10.   
  11.     /* If the system clock is moved to the future, and then set back to the 
  12.      * right value, time events may be delayed in a random way. Often this 
  13.      * means that scheduled operations will not be performed soon enough. 
  14.      * 
  15.      * Here we try to detect system clock skews, and force all the time 
  16.      * events to be processed ASAP when this happens: the idea is that 
  17.      * processing events earlier is less dangerous than delaying them 
  18.      * indefinitely, and practice suggests it is. */  
  19.     // 通过重置事件的运行时间,  
  20.     // 防止因时间穿插(skew)而造成的事件处理混乱  
  21.     if (now < eventLoop->lastTime) {  
  22.         te = eventLoop->timeEventHead;  
  23.         while(te) {  
  24.             te->when_sec = 0;  
  25.             te = te->next;  
  26.         }  
  27.     }  
  28.     // 更新最后一次处理时间事件的时间  
  29.     eventLoop->lastTime = now;  
  30.   
  31.     te = eventLoop->timeEventHead;  
  32.     maxId = eventLoop->timeEventNextId-1;  
  33.     while(te) {  
  34.         long now_sec, now_ms;  
  35.         long long id;  
  36.   
  37.         // 跳过无效事件  
  38.         if (te->id > maxId) {  
  39.             te = te->next;  
  40.             continue;  
  41.         }  
  42.           
  43.         // 获取当前时间  
  44.         aeGetTime(&now_sec, &now_ms);  
  45.   
  46.         // 如果当前时间等于或等于事件的执行时间,那么执行这个事件  
  47.         if (now_sec > te->when_sec ||  
  48.             (now_sec == te->when_sec && now_ms >= te->when_ms))  
  49.         {  
  50.             int retval;  
  51.   
  52.             id = te->id;  
  53.             retval = te->timeProc(eventLoop, id, te->clientData);  
  54.             processed++;  
  55.             /* After an event is processed our time event list may 
  56.              * no longer be the same, so we restart from head. 
  57.              * Still we make sure to don't process events registered 
  58.              * by event handlers itself in order to don't loop forever. 
  59.              * To do so we saved the max ID we want to handle. 
  60.              * 
  61.              * FUTURE OPTIMIZATIONS: 
  62.              * Note that this is NOT great algorithmically. Redis uses 
  63.              * a single time event so it's not a problem but the right 
  64.              * way to do this is to add the new elements on head, and 
  65.              * to flag deleted elements in a special way for later 
  66.              * deletion (putting references to the nodes to delete into 
  67.              * another linked list). */  
  68.   
  69.             // 记录是否有需要循环执行这个事件时间  
  70.             if (retval != AE_NOMORE) {  
  71.                 // 是的, retval 毫秒之后继续执行这个时间事件  
  72.                 aeAddMillisecondsToNow(retval,&te->when_sec,&te->when_ms);  
  73.             } else {  
  74.                 // 不,将这个事件删除  
  75.                 aeDeleteTimeEvent(eventLoop, id);  
  76.             }  
  77.   
  78.             // 因为执行事件之后,事件列表可能已经被改变了  
  79.             // 因此需要将 te 放回表头,继续开始执行事件  
  80.             te = eventLoop->timeEventHead;  
  81.         } else {  
  82.             te = te->next;  
  83.         }  
  84.     }  
  85.     return processed;  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值