[libevent]event_base_new()

struct event_base *
event_base_new(void)
{
	int i;
	struct event_base *base; //相当于一个reactor,声明后分配空间

	if ((base = calloc(1, sizeof(struct event_base))) == NULL)
		event_err(1, "%s: calloc", __func__);

	detect_monotonic(); //通过调用clock_gettime()来检测系统是否支持monotonic时钟类型
	gettime(base, &base->event_tv);//将当前时间赋值给event_tv

	
	min_heap_ctor(&base->timeheap);//初始化小根堆
	TAILQ_INIT(&base->eventqueue);//初始化链表
	base->sig.ev_signal_pair[0] = -1;//初始化socket对
	base->sig.ev_signal_pair[1] = -1;
	
	base->evbase = NULL;
	for (i = 0; eventops[i] && !base->evbase; i++) {//选择统一事件的方法,如epoll
		base->evsel = eventops[i];

		base->evbase = base->evsel->init(base);//选择完之后调用epoll_init()
	}

	if (base->evbase == NULL)
		event_errx(1, "%s: no event mechanism available", __func__);

	if (evutil_getenv("EVENT_SHOW_METHOD"))            //打印一些信息而已,没什么用
		event_msgx("libevent using: %s\n",
			   base->evsel->name);

	/* allocate a single active event queue */
	event_base_priority_init(base, 1);          //设置优先级的最高级,这里只有1个优先级

	return (base);
}

你可能感兴趣的:(libevent源码分析)