Libevent Source Analysis-event

Source: Internet
Author: User

    • Event structure
    • Event-related interfaces
    • Libevent Management of the event

Event structure

The event is the most important component in the reactor pattern. It contains a handle FD and sets which events (read/write, etc.) on the handle, sets the corresponding function pointer, and, when the event arrives, the callback function pointer to handle the event.
First look at the structure of the event. It is located in the Include/event2/event_struct.h

struct Event{Tailq_entry (Event) Ev_active_next; Tailq_entry (Event) Ev_next;/ * for managing timeouts * /Union {Tailq_entry (Event) Ev_next_with_common_timeout;intMin_heap_idx;    } Ev_timeout_pos; evutil_socket_t ev_fd;structEvent_base *ev_base; Union {/ * Used for IO events * /        struct{Tailq_entry (Event) Ev_io_next;structTimeval ev_timeout; } ev_io;/ * Used by signal events * *        struct{Tailq_entry (Event) Ev_signal_next; ShortEv_ncalls;/ * allows deletes in callback * /             Short*ev_pncalls;    } ev_signal; } _ev; Shortev_events; ShortEv_res;/ * Result passed to event callback * /     ShortEv_flags; ev_uint8_t Ev_pri;/ * Smaller numbers is higher priority * /ev_uint8_t ev_closure;structTimeval ev_timeout;/ * allows us to adopt for different types of events * /    void(*ev_callback) (evutil_socket_t, Short,void*ARG);void*ev_arg;};

Tailq_entry is a macro definition that expands to a pointer node in a doubly linked list.

 #define Tailq_entry (type ) \  struct  { struct   type  *tqe_next ; /* next  element  */\  struct  type  * * tqe_prev ; /* address  of  previous  next  element  */\ }  

This pointer information is used to manage event events. Event events are stored in the reactor with a linked list. The
ev_active_next is the location of the activation event in the Activation event chain list. There is a list of activation events in reactor, and the program iterates through the list and executes the handler for the response.
Ev_next Saves the location of the current event in the registered list. The
Ev_timeout_pos is the union, which is the position of the timer or in the time-out list.
ev_fd When the event manages the FD, which can be either socket or signal.
ev_base is the reactor where the event resides. The
_ev is a union,event that may be an IO event or a signal event. Save space with a union representation. The
ev_events indicates the type of listener event. can be the following event

#define EV_TIMEOUT 0x01  //定时器事件#define EV_READ  0x02  //IO读事件#define EV_WRITE 0x04  //IO写事件#define EV_SIGNAL 0x08  //信号事件#define EV_PERSIST 0x10 //永久事件

ev_flagsRepresents the status of the current event, with the following values

#define EVLIST_TIMEOUT  0x01 //在time堆中#define EVLIST_INSERTED 0x02  //已经添加到事件列表中#define EVLIST_SIGNAL   0x04 //#define EVLIST_ACTIVE   0x08 //在激活链表中#define EVLIST_INTERNAL 0x10 #define EVLIST_INIT 0x80  //已经初始化

ev_priRepresents the priority level. The smaller the number, the higher the priority, the function settings can be called

int event_priority_set(structeventint pri)

ev_closureDifferent callback functions are invoked according to their type.
ev_callbackis the callback function of the event. Three parameters are,, ev_fd ,, respectively ev_events ev_arg .

Event-related interfaces

The interface definition for event is in event.c. In the previous procedure.

event_set(&listenEvent, sock, EV_READ|EV_PERSIST, handleAccept, NULL);event_base_set(base, &listenEvent);event_add(&listenEvent, NULL);

which

event *ev, evutil_socket_t fd, short events,      (*callback)(evutil_socket_t, short, void *), void *arg)

The function is the parameter that sets the event.
evRepresents the event events.
fdRepresents the FD associated with the event.
callbackis a function pointer that represents the event handler.
argis the value of Ev_arg.

intevent_base_set(struct event_base *basestructevent *ev)

function to associate the event with the reactor.
baseReactor is the event's location.

intevent_add(structeventconststruct timeval *tv)

function to add an event to the reactor.
tvIs the time-out.

Libevent Management of the event


With the help of other people's blog diagram, look at the image of reactor, how to manage the event. There are corresponding lists and heaps in the reactor event_base to manage event events. Different events correspond to different pointers/fields, IO events correspond, ev_next signal events correspond, ev_signal_next timer events correspond min_head_idx , active events correspond ev_active_next . When the event is activated, the event is transferred to the list[priority] queue, and then the event one by one is processed.

Reference:
Deep understanding of the TAILQ queue

Linux kernel analysis – double linked list of data structures in the kernel (i)

Libevent source Depth analysis of five--libevent core: Events Event

Http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/structevent.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Libevent Source Analysis-event

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.