出處:http://blog.csdn.net/beitiandijun/article/details/72772899
一、事件類型 event-internal.h
- /**
- * @name event flags
- *
- * Flags to pass to event_new(), event_assign(), event_pending(), and
- * anything else with an argument of the form "short events"
- */
- /**@{*/
- /** Indicates that a timeout has occurred. It's not necessary to pass
- * this flag to event_for new()/event_assign() to get a timeout. */
- // 定時事件
- #define EV_TIMEOUT 0x01
- /** Wait for a socket or FD to become readable */
- // 讀事件
- #define EV_READ 0x02
- /** Wait for a socket or FD to become writeable */
- // 寫事件
- #define EV_WRITE 0x04
- /** Wait for a POSIX signal to be raised*/
- // 信號
- #define EV_SIGNAL 0x08
- /**
- * Persistent event: won't get removed automatically when activated.
- *
- * When a persistent event with a timeout becomes activated, its timeout
- * is reset to 0.
- */
- // 永久事件,激活執行後會重新加到隊列中等待下一次激活,否則激活執行後會自動移除
- #define EV_PERSIST 0x10
- /** Select edge-triggered behavior, if supported by the backend. */
- // 邊沿觸發,一般需要後臺方法支持
- #define EV_ET 0x20
- /**
- * 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.
- *
- * To use this option safely, you may need to use event_finalize() or
- * event_free_finalize() in order to safely tear down an event in a
- * multithreaded application. See those functions for more information.
- *
- * THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
- * BECOMES STABLE.
- **/
- // 終止事件,如果設置這個選項,則event_del不會阻塞,需要使用event_finalize或者
- // event_free_finalize以保證多線程安全
- #define EV_FINALIZE 0x40
- /**
- * Detects connection close events. You can use this to detect when a
- * connection has been closed, without having to read all the pending data
- * from a connection.
- *
- * Not all backends support EV_CLOSED. To detect or require it, use the
- * feature flag EV_FEATURE_EARLY_CLOSE.
- **/
- // 檢查事件連接是否關閉;可以使用這個選項來檢測鏈接是否關閉,而不需要讀取此鏈接所有未決數據;
- #define EV_CLOSED 0x80
二、事件狀態標誌 event_struct.h中
- // 事件在time min_heap堆中
- #define EVLIST_TIMEOUT 0x01
- // 事件在已註冊事件鏈表中
- #define EVLIST_INSERTED 0x02
- // 目前未使用
- #define EVLIST_SIGNAL 0x04
- // 事件在激活鏈表中
- #define EVLIST_ACTIVE 0x08
- // 內部使用標記
- #define EVLIST_INTERNAL 0x10
- // 事件在下一次激活鏈表中
- #define EVLIST_ACTIVE_LATER 0x20
- // 事件已經終止
- #define EVLIST_FINALIZING 0x40
- // 事件初始化完成,但是哪兒都不在
- #define EVLIST_INIT 0x80
- // 包含所有事件狀態,用於判斷合法性的
- #define EVLIST_ALL 0xff
三、後臺方法 event.c
- /* Array of backends in order of preference. */
- 後臺方法的全局靜態數組
- static const struct eventop *eventops[] = {
- #ifdef EVENT__HAVE_EVENT_PORTS
- &evportops,
- #endif
- #ifdef EVENT__HAVE_WORKING_KQUEUE
- &kqops,
- #endif
- #ifdef EVENT__HAVE_EPOLL
- &epollops,
- #endif
- #ifdef EVENT__HAVE_DEVPOLL
- &devpollops,
- #endif
- #ifdef EVENT__HAVE_POLL
- &pollops,
- #endif
- #ifdef EVENT__HAVE_SELECT
- &selectops,
- #endif
- #ifdef _WIN32
- &win32ops,
- #endif
- NULL
- };
四、後臺方法特徵列表 event.h
- /**
- A flag used to describe which features an event_base (must) provide.
- Because of OS limitations, not every Libevent backend supports every
- possible feature. You can use this type with
- event_config_require_features() to tell Libevent to only proceed if your
- event_base implements a given feature, and you can receive this type from
- event_base_get_features() to see which features are available.
- */
- // 用來描述event_base必須提供的特徵值,其實是後臺方法提供的;
- // 因爲OS的限制,不是所有event_base後臺方法都支持每個可能的特徵;
- // 必須使用event_config_require_features()進行配置,同時必須使用
- // event_base_get_features()查看是否支持配置的特徵。
- enum event_method_feature {
- /** Require an event method that allows edge-triggered events with EV_ET. */
- // 邊沿觸發,高效但是容易丟消息,注意與水平觸發區分
- EV_FEATURE_ET = 0x01,
- /** Require an event method where having one event triggered among
- * many is [approximately] an O(1) operation. This excludes (for
- * example) select and poll, which are approximately O(N) for N
- * equal to the total number of possible events. */
- // 要求具有很多事件的後臺方法可以以近似O(1)處理事件;select和poll
- // 無法提供這種特徵,它們只能提供近似O(N)的操作
- EV_FEATURE_O1 = 0x02,
- /** Require an event method that allows file descriptors as well as
- * sockets. */
- // 後臺方法可以處理包括sockets在內的各種文件描述符
- EV_FEATURE_FDS = 0x04,
- /** Require an event method that allows you to use EV_CLOSED to detect
- * connection close without the necessity of reading all the pending data.
- *
- * Methods that do support EV_CLOSED may not be able to provide support on
- * all kernel versions.
- **/
- // 要求後臺方法可以使用EV_CLOSED檢測鏈接關閉,而不需要讀完所有未決數據才能判斷
- // 支持EV_CLOSED的後臺方法不是所有OS內核都支持的
- EV_FEATURE_EARLY_CLOSE = 0x08
- };
五、event_base或者libevent工作模式 event.h
- /**
- A flag passed to event_config_set_flag().
- These flags change the behavior of an allocated event_base.
- @see event_config_set_flag(), event_base_new_with_config(),
- event_method_feature
- */
- // 可以使用event_config_set_flag設置以下配置。
- // 這個配置可以改變event_base的行爲
- enum event_base_config_flag {
- /** Do not allocate a lock for the event base, even if we have
- locking set up.
- Setting this option will make it unsafe and nonfunctional to call
- functions on the base concurrently from multiple threads.
- */
- // 非阻塞模式,多線程不安全
- EVENT_BASE_FLAG_NOLOCK = 0x01,
- /** Do not check the EVENT_* environment variables when configuring
- an event_base */
- // 這種模式下不再檢查EVENT_*環境變量
- EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
- /** Windows only: enable the IOCP dispatcher at startup
- If this flag is set then bufferevent_socket_new() and
- evconn_listener_new() will use IOCP-backed implementations
- instead of the usual select-based one on Windows.
- */
- // 只應用於windows環境
- EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
- /** Instead of checking the current time every time the event loop is
- ready to run timeout callbacks, check after each timeout callback.
- */
- // 每次event_loop準備運行timeout回調時,不再檢查當前的時間,而是
- // 在每次timeout回調之後檢查
- EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
- /** If we are using the epoll backend, this flag says that it is
- safe to use Libevent's internal change-list code to batch up
- adds and deletes in order to try to do as few syscalls as
- possible. Setting this flag can make your code run faster, but
- it may trigger a Linux bug: it is not safe to use this flag
- if you have any fds cloned by dup() or its variants. Doing so
- will produce strange and hard-to-diagnose bugs.
- This flag can also be activated by setting the
- EVENT_EPOLL_USE_CHANGELIST environment variable.
- This flag has no effect if you wind up using a backend other than
- epoll.
- */
- // 如果後臺方法是epoll,則此模式是指可以安全的使用libevent內部changelist
- // 進行批量增刪而儘可能減少系統調用。這種模式可以讓代碼性能更高,
- // 但是可能會引起Linux bug:如果有任何由dup()或者他的變量克隆的fds,
- // 則是不安全的。這樣做會引起奇怪並且難以檢查的bugs。此模式可以通過
- // EVENT_EPOLL_USE_CHANGELIST環境變量激活。
- // 此模式只有在使用epoll時可用
- EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
- /** Ordinarily, Libevent implements its time and timeout code using
- the fastest monotonic timer that we have. If this flag is set,
- however, we use less efficient more precise timer, assuming one is
- present.
- */
- // 通常情況下,libevent使用最快的monotonic計時器實現自己的計時和超時控制;
- // 此模式下,會使用性能較低但是準確性更高的計時器。
- EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
- };
六、事件關閉時的回調函數模式類型
- /** @name Event closure codes
- Possible values for evcb_closure in struct event_callback
- @{
- */
- /** A regular event. Uses the evcb_callback callback */
- // 常規事件,使用evcb_callback回調
- #define EV_CLOSURE_EVENT 0
- /** A signal event. Uses the evcb_callback callback */
- // 信號事件;使用evcb_callback回調
- #define EV_CLOSURE_EVENT_SIGNAL 1
- /** A persistent non-signal event. Uses the evcb_callback callback */
- // 永久性非信號事件;使用evcb_callback回調
- #define EV_CLOSURE_EVENT_PERSIST 2
- /** A simple callback. Uses the evcb_selfcb callback. */
- // 簡單回調,使用evcb_selfcb回調
- #define EV_CLOSURE_CB_SELF 3
- /** A finalizing callback. Uses the evcb_cbfinalize callback. */
- // 結束的回調,使用evcb_cbfinalize回調
- #define EV_CLOSURE_CB_FINALIZE 4
- /** A finalizing event. Uses the evcb_evfinalize callback. */
- // 結束事件回調,使用evcb_evfinalize回調
- #define EV_CLOSURE_EVENT_FINALIZE 5
- /** A finalizing event that should get freed after. Uses the evcb_evfinalize
- * callback. */
- // 結束事件之後應該釋放,使用evcb_evfinalize回調
- #define EV_CLOSURE_EVENT_FINALIZE_FREE 6
- /** @} */