event based: libev libevent libuv

libev

===

First let's talk about libev, it is a lib used in ss.

The coding style of libev is ugly.

To see man page of libev by: man ./ev.3

3 files need to take care when understanding the implementation of the libev: ev.c, ev_epoll.c, ev_vars.h.

ev_vars.h defines the most members inside the struct ev_loop, which is the central structure of the lib.

In linux's epoll implementation of the libev, fds are added to the ev_io var by ev_io_init(); and then this event is added to the main *loop global variable, which is the EV_A through ev_io_start(); The main epoll_ctl and epoll_wait behavior runs in ev_run();

What happens in ev_run()? It triggers the .backend_poll() which is assigned during initialization in loop_init() when we do EV_P = EV_DEFAULT. And if it is linux, the epoll_poll() is used.

In epoll_poll(), the backend_fd is epoll_wait()-ed, number of observed fds is returned, and all the observed fds are iterated. If observed ops do not match the ops that we want, the epoll_ctl() is called with MOD/DEL accordingly. then fd_event() ->fd_event_nocheck()->ev_feed_event() is called. In the ev_feed_event() function, the ev_io *ptr of the fds and the observed revent (EV_READ/EV_WRITE) is pushed into a pending list of global loop->pendings, with priority as its 2-dim array index and returned to ev_run() main loop, the last step of the main loop is the EV_INVOKE_PENDING, which is to invoke the registered cb function to the fd with priority ordered.

libevent

====

 

posted on 2018-10-19 03:13  三叁  阅读(350)  评论(0编辑  收藏  举报

导航