libevent源码剖析

1.给信号绑定一个callback,具体是如何实现的?

应用层的代码:
struct event_base *base = event_base_new_with_config(NULL);
struct event *signal_event = event_new(base, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb, NULL);
//最顶层的绑定,给信号SIGINT绑定一个signal_cb
event_add(signal_event, NULL);

lib中的代码:
static const struct eventop evsigops = {
	"signal",
	NULL,
	evsig_add,
	evsig_del,
	NULL,
	NULL,
	0, 0, 0
};

static void *
epoll_init(struct event_base *base)
{
	evsig_init_(base);
}

int
evsig_init_(struct event_base *base)
{
	evutil_make_internal_pipe_(base->sig.ev_signal_pair);//创建pipe
	event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[0],
		EV_READ | EV_PERSIST, evsig_cb, base);
	//给信号base->sig.ev_signal指定一个fd,用于从pipe接收该信号。给该信号绑定一个callback evsig_cb。
	base->evsigsel = &evsigops;//指定信号处理的函数
}

static void
evsig_cb(evutil_socket_t fd, short what, void *arg)
{
    //当通过管道接收到该信号时,调用evmap_signal_active_
	read(fd, signals, sizeof(signals));
	for (i = 0; i < n; ++i) {
		ev_uint8_t sig = signals[i];
		if (sig < NSIG)
			ncaught[sig]++;
	}
	
	for (i = 0; i < NSIG; ++i) {
		if (ncaught[i])
			evmap_signal_active_(base, i, ncaught[i]);
	}
}


static int
evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
{
	evsig_base_fd = base->sig.ev_signal_pair[1];
	evsig_set_handler_(base, (int)evsignal, evsig_handler)
}

int
evsig_set_handler_(struct event_base *base,
    int evsignal, void (__cdecl *handler)(int))
{
	signal(evsignal, handler);//捕获信号,绑定一个callback,这个是最底层的操作
}


static evutil_socket_t evsig_base_fd = -1;
static void __cdecl
evsig_handler(int sig)
{
	write(evsig_base_fd, (char*)&msg, 1);//当收到信号之后,通过pipe通知对端
}

int
evmap_signal_add_(struct event_base *base, int sig, struct event *ev)
{
	const struct eventop *evsel = base->evsigsel;
	evsel->add(base, ev->ev_fd, 0, EV_SIGNAL, NULL);//调用evsigops的 evsig_add
}

int
event_add(struct event *ev, const struct timeval *tv)
{
	event_add_nolock_(ev, tv, 0);
}

int
event_add_nolock_(struct event *ev, const struct timeval *tv,
    int tv_is_absolute)
{
	struct event_base *base = ev->ev_base;
	evmap_io_add_(base, ev->ev_fd, ev);
	evmap_signal_add_(base, (int)ev->ev_fd, ev);
}

2.libevent是如何选择多路复用的函数的?

static const struct eventop epollops_changelist = {
	"epoll (with changelist)",
	epoll_init,
	event_changelist_add_,
	event_changelist_del_,
	epoll_dispatch,
	epoll_dealloc,
	1, /* need reinit */
	EV_FEATURE_ET|EV_FEATURE_O1| EARLY_CLOSE_IF_HAVE_RDHUP,
	EVENT_CHANGELIST_FDINFO_SIZE
};

struct event_base * 
event_base_new_with_config(const struct event_config *cfg)
{
	for (i = 0; eventops[i] && !base->evbase; i++) {
		base->evsel = eventops[i];
		event_msgx("[%s][%d],IO type:%s", __func__, __LINE__, base->evsel->name);
		base->evbase = base->evsel->init(base);//init
	}
}
eventops中存储了多个函数,默认的类型为epoll。
可以通过该函数设置不想使用的方法,event_config_avoid_method(config, "epoll");

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值