前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Envoy:event相关代码阅读(一)

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

作者头像
灰子学技术
发布2023-10-30 16:03:59
1480
发布2023-10-30 16:03:59
举报
文章被收录于专栏:灰子学技术灰子学技术

本篇文章试图来介绍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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-02-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 灰子学技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本篇文章试图来介绍envoy的事件处理部分的代码,对于envoy来说是基于libevent做了简单封装来实现的异步调度。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档