Libev Study Notes 1

Source: Internet
Author: User

Similar to Libevent, Libev is a high-performance event-driven framework, which is said to be higher than libevent, with fewer bugs than libevent. Libev is just an event-driven framework, not a network library, because it does not have any socket programming code inside it. Supported event-driven mechanisms include:

Select Pollepollkqueuesolaris Event Port mechanisms

There are also a number of supported event types, all listed below.

Official Homepage Address: http://software.schmorp.de/pkg/libev.html

Installation method: Apt-get Install Libev-dev

The first routine of the official document:

//a single header file is required#include <ev.h>#include<stdio.h>//For puts//Every watcher type have its own typedef ' d struct//With the name Ev_typeev_io Stdin_watcher;ev_timer timeout_watcher;//All watcher callbacks has a similar signature//This callback was called when data was readable on stdinStatic voidSTDIN_CB (ev_p_ ev_io*w,intrevents) {Puts ("stdin Ready"); //for one-shot events, one must manually stop the watcher//With its corresponding stop function.Ev_io_stop (Ev_a_ W); //This causes all nested Ev_run ' s to stop iteratingev_break (Ev_a_ evbreak_all);}//another callback, this time for a time-outStatic voidTIMEOUT_CB (ev_p_ ev_timer*w,intrevents) {Puts ("Timeout"); //This causes the innermost ev_run to stop iteratingev_break (Ev_a_ evbreak_one);}intMain (void){    //Use the default event loop unless special needs    structEv_loop *loop =Ev_default; //initialise an IO watcher and then start it//This one would watch for stdin to become readableEv_io_init (&stdin_watcher, STDIN_CB,/*Stdin_fileno*/ 0, Ev_read); Ev_io_start (Loop,&stdin_watcher); //initialise A timer watcher, then start it//Simple non-repeating 5.5 second timeoutEv_timer_init (&timeout_watcher, TIMEOUT_CB,5.5,0.); Ev_timer_start (Loop,&timeout_watcher); //Now wait for events to arriveEv_run (Loop,0); //Break is called, so exit    return 0;}

These event-based libraries are used in a similar way: first create an event loop, then create an event to observe, set the callback function, add the event to the event loop, and finally run the loop. The above procedure focuses on two types of events:

    1. I/O events, represented by the EV_IO structure.
    2. Timer event, expressed in ev_timer structure.

Different types of events require different struct types, and the corresponding relationships are as follows:

Ev_io//io readable writableEv_stat//file attribute ChangesEv_signal//Signal ProcessingEv_timer//Relative TimersEv_periodic//Absolute TimersEv_child//child process state changesEv_fork//Fork EventEv_cleanup//Event loop exits trigger eventsEv_idle//Event Loop Idle trigger eventsEv_embed//embed another background loopEv_prepare//events before event loopEv_check//Events after event loopEv_async//asynchronous events between Threads

First analyze the struct Ev_loop, the event loop, where a thread can have only one struct ev_loop, which is defined as follows:

#ifEv_multiplicity/*need to define Ev_loop class when multi-threading is supported*/  structEv_loop {Ev_tstamp ev_rt_now; #defineEv_rt_now (loop)->ev_rt_now)#defineVAR (NAME,DECL) decl;#include"Ev_vars.h"    #undefVar  }; #include"Ev_wrap.h"  Static structEv_loop default_loop_struct; Ev_api_declstructEv_loop *ev_default_loop_ptr =0;/*Needs to being initialised to make it a definition despite extern*/#else  /*All variables are global variables when single thread*/ev_api_decl Ev_tstamp Ev_rt_now=0;/*Needs to being initialised to make it a definition despite extern*/  #defineVAR (name,decl) static decl;#include"Ev_vars.h"  #undefVarStatic intev_default_loop_ptr;#endif

Conditional compilation macros ev_multiplicity determine whether a program is multithreaded when compiling a program. When the program is multi-threading, that is, the if branch of the above program, because of the existence of different ev_loop, then the struct ev_loop is required to encapsulate the struct. When the program is single-threaded, that is, the else branch of the above program, the entire program has only one ev_loop, which will be used as a global variable by all functions. A variety of macros are defined in the header file Ev_vars.h, which encapsulates member variables that are equivalent to Ev_loop in Ev_loop, while the ev_vars.h of the Else branch is not encapsulated in any struct, so it declares a whole bunch of global variables. after in-depth analysis, found that ev_multiplicity equals 1, so after the source analysis is assumed ev_multiplicity=1.

Many of the functions in Libev are in the form of:

Static void  int revents)

Ev_p_ is also available for multi-threading, and its definition is as follows:

/*Support multiple event loops?*/#ifEv_multiplicitystructev_loop;# define Ev_pstructEv_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*/# define EV_DEFAULT_UC ev_default_loop_uc_ ()/*The default loop, if initialised, as sole arg*/# define EV_DEFAULT_UC_ Ev_default_uc,/*The default loop as first of multiple arguments*/# define Ev_default ev_default_loop (0)/*The default loop as sole arg*/# define Ev_default_ ev_default,/*The default loop as first of multiple arguments*/#else# define Ev_pvoid# define ev_p_# define ev_a# define ev_a_# define ev_default# define ev_default_# define ev_default_uc# define Ev_def ault_uc_# undef ev_embed_enable#endif

The macro ev_p_ eventually equals the struct Ev_loop *loop. When the system supports multi-threading, each thread has a struct ev_loop, the function needs to distinguish between different struct ev_loop, which is to pass the macro ev_p_ into a specific struct ev_loop. That is, when the program is single-threaded, the function is ultimately declared as:

Static void  int revents)

The Libev manipulates only one global struct ev_loop. When a program supports multiple threads, the function is ultimately declared as:

Static void TIMEOUT_CB (structint revents)

The Libev manipulates the corresponding struct ev_loop according to the incoming loop.

Reference:

http://c4fun.cn/blog/2014/03/06/libev-study/

Libev Learning Note 1

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.