Envoy:event相关代码阅读(一)

2023-10-30 16:03:59 浏览数 (2)

本篇文章试图来介绍envoy的事件处理部分的代码,对于envoy来说是基于libevent做了简单封装来实现的异步调度。

本篇文章会从下面两部分来进行讲解,libevent的基础知识介绍,envoy中event的类的实现和event在envoy中的调度逻辑。

一、libevent的基础知识篇

1.事件flags:

#define EV_TIMEOUT 0x01 Indicates that a timeout has occurred. #define EV_READ 0x02 Wait for a socket or FD to become readable. #define EV_WRITE 0x04 Wait for a socket or FD to become writeable. #define EV_SIGNAL 0x08 Wait for a POSIX signal to be raised. #define EV_PERSIST 0x10 Persistent event: won't get removed automatically when activated. #define EV_ET 0x20 Select edge-triggered behavior, if supported by the backend. #define EV_FINALIZE 0x40 If this option is provided, then event_del() will not block in one thread while waiting for the event callback to complete in another thread. #define EV_CLOSED 0x80 Detects connection close events.

2.信号量对事件的封装

对于事件,libevent分成了三类,分别是:signal、timer和文件,同时libevent也对他们做了函数层面的重定义,如下所示:

#define evsignal_add(ev, tv) event_add((ev), (tv))

#define evsignal_assign(ev, b, x, cb, arg) event_assign((ev), (b), (x), EV_SIGNAL|EV_PERSIST, cb, (arg))

#define evsignal_new(b, x, cb, arg) event_new((b), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg))

#define evsignal_del(ev) event_del(ev)

#define evsignal_pending(ev, tv) event_pending((ev), EV_SIGNAL, (tv))

#define evsignal_initialized(ev) event_initialized(ev)

Timer对事件的封装: #define evtimer_assign(ev, b, cb, arg) event_assign((ev), (b), -1, 0, (cb), (arg))

#define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg))

#define evtimer_add(ev, tv) event_add((ev), (tv))

#define evtimer_del(ev) event_del(ev)

#define evtimer_pending(ev, tv) event_pending((ev), EV_TIMEOUT, (tv))

#define evtimer_initialized(ev) event_initialized(ev)

用户对事件的封装: #define evuser_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg))

#define evuser_del(ev) event_del(ev)#define evuser_pending(ev, tv) event_pending((ev), 0, (tv))

#define evuser_initialized(ev) event_initialized(ev)

#define evuser_trigger(ev) event_active((ev), 0, 0

3.基础函数介绍

对于单个事件来说,主要具备下面几种状态,而这些状态之间的转换基本就是下面的这个图描述的样子。

event_assign() 描述:Prepare a new, already-allocated event structure to be added.

event_new() 描述:Allocate and assign a new event structure, ready to be added.

event_add() 描述:Add an event to the set of pending events.

event_del() 描述:Remove an event from the set of monitored events.

event_free() 描述:Deallocate a struct event * returned by event_new().

event_active() 描述:Make an event active.You can use this function on a pending or a non-pending event to make it active,so that its callback will be run by event_base_dispatch() or event_base_loop(). One common use in multithreaded programs is to wake the thread running event_base_loop() from another thread.

event_pending() 描述:Checks if a specific event is pending or scheduled.

event_remove_timer() 描述:Remove a timer from a pending event without removing the event itself.

event_initialized() 描述:Test if an event structure might be initialized.

event_init() 描述:Initialize the event API.

event_loop() 描述: Handle events.This function behaves like event_base_loop(), but uses the "current" base

event_loopbreak() 描述: Abort the active event_loop() immediately.This function behaves like event_base_loopbreakt(), except that it uses the "current" base.

event_loopexit() 描述:Exit the event loop after the specified time.This function behaves like event_base_loopexit(), except that it uses the "current" base. event_once() 描述:Schedule a one-time event to occur.

event_set() 描述:Prepare an event structure to be added.

event_dispatch() 描述: Loop to process events.Like event_base_dispatch(), but uses the "current" base.

event_base_active_by_fd() 描述: Activates all pending events for the given fd and event mask. This function activates pending events only. Events which have not been added will not become active. event_base_active_by_signal() 描述:Activates all pending signals with a given signal number. This function activates pending events only. Events which have not been added will not become active. event_base_dispatch() 描述:Event dispatching loop. This loop will run the event base until either there are no more pending or active, or until something calls event_base_loopbreak() or event_base_loopexit(). event_base_loop() 描述:Wait for events to become active, and run their callbacks.This is a more flexible version of event_base_dispatch(). By default, this loop will run the event base until either there are no more pending or active events, or until something calls event_base_loopbreak() or event_base_loopexit(). You can override this behavior with the 'flags' argument.

备注:第二部分,请在下一篇文章查看。

参考文档:

https://libevent.org/doc/event_8h.html#acd7da32086d1e37008e7c76c9ea54fc4

0 人点赞