Analysis of Libevent source code (II): The realization of libevent timer

Source: Internet
Author: User

In Libevent, the implementation of the timer is achieved by a priority queue based on the minimum heap.

The two data structures are unfamiliar to the 6.5 section of the introduction of the algorithm can be flipped.

The main source code is in the MIN_HEAP.C.

Let's look at the main data structures first:

typedef struct min_heap
{
  struct event** p;
  unsigned n, a;
} min_heap_t;

In this data structure P is the entire priority queue, and each node of this priority queue is a struct *EVENT.N representing the number of elements in the queue. A indicates the size of this queue.

Let's look at some of the main ways:

Min_heap_reserve adjusts the size of the queue.

int min_heap_reserve(min_heap_t* s, unsigned n)
{
  if(s->a < n)
  {
    struct event** p;
///这里可以看到当需要扩大队列的空间时,每次都是以8的倍数进行扩展
    unsigned a = s->a ? s->a * 2 : 8;
    if(a < n)
      a = n;
///扩大队列的空间
    if(!(p = (struct event**)realloc(s->p, a * sizeof *p)))
      return -1;
    s->p = p;
    s->a = a;
  }
  return 0;
}

MIN_HEAP_SHIFT_UP_ inserts a timer into the current queue:

void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
{
 
///首先计算当前插入点的元素的父节点。
  unsigned parent = (hole_index - 1) / 2;
///循环处理定时器的位置,首先比较当前的定时器与他的父节点的定时器,如果大于父节点的定时器,则直接跳过循环然后将定时器加入队列。如果大与父节点的定时器则交换两个节点的值,然后这里还有个需要注意的地方就是min_heap_idx这个数据,它表示当前event对象也就是定时器对象在定时器队列的索引值。
  while(hole_index && min_heap_elem_greater(s->p[parent], e))
  {
    (s->p[hole_index] = s->p[parent])->min_heap_idx = hole_index;
    hole_index = parent;
    parent = (hole_index - 1) / 2;
  }
///得到定时器应插入的位置hole_index.
  (s->p[hole_index] = e)->min_heap_idx = hole_index;
}

Min_heap_shift_down_ takes out the current minimum time timer, which is actually the root node, and then balances the minimum heap.

void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
{
///得到当前节点的右孩子。每次取出root节点之后,传递最后一个元素到root节点,然后平衡此最小堆。
  unsigned min_child = 2 * (hole_index + 1);
  while(min_child <= s->n)
 {
    min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
    if(!(min_heap_elem_greater(e, s->p[min_child])))
      break;
    (s->p[hole_index] = s->p[min_child])->min_heap_idx = hole_index;
    hole_index = min_child;
    min_child = 2 * (hole_index + 1);
 }
  min_heap_shift_up_(s, hole_index, e);
}

PS: In fact, as long as the minimum heap and priority queue to understand, the implementation of this timer is very simple. But Libevent's algorithm is a bit ugly.

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.