Redis 源碼簡潔剖析 08 - epoll

select, poll, epoll

關於 select, poll, epoll,​網絡 IO 演變發展過程和模型介紹 這篇文章講得很好,本文就不浪費筆墨了。

Redis 如何針對不同操作系統,選擇不同的 IO 多路複用機制,具體代碼在 ae.c。

/* Include the best multiplexing layer supported by this system.
 * The following should be ordered by performances, descending. */
#ifdef HAVE_EVPORT
#include "ae_evport.c"
#else
    #ifdef HAVE_EPOLL
    #include "ae_epoll.c"
    #else
        #ifdef HAVE_KQUEUE
        #include "ae_kqueue.c"
        #else
        #include "ae_select.c"
        #endif
    #endif
#endif

從代碼中可看到,有 epoll 就會使用 epoll(Linux);沒有的話則會使用 kqueue(MacOS)或 select(Windows)。

源碼分析

由於我的開發環境是 Mac,所以分析 ae_kqueue.c 文件。在 Linux 系統下可以分析 ae_epoll.c 文件。kqueue 的詳細介紹:Kernel Queues and Events

typedef struct aeApiState {
    int kqfd;
    struct kevent *events;

    /* Events mask for merge read and write event.
     * To reduce memory consumption, we use 2 bits to store the mask
     * of an event, so that 1 byte will store the mask of 4 events. */
    char *eventsMask; 
} aeApiState;

kevent 定義在 event.h 源文件中。

struct kevent {
	uintptr_t       ident;  /* identifier for this event */
	int16_t         filter; /* filter for event */
	uint16_t        flags;  /* general flags */
	uint32_t        fflags; /* filter-specific flags */
	intptr_t        data;   /* filter-specific data */
	void            *udata; /* opaque user data identifier */
};

具體源碼 // todo。

參考鏈接

Redis 源碼簡潔剖析系列

最簡潔的 Redis 源碼剖析系列文章

Java 編程思想-最全思維導圖-GitHub 下載鏈接,需要的小夥伴可以自取~

原創不易,希望大家轉載時請先聯繫我,並標註原文鏈接。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章