Libevent基本流程

以libevent的sample中的time_test.c为例,在windows下单步调试,分析其基本流程。

//time_test.c源码如下:
#include <sys/types.h>
#include <event2/event-config.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>
#include <winsock2.h>

struct timeval lasttime;
int event_is_persistent;

static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
	struct timeval newtime, difference;
	struct event *timeout = arg;
	double elapsed;

	evutil_gettimeofday(&newtime, NULL);
	evutil_timersub(&newtime, &lasttime, &difference);
	elapsed = difference.tv_sec +
	    (difference.tv_usec / 1.0e6);

	printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
	    (int)newtime.tv_sec, elapsed);
	lasttime = newtime;

	if (! event_is_persistent) {
		struct timeval tv;
		evutil_timerclear(&tv);
		tv.tv_sec = 2;
		event_add(timeout, &tv);//对于非永久事件,则再次将其加入到事件处理框架中,否则事件只会被处理一次
	}
}

int
main(int argc, char **argv)
{
	struct event timeout;
	struct timeval tv;
	struct event_base *base;
	int flags;

	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD(2, 2);
	(void)WSAStartup(wVersionRequested, &wsaData);

	event_is_persistent = 0;
	flags = 0;

	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
	event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);

	evutil_timerclear(&tv);
	tv.tv_sec = 2;
	event_add(&timeout, &tv);

	evutil_gettimeofday(&lasttime, NULL);

	event_base_dispatch(base);

	return (0);
}

由上述代码可知,本例是一个简单的超时事件,但超时时间到达时,调用回调函数输出打印信息,同时由于该事件在main中设置为非永久事件,因此在回调函数中,会再次调用event_add()将事件再次插入到事件处理框架中。

由于本例是一个简单的超时事件,同时通过查看event_base和event的数据结构可知,事件只会出现在小根堆或者激活队列中,也即base的struct min_heap timeheap;和struct event_list *activequeues;

在调用event_add()后,事件被加入到小根堆中,如下所示:event中的min_heap_idx记录其在小根堆中的位置



在调用event_dispatch()后的监听过程中,进入到event_base_loop()循环,然后调用timeout_process()将超时事件从小根堆中移除,并加入到激活队列中,其中event中的ev_pri记录了它的优先级,通过这个参数可以找到其所在的优先级队列



然后在循环中调用event_process_active()处理激活队列中的事件,将事件从激活队列中删除,并调用回调函数

1、首先初始化事件处理框架event_base,event_init()、event_base_new()最终都相当于调用event_base_new_with_config()来创建一个base

struct event_base *
event_init(void)
{
	struct event_base *base = event_base_new_with_config</strong>(NULL);
	return (base);
}

struct event_base *
event_base_new(void)
{
	struct event_base *base = NULL;
	struct event_config *cfg = event_config_new();
	if (cfg) {
		base = event_base_new_with_config(cfg);
		event_config_free(cfg);
	}
	return base;
}

struct event_base *
event_base_new_with_config(const struct event_config *cfg)
{
	int i;
	struct event_base *base;
	int should_check_environment;
	if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) {	//分配内存
	}
	detect_monotonic();
	gettime(base, &base->event_tv);

	min_heap_ctor(&base->timeheap);		//初始化小根堆、事件队列和一些成员变量
	TAILQ_INIT(&base->eventqueue);
	base->sig.ev_signal_pair[0] = -1;
	base->sig.ev_signal_pair[1] = -1;
	base->th_notify_fd[0] = -1;
	base->th_notify_fd[1] = -1;

	event_deferred_cb_queue_init(&base->defer_queue);	//初始化延迟队列及相应的通知函数和参数
	base->defer_queue.notify_fn = notify_base_cbq_callback;
	base->defer_queue.notify_arg = base;

	evmap_io_initmap(&base->io);
	evmap_signal_initmap(&base->sigmap);
	event_changelist_init(&base->changelist);

	base->evbase = NULL;

	should_check_environment =
	    !(cfg &&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值