redis是单线程,要做到快速呢,就需要用到I/O多路复用技术了,今天闲来讲讲最为普遍的select
-
#include <sys/select.h>
-
int select(int maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *errorfds, const struct timeval *timeout);
其中各个参数的含义如下:
-
maxfdp1: 待测试的文件描述字的个数,其值是待测试的最大描述符加1(由于描述符是从0开始索引)
-
readfds: 指向对可读感兴趣的描述符集合的指针
-
writefds: 指向对可写感兴趣的描述符集合的指针
-
errorfds: 指向对处于异常条件感兴趣的描述符集合的指针
-
timeout: 指定愿意等待的时间长度。若为NULL,则堵塞;如果其结构体内元素均为0,则直接非堵塞;如果以上两种均不满足,则等待固定时间后返回
其中fd_set可以理解为一个描述符集合,一般情况是用如下几个宏来对其进行操作
-
FD_ZERO(fd_set *fdset); /* clear all bits in fdset */
-
FD_SET(int fd, fd_set *fdset); /* turn on the bit for fd in fd_set */
-
FD_CLR(int fd, fd_set *fdset); /* turn off the bit for fd in fd_set */
-
int FD_ISSET(int fd, fd_set *fdset); /* is the bit for fd on in fdset? */
函数的返回值由三种情况
-
-1:出错
-
0:没有描述符准备好
-
正数:3个描述符集合当中已经准备好的描述符的个数之和
其劣势也就显而易见了,如果maxfd比较大,那么当返回之后,我们就得,全部轮询各个集合来进行后续操作,这就比较浪费 时间了,所以,select是redis当中I/O多路复用的下下之选,
在redis当中,封装结构如下:
-
typedef struct aeApiState {
-
fd_set rfds, wfds;
-
/* We need to have a copy of the fd sets as it's not safe to reuse
-
* FD sets after select(). */
-
fd_set _rfds, _wfds;
-
} aeApiState;
其实现常规的api函数如下:
-
static int aeApiCreate(aeEventLoop *eventLoop) {
-
aeApiState *state = zmalloc(sizeof(aeApiState));
-
-
if (!state) return -1;
-
FD_ZERO(&state->rfds);
-
FD_ZERO(&state->wfds);
-
eventLoop->apidata = state;
-
return 0;
-
}
-
-
static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
-
/* Just ensure we have enough room in the fd_set type. */
-
if (setsize >= FD_SETSIZE) return -1;
-
return 0;
-
}
-
-
static void aeApiFree(aeEventLoop *eventLoop) {
-
zfree(eventLoop->apidata);
-
}
-
-
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;
-
}
-
-
static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
-
aeApiState *state = eventLoop->apidata;
-
-
if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
-
if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
-
}
-
static char *aeApiName(void) {
-
return "select";
-
}
最主要的实现函数为
-
static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
-
aeApiState *state = eventLoop->apidata;
-
int retval, j, numevents = 0;
-
-
memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
-
memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
-
-
retval = select(eventLoop->maxfd+1,
-
&state->_rfds,&state->_wfds,NULL,tvp);
-
if (retval > 0) {
-
for (j = 0; j <= eventLoop->maxfd; j++) {
-
int mask = 0;
-
aeFileEvent *fe = &eventLoop->events[j];
-
-
if (fe->mask == AE_NONE) continue;
-
if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
-
mask |= AE_READABLE;
-
if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
-
mask |= AE_WRITABLE;
-
eventLoop->fired[numevents].fd = j;
-
eventLoop->fired[numevents].mask = mask;
-
numevents++;
-
}
-
}
-
return numevents;
-
}
由以上函数我们可以知晓,在ae_select设置的时候,使用的是rfds和wfds,在调用select的时候,是将上面两个拷贝到_rfds和_wfds当中,这里是为了避免重用描述符时的安全问题~
还望各位看官多提宝贵意见
阅读(9832) | 评论(0) | 转发(0) |