Example #1
0
File: ae.c Project: hiepnm/redislib
aeEventLoop *aeCreateEventLoop(int setsize) {
    aeEventLoop *eventLoop;
    int i;

    if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
    eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
    eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
    if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
    eventLoop->setsize = setsize;
    eventLoop->lastTime = time(NULL);
    eventLoop->timeEventHead = NULL;
    eventLoop->timeEventNextId = 0;
    eventLoop->stop = 0;
    eventLoop->maxfd = -1;
    eventLoop->beforesleep = NULL;
    if (aeApiCreate(eventLoop) == -1) goto err;
    /* Events with mask == AE_NONE are not set. So let's initialize the
     * vector with it. */
    for (i = 0; i < setsize; i++)
        eventLoop->events[i].mask = AE_NONE;
    return eventLoop;

err:
    if (eventLoop) {
        zfree(eventLoop->events);
        zfree(eventLoop->fired);
        zfree(eventLoop);
    }
    return NULL;
}
Example #2
0
aeEventLoop *aeCreateEventLoop(void) {
    aeEventLoop *eventLoop;
    int i;

    eventLoop = zmalloc(sizeof(*eventLoop));
    if (!eventLoop) return NULL;
    eventLoop->timeEventHead = NULL;
    eventLoop->timeEventNextId = 0;
    eventLoop->stop = 0;
    eventLoop->maxfd = -1;
    eventLoop->beforesleep = NULL;
    if (aeApiCreate(eventLoop) == -1) {
        zfree(eventLoop);
        return NULL;
    }
    /* Events with mask == AE_NONE are not set. So let's initialize the
     * vector with it. */
    for (i = 0; i < AE_SETSIZE; i++) {
        eventLoop->events[i].mask = AE_NONE;
        eventLoop->events[i].clientData = NULL;
        eventLoop->events[i].rfileProc = NULL;
        eventLoop->events[i].wfileProc = NULL;       
    }
    return eventLoop;
}
Example #3
0
/*
 * 创建事件处理器
 *
 * setsize 已追踪的最大文件描述符
 *
 */
aeEventLoop *aeCreateEventLoop(int setsize) {

    // 事件处理器
    aeEventLoop *eventLoop;
    // 循环计数器
    int i;

    // 创建事件处理器
    if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;

    // 初始化文件事件结构
    eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
    // 初始化已就绪事件结构
    eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);

    // 检查文件事件与已就绪事件是否为NULL
    if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;

    // 设置已追踪的最大文件描述符
    eventLoop->setsize = setsize;
    // 设置最后一次执行事件的时间
    eventLoop->lastTime = time(NULL);
    // 设置时间事件头
    eventLoop->timeEventHead = NULL;
    // 设置下个事件ID
    eventLoop->timeEventNextId = 0;
    // 标记为启动事件
    eventLoop->stop = 0;
    // 设置当前已注册的最大文件描述符
    eventLoop->maxfd = -1;
    // 设置事件处理前的sleep方法
    eventLoop->beforesleep = NULL;

    // 创建事件API
    if (aeApiCreate(eventLoop) == -1) goto err;

    /* Events with mask == AE_NONE are not set. So let's initialize the
     * vector with it. */
    // 初始化事件状态类型为未设置
    for (i = 0; i < setsize; i++)
        eventLoop->events[i].mask = AE_NONE;

    // 返回事件处理器
    return eventLoop;

