-
Notifications
You must be signed in to change notification settings - Fork 1
/
ae.h
141 lines (126 loc) · 7.14 KB
/
ae.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* A simple event-driven programming library. Originally I wrote this code
* for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
* it in form of a library for easy reuse.
*
* Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __AE_H__
#define __AE_H__
#include <time.h>
#define AE_OK 0
#define AE_ERR -1
// 与aeFiredEvent.mask相关
#define AE_NONE 0 /* No events registered. */
#define AE_READABLE 1 /* Fire when descriptor is readable. */
#define AE_WRITABLE 2 /* Fire when descriptor is writable. */
#define AE_BARRIER 4 /* With WRITABLE, never fire the event if the
READABLE event already fired in the same event
loop iteration. Useful when you want to persist
things to disk before sending replies, and want
to do that in a group fashion. */
// 与aeEventLoop.flags相关
#define AE_FILE_EVENTS 1
#define AE_TIME_EVENTS 2
#define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
#define AE_DONT_WAIT 4
#define AE_CALL_AFTER_SLEEP 8
// 与时间事件有关
#define AE_NOMORE -1 // timeProc函数的返回值如果是该值,说明该时间事件还需要重复执行,具体参见processTimeEvents
#define AE_DELETED_EVENT_ID -1 /* 该ID标识一个已经删除的时间事件节点 */
/* Macros */
#define AE_NOTUSED(V) ((void) V)
struct aeEventLoop;
/* Types and data structures */
typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask); // 文件事件处理函数
typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData); // 时间事件处理函数
typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
/* redis的事件分为文本事件和时间事件,对应aeFileEvent和aeTimeEvent,对应还有四个封装ae_epoll.c,ae_evport.c,ae_kqueue.c和ae_select.c。
时间事件在aeProcessEvents中有两种用途:用于设置aeApiPoll的超时时间,对epoll来说,设置了每次执行epoll_wait的超时时间;使用时间事件处理函数处理时间事件
数据结构参见https://blog.csdn.net/larry_zeng1/article/details/78835006
*/
/* File event structure */
typedef struct aeFileEvent {
int mask; /* one of AE_(READABLE|WRITABLE|BARRIER),此处的mask主要用于注册读写处理函数,而aeFiredEvent中的mask用于反映底层的读写事件*/
aeFileProc *rfileProc; /*当mask为READABLE时注册的文件处理函数*/
aeFileProc *wfileProc; /*当mask为WRITABLE时注册的文件处理函数*/
void *clientData;
} aeFileEvent;
/* Time event structure */
typedef struct aeTimeEvent {
long long id; /* 时间事件的ID,全局唯一,每增加一个等同于aeEventLoop.timeEventNextId++ */
long when_sec; /* 秒,在时间截止时触发timeProc */
long when_ms; /* 毫秒,在时间截止时触发timeProc */
aeTimeProc *timeProc; /* 时间事件处理函数 */
aeEventFinalizerProc *finalizerProc; /* 用于清理时间事件链表上的无效节点,即标记为AE_DELETED_EVENT_ID的节点 */
void *clientData;
struct aeTimeEvent *prev; /* 时间事件链表的前节点 */
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; /* 当前注册的文件描述符的最大值,与文件事件有关 */
int setsize; /* 能够跟踪的文件描述符的最大数目,该值限定了跟踪的文件描述符的上限。events和fired的长度不能大于该值 */
long long timeEventNextId;
time_t lastTime; /* 用于校验系统时间的准确性 */
aeFileEvent *events; /* 注册的文件事件,注意它与ae_epoll.c中的aeApiState.events的不同。前者主要用于注册文件描述符的处理函数,后者反映epoll句柄上注册的所有文件描述符的事件。数目上不一定相等 */
aeFiredEvent *fired; /* 触发的文件事件,触发后会调用aeFileEvent的rfileProc或wfileProc处理 */
aeTimeEvent *timeEventHead; /* 时间事件链表 */
int stop;
void *apidata; /* This is used for polling API specific data */
aeBeforeSleepProc *beforesleep; /* 执行处理事件函数aeProcessEvents前执行的回调函数 */
aeBeforeSleepProc *aftersleep; /* 执行处理事件函数aeProcessEvents后执行的回调函数 */
int flags;
} aeEventLoop;
/* Prototypes */
aeEventLoop *aeCreateEventLoop(int setsize);
void aeDeleteEventLoop(aeEventLoop *eventLoop);
void aeStop(aeEventLoop *eventLoop);
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData);
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
aeTimeProc *proc, void *clientData,
aeEventFinalizerProc *finalizerProc);
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
int aeProcessEvents(aeEventLoop *eventLoop, int flags);
int aeWait(int fd, int mask, long long milliseconds);
void aeMain(aeEventLoop *eventLoop);
char *aeGetApiName(void);
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep);
int aeGetSetSize(aeEventLoop *eventLoop);
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
void aeSetDontWait(aeEventLoop *eventLoop, int noWait);
#endif