设为首页 加入收藏

TOP

redis启动过程源码解析(四)
2019-09-17 18:41:58 】 浏览:81
Tags:redis 启动 过程 源码 解析
int mask; } aeFiredEvent; /* State of an event based program */ typedef struct aeEventLoop { //最大文件描述符 int maxfd; /* highest file descriptor currently registered */ //文件描述符的最大监听数 int setsize; /* max number of file descriptors tracked */ //生成时间事件的唯一标识 long long timeEventNextId; //检测系统时钟偏差 time_t lastTime; /* Used to detect system clock skew */ //双向链表,注册文件事件 aeFileEvent *events; /* Registered events */ //已触发的文件事件 aeFiredEvent *fired; /* Fired events */ //注册的时间事件 aeTimeEvent *timeEventHead; int stop; //处理底层特定API的数据,对于epoll来说,该结构体包含了epoll fd和epoll_event void *apidata; /* This is used for polling API specific data */ aeBeforeSleepProc *beforesleep; aeBeforeSleepProc *aftersleep; } aeEventLoop;

  函数aeCreateTimeEvent(ae.c)用于往server中的变量el时间事件队列添加元素,源码如下:

 1 long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
 2         aeTimeProc *proc, void *clientData,
 3         aeEventFinalizerProc *finalizerProc)
 4 {
 5     long long id = eventLoop->timeEventNextId++;
 6     aeTimeEvent *te;
 7 
 8     te = zmalloc(sizeof(*te));
 9     if (te == NULL) return AE_ERR;
10     te->id = id;
11     aeAddMillisecondsToNow(milliseconds,&te->when_sec,&te->when_ms);
12     te->timeProc = proc;
13     te->finalizerProc = finalizerProc;
14     te->clientData = clientData;
15     te->prev = NULL;
16     te->next = eventLoop->timeEventHead;
17     if (te->next)
18         te->next->prev = te;
19     eventLoop->timeEventHead = te;
20     return id;
21 }

  aeCreateFileEvent(ae.c)函数用于server.el增加文件事件队列添加元素,源码如下:

static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
    aeApiState *state = eventLoop->apidata;

    if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
    if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
    return 0;
}

int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
        aeFileProc *proc, void *clientData)
{
    if (fd >= eventLoop->setsize) {
        errno = ERANGE;
        return AE_ERR;
    }
    aeFileEvent *fe = &eventLoop->events[fd];

    if (aeApiAddEvent(eventLoop, fd, mask) == -1)
        return AE_ERR;
    fe->mask |= mask;
    if (mask & AE_READABLE) fe->rfileProc = proc;
    if (mask & AE_WRITABLE) fe->wfileProc = proc;
    fe->clientData = clientData;
    if (fd > eventLoop->maxfd)
        eventLoop->maxfd = fd;
    return AE_OK;
}

  执行完initServer及aeSetBeforeSleepProc和aeSetAfterSleepProc函数设置好全局变量server后,进程调用aeMain函数进入循环,开始接收客户端连接和命令交互。aeMain(ae.c)函数源码如下:

 1 //处理客户端命令的主函数,由main函数调用,aeEventLoop定义在(ae.h),beforesleep和aftersleep均在服务器启动初始化时设定
 2 void aeMain(aeEventLoop *eventLoop) {
 3     eventLoop->stop = 0;
 4     while (!eventLoop->stop) {
 5         if (eventLoop->beforesleep != NULL)
 6             eventLoop->beforesleep(eventLoop);
 7         //处理文件时间及时间事件
 8         aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP);
 9     }
10 }

 

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CentOS7.3安装mysql数据库 下一篇DBA_OBJECTS

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目