Understanding and Summary of Libevent

Version: 1.4.13
What is libevent? ? ?

First of all, what to start with, libevent is an asynchronous event library, an event library that supports linux, windows, and cross-platform,
using the classic Reactor mode, the event-driven mode.
Support multiple I/O multiplexing technologies: epoll, select, poll, dev, kqueue
support I/O events, timing events and signal events


Why use Reactor mode? ? ?
Let's talk about the scene first. As a lightweight network library, libevent is bound to handle requests for multiple events. If only a few events occur, simple multi-threading can be implemented.
Then, discuss its advantages under the premise of being a high-performance network library. You can see why to choose Reactor.
The first point: Because the Reactor mode is not blocked by a single event, if the Reactor mode is not used, it is necessary to open multiple threads to respond to the occurrence of events in a timely manner. However, blindly increasing the number of threads, process scheduling will affect its performance.
The second point: The Reactor framework itself has nothing to do with the logical processing of events and has high reusability.


The process of libevent
1. Initialize libevent, which is equivalent to an instance of Reactor
2. Initialize event event, set callback function and related events
3. Set subordinate event_base
4. Add events
5. Infinite loop, waiting for


some details of ready event libevent (framework and processing method)
1.libevent's management method for event events is to use 3 doubly linked lists and a small root heap
doubly linked list:
aI/O/Signal event_list ,Linked by ev_next (a member of the event structure)
b.Signal[no sig] event_list ,Linked by ev_signal_next
c.active event_list[priortiy] ,Linked by ev_active_next
Small root heap:
Timer mini_heap ,Linked by min_heap_idx
libevent also made a small optimization for the heap adjustment of the small root heap, that is, after getting a new timing event, instead of inserting the number directly, it expands the capacity first, then the number to be sorted is compared with the parent node, The replacement operation only leaves a hole, and the value is assigned in the last step, which is more intuitive to see:
Heap tuning optimization


2. Libevent uniformly encapsulates the I/O demultiplex mechanism provided by the system into an eventop structure; therefore, eventops[] contains select, poll , kequeue and epoll, etc. several global instance objects.
The eventtop structure has five function pointers, which can point to different corresponding functions of epoll, such as: initialization, event registration, deletion of events, event distribution, and logout to release resources.
Libevent stores all supported I/O demultiplex mechanisms in a global static array eventops, and selects which mechanism to use during initialization. The contents of the array are declared as follows in priority order:

static const structeventop *eventops[] = { #ifdefHAVE_EVENT_PORTS  &evportops, 
#endif 
#ifdefHAVE_WORKING_KQUEUE 
#ifdefHAVE_EPOLL 
&epollops,
#endif 
#ifdefHAVE_DEVPOLL 
&devpollops,
#endif 
#ifdefHAVE_POLL 
 &pollops, 
 #endif 
 #ifdefHAVE_SELECT 
  &selectops, 
  #endif 
  #ifdefWIN32 
  &win32ops, 
  #endif 
  NULL 
  };



3. As mentioned earlier, libevent supports three kinds of events. Let’s talk about how to combine the three kinds of events:
The method of combining aI/O events and timing events Take
the Linux multiplexing technology as an example, whether it is select, poll or epoll has set a maximum timeout time to ensure that the function can return even if no event is triggered, so as long as the minimum time of the timer event is set to the timeout time of the system I/O.
The method of combining bI/O events and signal events
Since the triggering of signal events is random, it cannot be as simple as processing timer events. The processing method of libevent is to notify the signal events of the I/O events of the system in some way. In other words, signal and I/O events can be handled with the same mechanism. Here, socketpair is used.






Temporarily sorted here, if there is a new understanding, it will be further revised

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325858719&siteId=291194637