// 错误处理
err:
    if (eventLoop) {
        // 释放文件事件
        zfree(eventLoop->events);
        // 释放已就绪事件
        zfree(eventLoop->fired);
        // 释放事件处理器
        zfree(eventLoop);
    }
    // 返回NULL
    return NULL;
}
Example #4
0
// 首先初始化事件模型
void thread_init(int nthreads) {
    int         i;
    pthread_mutex_init(&ibuffer_lock, NULL);
    pthread_mutex_init(&conn_lock, NULL);
    pthread_mutex_init(&leader, NULL);

    memset(&loop, 0, sizeof(loop));
    if (aeApiCreate(&loop) == -1) {
        exit(1);
    }
}
Example #5
0
aeEventLoop *aeCreateEventLoop(int setsize) {
    aeEventLoop *eventLoop;
    int i;

	/* 新建一个aeEventLoop对象 */
    if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
	
	/* 创建保存已注册IO事件的数组, 数组大小为setsize的值, 数组以文件描述符索引
	 * 判断数组元素对应的事件是否注册的依据是mask成员, 如果mask=AE_NONE, 则表示该
	 * fd(数组索引)对应的事件未注册
	 */
    eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
	
	/* 创建保存已就绪IO事件的数组, 数组大小为setsize的值 */
    eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
    if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
	
	
    eventLoop->setsize = setsize;
    eventLoop->lastTime = time(NULL);
    
	/* 定时事件链表初始为空 */
	eventLoop->timeEventHead = NULL;
	
	/* 下一个时间ID初始化为0 */
    eventLoop->timeEventNextId = 0;
	
	/* 循环处理停止标志初始化为否 */
    eventLoop->stop = 0;
	
	/* 当前最大文件描述符值初始化为-1, 表示没有注册任何IO事件 */
    eventLoop->maxfd = -1;
    eventLoop->beforesleep = NULL;
	
	/* aeApiCreate是选择的IO复用机制中定义的用于创建IO复用机制私有结构体数据 */
    if (aeApiCreate(eventLoop) == -1) goto err;
	
	/* 初始化已注册数组中所有元素的mask成员为AE_NONE, 表示无任何注册IO事件 */
    for (i = 0; i < setsize; i++)
        eventLoop->events[i].mask = AE_NONE;
		
    return eventLoop;

err:
    if (eventLoop) {
        zfree(eventLoop->events);
        zfree(eventLoop->fired);
        zfree(eventLoop);
    }
    return NULL;
}
Example #6
0
File: ae.c Project: naver/nbase-arc
aeEventLoop *aeCreateEventLoop(int setsize) {
    aeEventLoop *eventLoop;
    int i;

    if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
    eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
    eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
    if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
    eventLoop->setsize = setsize;
    eventLoop->lastTime = time(NULL);
    eventLoop->timeEventHead = NULL;
    eventLoop->timeEventNextId = 0;
    eventLoop->stop = 0;
    eventLoop->maxfd = EVENTLOOP_MAXFD_INITIAL_VALUE;
    eventLoop->beforesleep = NULL;
    if (aeApiCreate(eventLoop) == -1) goto err;
    /* Events with mask == AE_NONE are not set. So let's initialize the
     * vector with it. */
    for (i = 0; i < setsize; i++)
        eventLoop->events[i].mask = AE_NONE;
#ifdef _WIN32
    if ((eventLoop->fdiMap = zmalloc(sizeof(fd_index_map_t))) == NULL) goto err;
    eventLoop->fdiMap->map = dictCreate(&fdiDictType, NULL);
    eventLoop->fdiMap->recycle_pool = listCreate();
    if (eventLoop->fdiMap->map == NULL || eventLoop->fdiMap->recycle_pool == NULL) goto err;
    eventLoop->fdiMap->next_available = 3;	// stdin(0), stdout(1) and stderr(2)
#endif
    return eventLoop;

err:
#ifdef _WIN32
    if (eventLoop->fdiMap) {
        if (eventLoop->fdiMap->map != NULL) dictRelease(eventLoop->fdiMap->map);
        if (eventLoop->fdiMap->recycle_pool != NULL) listRelease(eventLoop->fdiMap->recycle_pool);
        zfree(eventLoop->fdiMap);
    }
#endif
    if (eventLoop) {
        zfree(eventLoop->events);
        zfree(eventLoop->fired);
        zfree(eventLoop);
    }
    return NULL;
}
/*
 * 初始化事件处理器状态
 */
aeEventLoop *aeCreateEventLoop(int setsize) {
    aeEventLoop *eventLoop;
    int i;

    // 创建事件状态结构
    if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;

    // 初始化文件事件结构和已就绪文件事件结构数组
    eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
    eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
    if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
    // 设置数组大小
    eventLoop->setsize = setsize;
    // 初始化执行最近一次执行时间
    eventLoop->lastTime = time(NULL);

    // 初始化时间事件结构
    eventLoop->timeEventHead = NULL;
    eventLoop->timeEventNextId = 0;

    eventLoop->stop = 0;
    eventLoop->maxfd = -1;
    eventLoop->beforesleep = NULL;
    if (aeApiCreate(eventLoop) == -1) goto err;

    /* Events with mask == AE_NONE are not set. So let's initialize the
     * vector with it. */
    // 初始化监听事件
    for (i = 0; i < setsize; i++)
        eventLoop->events[i].mask = AE_NONE;

    // 返回事件循环
    return eventLoop;

err:
    if (eventLoop) {
        zfree(eventLoop->events);
        zfree(eventLoop->fired);
        zfree(eventLoop);
    }
    return NULL;
}