Libev Study notes (continuous update)

Source: Internet
Author: User
Tags readable

2014-09-03

--

==libev Learning Notes = =


Reference:
1. Http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod
2.


Brief introduction:
Libev-a High Performance full-featured event loop written in C


Libev is a high-performance event loop library written by Marc Lehmann in C. Through the Libev, the various events can be flexibly managed, such as: clocks, IO, signals, etc. Libev in the industry is also widely praised, many projects are used to do the underlying event loop.
The function that Libev implements is a powerful reactor, the events that can notify mainly include the following:


Ev_io//IO readable writable
Ev_stat//File attribute changes
Ev_signal//Signal processing
Ev_timer//Relative timer
Ev_periodic//Absolute Timer
Ev_child//Sub-process status change
Ev_fork//Fork Event
Ev_cleanup//Event loop Exit Trigger Event
Ev_idle//Event loop Idle trigger Event
ev_embed//Embed another background loop
Ev_prepare//Event loop events before
Events after Ev_check//event loop
Ev_async//asynchronous events between threads


1. Some macro definitions:
/* Support multiple Event loops */
#if ev_multiplicity
struct Ev_loop;
# define ev_p struct Ev_loop *loop * a loop as sole parameter in a declaration */
# define EV_P_ ev_p,/* a loop as first of multiple parameters * *
# define EV_A Loop/* A loop as sole argument to a function call */
# define Ev_a_ ev_a,/* A Loop as first of multiple arguments * *


Ev_loop is the main loop, and Ev_multiplicity is a conditionally compiled macro that indicates whether multiple Ev_loop instances are supported, and in general, there is only one Ev_loop instance per thread.
If the entire program is single-threaded, you can use the global default Ev_loop in your program, and you do not need to pass parameters in the function. Calling functions in multiple threads often specifies loops for function operations.
For example, to start an IO event, call the function is void Ev_io_start (Ev_p_ ev_io *w), if not defined ev_multiplicity, will be compiled into Ev_io_start (io *w),
Otherwise it will be compiled into Ev_io_start (struct Ev_loop *loop, Ev_io *w). For each event, there are structural ev_type corresponding to it, such as Ev_io,ev_timer and so on.
In order to unify the event structure, Libev uses struct layout in C to achieve polymorphism, and the ev_watcher struct can be considered as the base class for all ev_type structures, which contains the same fields in all Ev_type.


2. Libev set user custom data:
void Ev_set_userdata (loop, void *data)/* Set Userdata=data */
void *ev_userdata (Loop)/* return UserData */


3. Ev_loop_fork (Loop):
/* Support multiple Event loops */
while (I < process_num) {
int pid = fork ();
if (PID = = 0) {
Ev_loop_fork (loop);
}
}


4. Ev_io:
The main mission of Ev_io is to listen and respond to a specified file describing the read and write events on the FD. The monitoring work of FD is mainly entrusted to the bottom-level IO library to complete. Libev provides support for the most popular IO libraries, such as SELECT, Epoll, and Windows IOCP. Here Libev uses the adaptor mode,
Hides the details of the underlying IO library through a unified adaptation layer.


Description of Ev_io on the manual:
I/O watchers check whether a file descriptor is readable or writable in each iteration of the event loop, or, more precise Ly, when reading would isn't block the process and writing would at least is able to write some data. This behaviour are called level-triggering because you keep receiving events as long as the condition persists.

/* Invoked when FD is either ev_readable or ev_writeable */
/* Revent Ev_read, Ev_write */
typedef struct EV_IO
{
Ev_watcher_list (Ev_io)


int FD; /* RO */
int events; /* RO */
} ev_io;


5. Ev_io_init (EV, CB, FD, events):
#define EV_IO_INIT (ev,cb,fd,events) do {ev_init (EV), (CB)) Ev_io_set ((EV), (FD), (events));} while (0)


For example:
ev_io* read_ev_io = (ev_io*) malloc (sizeof (EV_IO));
Ev_io_init (Read_ev_io, Mysql_libev_read_callback, Mysql_get_file_descriptor (MySQL), ev_read);


6. Ev_io_start (loop, Ev):
Next Example:
Ev_io_start (Loop, Read_ev_io)


7. Ev_io_stop (Loop, Ev):
Next Example:
Ev_io_stop (Loop, Read_ev_io)









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.