libevent source code analysis a --io incident response

  This article will analyze how the organization io libevent event, how to capture the occurrence of an event and the appropriate action. There will not be a detailed analysis of the details of the event and event_base, describes only how io events are stored and how to respond.

1.  select

  backend achieve io libevent event actually using io multiplexing interfaces, such as select, poll, epoll, etc., here in the most simple select example. First, a brief select the interface:

int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);

readfds, writefds, exceptfds file descriptor are set, after calling this function, if fd-readable readfds set, or set writefds writable fd, fd or exceptfds set an error occurs, or the time to reach the blocked timeout, the function returns.

  The function returns 0, return the results included in the collection into readfds readfds reference now read without blocking fd, writefds return results containing the corresponding write without blocking fd, fd exceptfds contains all the abnormality occurs. If the timeout, the function returns -1, the set is empty.

  You can see, select the incoming call requires interested io file descriptor fd, and libevent the familiar is event_base, event structure, event added to event_base after you can wait for a trigger event, how libevent is related event, event_base and select it?

In fact, the source select.c libevent, the definition of a data structure struct selectop,

struct selectop {
    int event_fds;        /* Highest fd in fd set */
    int event_fdsz;
    int resize_out_sets;
    fd_set *event_readset_in;
    fd_set *event_writeset_in;
    fd_set *event_readset_out;
    fd_set *event_writeset_out;
};

It saves each call to select libevent need to participate into the Senate, where the saved file descriptor is added to the event_base the corresponding event. Every time a new event added to the io event_base, inside the event will be added to the corresponding file descriptor fd event_readset_in and event_writeset_in, depending on the interest of the event is to read or write event event. The corresponding function call stack is:

  event_add->event_add_nolock_->evmap_io_add_->select_add

When using select libevent as a backend io, event_base a member evbase which points to a dynamically allocated structure selectop, then the file descriptor io event all interested have been saved in a structure belonging to selectop event_base in, that is when you call select be usable.

2.  event_io_map

  Previous analysis of the use of select parameters into how to save, but in addition to the event corresponding file descriptor, libevent also need to save the event structure, because event also recorded a number of other information, such as calling a timeout callback events, events time. Thus, added to the event structure event_base is also need to preserve, struct event_io_map io role event_base a member of what used to save the event structure event io added.

   struct event_io_map is a hash table, if it is in a non-windows environment, this hash table can be easily achieved with a dynamic array. For simplicity of description, here assumed to be non-windows environment. Which is defined as follows:

struct event_signal_map {
    /* An array of evmap_io * or of evmap_signal *; empty entries are
     * set to NULL. */
    void **entries;
    /* The number of entries available in entries */
    int nentries;
}; 

  It contains a dynamic array entries, the array length is expressed nentries. Dynamic content dynamically allocated array is directed evmap_io structure pointer. event corresponding to the file descriptor fd as its index in a dynamic array, and the same fd may have several events of interest to join the same event_base, so to connect them together to form a doubly linked list to resolve the conflict, the doubly linked list is struct evmap_io, that is the event fd as an index that is doubly linked list of these events can be found in the composition. event structure comprising two members ev_io pointers i.e. a doubly linked list pointer precursor and successor pointers. io event_base a member storage structure can be represented by FIGS. 2-1,

FIG. 2-1 event_io_map

3.  event_base_loop

  event structure contains callback information is already stored in the event_base of io members, to select, the corresponding file descriptor event has also been kept in the event_base of evbase members pointed struct selectop structure. So ultimately libevent how to read and write wait event occurs and eventually call the corresponding callback function do? The answer is event_base_loop function.

  First, the io event, event_base_loop each cycle, the dispatch function will be called the back-end, back-end for select, this dispatch function is select_dispatch, and select_dispatch will call the select function. Then, when the select returns a result, select_dispatch added according io will trigger descriptor from the hash table event_base event callback function to be performed event_base callback list, the list to be executed by the callback function event_base members struct evcallback_list * activequeues save. Finally, event_base_loop will execute the callback function that is on the list after dispatch, complete incident response.

4. activequeues

  activequeues is a dynamic array evcallback_list type used to implement priority, every member of an array of events to be executed is a list of the function. event_base_loop when performing these functions on the list to zero-based index in ascending order of the scanning array, if the members of the array pointed to the list is not empty, the callback function sequentially executed the above, the smaller the index, the list corresponding to the callback the first to be executed, constitutes a priority event. activequeues structure may be represented using FIGS 4-1,

Figure 4-1 event_base activequeues membership structure of

  Libevent event processing flow io simplified summarized as follows: First, the structure of the event added to the event io io event_base of members, and save the corresponding fd structure pointed to evbase; event_base_loop then call dispatch, the callback function will be triggered by an event Add to activequeues multi-priority list; callback on activequeues last event_base_loop execute, complete incident response.

The concept described part table

event_base

The basic structure of libevent, all added to the instance of the event structure, event_base_loop recall event process wherein

event

Libevent the expression of an event structure that contains information about the file descriptor, the callback function, etc.

struct selectop

Select rear end structure, structural parameter select function information storage needs, event_base pointing information using evbase

select.c

For in libevent select the back-end of the source file

struct event_io_map

Io members in event_base type, for configuration of a hash table stored event type io

struct evmap_io

Doubly linked list described structure, event type for io

activequeues

event_base structure member, dynamic array evcallback_list type list stored callback function to be performed.

 

 

 

 

 

 

 

 

 

 

 

 

  Select the backend defined in the source file select.c

  event / event_callback structure is defined in the source file in event_struct.h

  event_base structure definition in the source file in the event-internal.h

  event_add / event_base_loop / event_add_nolock function is defined in function event.c

  Function and evmap_io_add_ evmap_io / event_io_map file structures are defined in evmap.c

Guess you like

Origin www.cnblogs.com/yang-zd/p/11359666.html