libevent源码分析:time-test例子

时间:2024-04-08 12:34:50

time-test例子是libevent自带的一个例子,通过libevent提供的定时事件来实现,间隔固定时间打印的功能。

 /*
* gcc -g -o time-test time-test.c -levent_core
*/ #include <sys/types.h>
#include <event2/event-config.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <sys/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> 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 = ;
event_add(timeout, &tv);
}
} int main(int argc, char **argv)
{
struct event timeout;
struct timeval tv;
struct event_base *base;
int flags; if (argc == && strcmp(argv[], "-p"))
{
event_is_persistent = ;
flags = EV_PERSIST;
}
else
{
event_is_persistent = ;
flags = ;
} /* Initalize the event library */
base = event_base_new(); /* Initalize one event */
event_assign(&timeout, base, -, flags, timeout_cb, (void*)&timeout); evutil_timerclear(&tv);
tv.tv_sec = ;
event_add(&timeout, &tv); evutil_gettimeofday(&lasttime, NULL);
event_base_dispatch(base); return ;
}

这次就通过分析一个这个简单的例子来加深对libevent的理解。

1、首先通过event_base_new获取一个event_base对象。

2、通过event_assign来对一个event赋值(属于的event_base,监听的事件类型,回调函数等)。

3、通过event_add激活该event。

4、调用event_base_dispatch进入事件循环。

event_base_dispatch内部有一个很大的死循环,不停的调用io复用机制来监听指定文件描述符上的事件,并在相应事件发生的时候,触发相应的回调函数。

回调的堆栈如下:

libevent源码分析:time-test例子

可以看到是依次调用了event_base_loop()->event_process_active()->event_process_active_single_queue()->timeout_cb()。

 int
event_base_loop(struct event_base *base, int flags)
{
const struct eventop *evsel = base->evsel;
struct timeval tv;
struct timeval *tv_p;
int res, done, retval = ; /* Grab the lock. We will release it inside evsel.dispatch, and again
* as we invoke user callbacks. */
EVBASE_ACQUIRE_LOCK(base, th_base_lock); if (base->running_loop) {
event_warnx("%s: reentrant invocation. Only one event_base_loop"
" can run on each event_base at once.", __func__);
EVBASE_RELEASE_LOCK(base, th_base_lock);
return -;
} base->running_loop = ; clear_time_cache(base); if (base->sig.ev_signal_added && base->sig.ev_n_signals_added)
evsig_set_base_(base); done = ; #ifndef EVENT__DISABLE_THREAD_SUPPORT
base->th_owner_id = EVTHREAD_GET_ID();
#endif base->event_gotterm = base->event_break = ; while (!done) {
base->event_continue = ;
base->n_deferreds_queued = ; /* Terminate the loop if we have been asked to */
if (base->event_gotterm) {
break;
} if (base->event_break) {
break;
} tv_p = &tv;
if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
timeout_next(base, &tv_p);
} else {
/*
* if we have active events, we just poll new events
* without waiting.
*/
evutil_timerclear(&tv);
} /* If we have no events, we just exit */
if (==(flags&EVLOOP_NO_EXIT_ON_EMPTY) &&
!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
event_debug(("%s: no events registered.", __func__));
retval = ;
goto done;
} event_queue_make_later_events_active(base); clear_time_cache(base); res = evsel->dispatch(base, tv_p); if (res == -) {
event_debug(("%s: dispatch returned unsuccessfully.",
__func__));
retval = -;
goto done;
} update_time_cache(base); timeout_process(base); if (N_ACTIVE_CALLBACKS(base)) {
int n = event_process_active(base);
if ((flags & EVLOOP_ONCE)
&& N_ACTIVE_CALLBACKS(base) ==
&& n != )
done = ;
} else if (flags & EVLOOP_NONBLOCK)
done = ;
}
event_debug(("%s: asked to terminate loop.", __func__)); done:
clear_time_cache(base);
base->running_loop = ; EVBASE_RELEASE_LOCK(base, th_base_lock); return (retval);
}

event_base_loop()

其实如果去打开event_base_loop()函数的实现来看,会发现对于事件的处理时分两步的,先是调用dispatch,把不同io复用机制得到的事件转换为统一的libevent事件,并将这些事件放到event_base的激活事件队列中,然后再调用event_process_active函数从激活事件队列中挨个读取每个事件并调用其回调函数。