新看看官方demo的libevent如何使用信号
int called = 0;
- static void
signal_cb(int fd, short event, void *arg)
{
struct event *signal = arg;
printf("%s: got signal %d\n", __func__, EVENT_SIGNAL(signal));
if (called >= 2)
event_del(signal);
called++;
}
int
main (int argc, char **argv)
{
struct event signal_int;//这里我们把它称为事件2
/* Initalize the event library */
event_init();
/* 初始化事件2,设置相关信号,回调函数 */
event_set(&signal_int, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb,
&signal_int);
event_add(&signal_int, NULL);//激活信号
event_dispatch();//等待事件的触发
return (0);
}
这里只提信号相关操作~~~
event_init()有关信号操作将跳转到epoll.c中的epoll_init->evsignal_init
evsignal_init(base);
接下来分析evsignal_init函数,event_base结构体中有信号管理结构evsignal_info(注意不是指针)
而evsignal_info结构本身有一个event事件,这里称为事件1(这里很关键)
struct evsignal_info {
struct event ev_signal;//向event_base注册读事件使用的event结构体,这里我们称为事件1
int ev_signal_pair[2];//sock pair对,也就是clientfd跟servfd
int ev_signal_added;//记录ev_signal信号是否已经注册
volatile sig_atomic_t evsignal_caught;//是否有信号发生
struct event_list evsigevents[NSIG];//注册到信号的事件链表的一个标志
sig_atomic_t evsigcaught[NSIG];//记录每个信号的触发的次数
#ifdef HAVE_SIGACTION
struct sigaction **sh_old;//记录旧的信号处理函数
#else
ev_sighandler_t **sh_old;
#endif
int sh_old_max;
};
evsignal_init具体流程:event_init中最终会使用evsignal_init(base),来看看做了什么事
创建了一对Socketpair(作用:信号来临时,通过信号处理函数socketpair写端发送字节,事件1监听socketpair的读fd,触发事件1),并且将读的fd与事件1相关联,并且将事件1与event_base结构体的指针相关联
int
evsignal_init(struct event_base *base)
{
int i;
/*
* Our signal handler is going to write to one end of the socket
* pair to wake up our event loop. The event loop then scans for
* signals that got delivered.
*///创建一个socketpair
if (evutil_socketpair(
AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) {
#ifdef WIN32
/* Make this nonfatal on win32, where sometimes people
have localhost firewalled. */
event_warn("%s: socketpair", __func__);
#else
event_err(1, "%s: socketpair", __func__);
#endif
return -1;
}
////子进程不能访问该socketpair
FD_CLOSEONEXEC(base->sig.ev_signal_pair[0]);
FD_CLOSEONEXEC(base->sig.ev_signal_pair[1]);
base->sig.sh_old = NULL;
base->sig.sh_old_max = 0;
base->sig.evsignal_caught = 0;
memset(&base->sig.evsigcaught, 0, sizeof(sig_atomic_t)*NSIG);
/* initialize the queues for all events */
for (i = 0; i < NSIG; ++i)
TAILQ_INIT(&base->sig.evsigevents[i]);
//设置为非阻塞
evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]);
//可读事件设置与fd相关(但还缺乏注册到base注册链表中,需要在event_add中才会被注册到链表中去)
event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1],
EV_READ | EV_PERSIST, evsignal_cb, &base->sig.ev_signal);
base->sig.ev_signal.ev_base = base;//把信号对应的事件跟base相关联
base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
return 0;
}
接着分析event_add函数,实际对应epoll_add函数(以epoll举例)
实际调用的是evsignal_add函数:这里唯一注意的是(事件2并不是注册到base而是挂载到信号链表,因为信号对应的fd为-1,并没有什么卵用)
//添加事件
static int
epoll_add(void *arg, struct event *ev)
{
struct epollop *epollop = arg;//获取epoll管理结构体
struct epoll_event epev = {0, {0}};//epoll事件
struct evepoll *evep;//读写事件指针
int fd, op, events;
if (ev->ev_events & EV_SIGNAL)//是否注册了信号
return (evsignal_add(ev));//是的话,添加信号到此事件
fd = ev->ev_fd;//获取对应的描述符
if (fd >= epollop->nfds) {//判断描述符是否大于最大值,是的话,扩充
/* Extent the file descriptor array as necessary */
if (epoll_recalc(ev->ev_base, epollop, fd) == -1)
return (-1);
}
evep = &epollop->fds[fd];//获取事件指针
op = EPOLL_CTL_ADD;//默认是添加,其实还有修改等
events = 0;
if (evep->evread != NULL) {//这里epoll的修改与添加设置到一起了,如果不为空,说明本身已有事件了,那就只是修改器读写而已
events |= EPOLLIN;//修改
op = EPOLL_CTL_MOD;//设置为默认
}
if (evep->evwrite != NULL) {//为空
events |= EPOLLOUT;
op = EPOLL_CTL_MOD;
}
if (ev->ev_events & EV_READ)//是否可读
events |= EPOLLIN;//events设置为可读
if (ev->ev_events & EV_WRITE)//是否可写
events |= EPOLLOUT;//设置为可写,注意events只是int类型
epev.data.fd = fd;//epoll事件设置fd
epev.events = events;//epoll事件设置为是否可读可写
if (epoll_ctl(epollop->epfd, op, ev->ev_fd, &epev) == -1)
return (-1);
/* Update events responsible */
if (ev->ev_events & EV_READ)//更新ev_events是否可读可写,如果是,那就更新evep读写事件指针,表示此事件可读可写
evep->evread = ev;
if (ev->ev_events & EV_WRITE)
evep->evwrite = ev;
return (0);
}
evsignal_add函数分析
设置信号处理函数evsignal_handler,将事件1注册到base中(这样就可以响应了),并且将ev_signal_added标志设置为1,表示因事件1注册而表示有信号加入。
同时将事件2挂载到信号链表(通过信号值为索引)的末端。
int
evsignal_add(struct event *ev)
{
int evsignal;
struct event_base *base = ev->ev_base;
struct evsignal_info *sig = &ev->ev_base->sig;//获取信号事件结构体
if (ev->ev_events & (EV_READ|EV_WRITE))//信号事件不可以是读写
event_errx(1, "%s: EV_SIGNAL incompatible use", __func__);
evsignal = EVENT_SIGNAL(ev);//信号的fd就是信号的number
assert(evsignal >= 0 && evsignal < NSIG); // //信号不能超过NSIG这个数
if (TAILQ_EMPTY(&sig->evsigevents[evsignal])) {////如果说该信号链表为空
event_debug(("%s: %p: changing signal handler", __func__, ev));
if (_evsignal_set_handler( //设置信号处理函数,同时,保存原来的信号处理函数到ev_base->sh_old中去
base, evsignal, evsignal_handler) == -1)
return (-1);
/* catch signals if they happen quickly */
evsignal_base = base;
if (!sig->ev_signal_added) {//判断是否已加入
if (event_add(&sig->ev_signal, NULL))//正式注册,添加到epoll_wait中去
return (-1);
sig->ev_signal_added = 1;//表示已经添加了
}
}
//把ev->ev_signal_next加入到sig->evsigevents[evsignal]的链表末端
/* multiple events may listen to the same signal */
TAILQ_INSERT_TAIL(&sig->evsigevents[evsignal], ev, ev_signal_next);
return (0);
}
event_dispatch()分析,实际调用epoll_dispatch,而epoll_dispatch实际调用evsignal_process。
static int
epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
{
struct epollop *epollop = arg; //获取管理epoll的结构
struct epoll_event *events = epollop->events;//epoll的事件数组
struct evepoll *evep;
int i, res, timeout = -1;
if (tv != NULL)
timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;//设置超时事件
if (timeout > MAX_EPOLL_TIMEOUT_MSEC) {//不可以大于最大超时事件
/* Linux kernels can wait forever if the timeout is too big;
* see comment on MAX_EPOLL_TIMEOUT_MSEC. */
timeout = MAX_EPOLL_TIMEOUT_MSEC;
}
res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
if (res == -1) {
if (errno != EINTR) {
event_warn("epoll_wait");
return (-1);
}
evsignal_process(base);//处理信号事件
return (0);
} else if (base->sig.evsignal_caught) {
evsignal_process(base);//处理信号事件
}
event_debug(("%s: epoll_wait reports %d", __func__, res));
for (i = 0; i < res; i++) {
int what = events[i].events;
struct event *evread = NULL, *evwrite = NULL;
int fd = events[i].data.fd;
if (fd < 0 || fd >= epollop->nfds)
continue;
evep = &epollop->fds[fd];
if (what & (EPOLLHUP|EPOLLERR)) {
evread = evep->evread;
evwrite = evep->evwrite;
} else {
if (what & EPOLLIN) {//可读
evread = evep->evread;
}
if (what & EPOLLOUT) {//可写
evwrite = evep->evwrite;
}
}
if (!(evread||evwrite))
continue;
if (evread != NULL)//插入就绪链表
event_active(evread, EV_READ, 1);
if (evwrite != NULL)//插入就绪链表
event_active(evwrite, EV_WRITE, 1);
}
if (res == epollop->nevents && epollop->nevents < MAX_NEVENTS) {
/* We used all of the event space this time. We should
be ready for more events next time. */
int new_nevents = epollop->nevents * 2;
struct epoll_event *new_events;
new_events = realloc(epollop->events,
new_nevents * sizeof(struct epoll_event));
if (new_events) {
epollop->events = new_events;
epollop->nevents = new_nevents;
}
}
return (0);
}
evsignal_process
假设有信号发生了~将调用信号处理函数设置信号发生标志位同时发送一个字节数据到socketpair的读端,
而事件1恰好是监听此读端,所以epoll_wait返回然后看信号触发位是否设置为1了,设置了将调用evsignal_process()
而evsignal_process函数内容是遍历信号链表看是否有挂载的事件,有的话,将该事件2插入已就绪链表中,另外也将事件1插入就绪链表
void
evsignal_process(struct event_base *base)//遍历信号链表,是否有事件2挂载
{
struct evsignal_info *sig = &base->sig;//获取信号管理结构体
struct event *ev, *next_ev;
sig_atomic_t ncalls;
int i;
base->sig.evsignal_caught = 0;
for (i = 1; i < NSIG; ++i) {
ncalls = sig->evsigcaught[i];//是否有触发
if (ncalls == 0)//没有就可以滚了
continue;
sig->evsigcaught[i] -= ncalls;//有的话。。。清空
for (ev = TAILQ_FIRST(&sig->evsigevents[i]);
ev != NULL; ev = next_ev) {
next_ev = TAILQ_NEXT(ev, ev_signal_next);
if (!(ev->ev_events & EV_PERSIST))//没设置这个位,就只使用一次了
event_del(ev);
event_active(ev, EV_SIGNAL, ncalls);//插入就绪链表
}
}
}
总流程:
event_base结构体中有信号管理结构evsignal_info(注意不是指针)
而evsignal_info结构本身有一个event事件,这里称为事件1(这里很关键)
event_init中最终会使用evsignal_init(base),来看看做了什么事
创建了一对Socketpair(作用:通过信号处理函数发送字节,事件1监听读fd,触发事件1发生),并且将读的fd与事件1相关联,并且将事件1与event_base结构体的指针相关联
然后我们在main函数中,创建一个事件2,通过初始化信号2
event_set(&signal_int, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb,
&signal_int);
然后event_add实际使用的是epoll_add然后把事件插入到已注册链表
看看epoll_add实际是evsignal_add
设置信号处理函数evsignal_handler,将事件1注册到base中(这样就可以响应了),并且将ev_signal_added设置为1,表示因事件1注册而表示有信号加入。
同时将事件2挂载到信号链表(通过信号值查找)的末端(貌似事件2并不是注册到base而是挂载到链表,因为信号对应的fd为-1,并没有什么卵用)
evsignal_handler信号处理函数的内容如下
设置信号已经触发位为1,触发次数+1
同时发送1个字节数据到socketpair的读端
当当当~接下来开始dispatch了
~~~假设有信号发生了~将调用信号处理函数设置信号发生标志位同时发送一个字节数据到socketpair的读端,
而事件1恰好是监听此读端,所以epoll_wait返回然后看信号触发位是否设置为1了,设置了将调用evsignal_process()
而evsignal_process函数内容是遍历信号链表看是否有挂载的事件,有的话,将该事件2插入已就绪链表中,另外也将事件1插入就绪链表
接着发现有就绪事件,就调用event_process_active(),实际也就是调用其事件对应的回调函数完成处理~~End