事件驱动数据结构

redis 事件驱动内部有四个主要的数据结构,分别是:事件循环结构体,文件事件结构体,时间事件结构体和触发事件结构体。

// 文件事件结构体
/* File event structure */
typedef struct aeFileEvent {
int mask; /* one of AE_(READABLE|WRITABLE) */

// 回调函数指针
aeFileProc *rfileProc;
aeFileProc *wfileProc;

// clientData 参数一般是指向 redisClient 的指针
void *clientData;
} aeFileEvent;

// 时间事件结构体å
/* Time event structure */
typedef struct aeTimeEvent {
long long id; /* time event identifier. */
long when_sec; /* seconds */
long when_ms; /* milliseconds */

// 定时回调函数指针
aeTimeProc *timeProc;

// 定时事件清理函数,当删除定时事件的时候会被调用
aeEventFinalizerProc *finalizerProc;

// clientData 参数一般是指向 redisClient 的指针
void *clientData;

// 定时事件表采用链表来维护
struct aeTimeEvent *next;
} aeTimeEvent;

// 触发事件
/* A fired event */
typedef struct aeFiredEvent {
int fd;
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 */

// 记录最大的定时事件 id + 1
long long timeEventNextId;

// 用于系统时间的矫正
time_t lastTime; /* Used to detect system clock skew */

// I/O 事件表
aeFileEvent *events; /* Registered events */

// 被触发的事件
aeFiredEvent *fired; /* Fired events */

// 定时事件表
aeTimeEvent *timeEventHead;

// 事件循环结束标识
int stop;

// 对于不同的 I/O 多路复用技术,有不同的数据,详见各自实现
void *apidata; /* This is used for polling API specific data */

// 新的循环前需要执行的操作
aeBeforeSleepProc *beforesleep;
} aeEventLoop;

上面的数据结构能给我们很好的提示:事件循环结构体维护 I/O 事件表,定时事件表和触发事件表。

results matching ""

    No results matching ""