暗淡青春|Redis1.0源码阅读笔记一、总体流程( 三 )


retval = select(maxfd+1, if (retval > 0) {fe = eventLoop->fileEventHead;while(fe != NULL) {int fd = (int) fe->fd;if ((fe->maskif (fe->maskif (fe->maskif (fe->mask/** fileProce = readQueryFromClient, sendReplyToClient, sendBulkToSlave, acceptHandler*/fe->fileProc(eventLoop, fe->fd, fe->clientData, mask);processed++;/* After an event is processed our file event list* may no longer be the same, so what we do* is to clear the bit for this file descriptor and* restart again from the head. */fe = eventLoop->fileEventHead;FD_CLR(fd,// FD_CLR(fd_set *fdset);用于在文件描述符集合中删除一个文件描述符 。FD_CLR(fd,FD_CLR(fd,} else {fe = fe->next;}}}处理完socket事件后 , 就是处理时间事件 。 同处理socket类似 , 遍历eventLoop中的链表timeEventHead , 依次调用每个节点的timeProc函数 。 timeEventHead的每个节点 , 类型是struct aeTimeEvent , 由函数long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, aeTimeProc *proc, void *clientData, aeEventFinalizerProc *finalizerProc)创建 。搜索全局 , 只在函数initServer中 , 调用了该函数 , 创建aeTimeEvent:aeCreateTimeEvent(server.el, 1000, serverCron, NULL, NULL); 。
【暗淡青春|Redis1.0源码阅读笔记一、总体流程】/* Check time events */if (flagsmaxId = eventLoop->timeEventNextId-1;while(te) {long now_sec, now_ms;long long id;if (te->id > maxId) {te = te->next;continue;}aeGetTime(if (now_sec > te->when_sec ||(now_sec == te->when_secid = te->id;retval = te->timeProc(eventLoop, id, te->clientData);/* After an event is processed our time event list may* no longer be the same, so we restart from head.* Still we make sure to don't process events registered* by event handlers itself in order to don't loop forever.* To do so we saved the max ID we want to handle. */if (retval != AE_NOMORE) {aeAddMillisecondsToNow(retval,} else {aeDeleteTimeEvent(eventLoop, id);}te = eventLoop->timeEventHead;} else {te = te->next;}}}


推荐阅读