Redis 源碼簡潔剖析 08 - epoll

select, poll, epoll

關(guān)于 select, poll, epoll,網(wǎng)絡(luò) IO 演變發(fā)展過程和模型介紹 這篇文章講得很好,本文就不浪費(fèi)筆墨了。

image

Redis 如何針對不同操作系統(tǒng),選擇不同的 IO 多路復(fù)用機(jī)制,具體代碼在 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)。

源碼分析

由于我的開發(fā)環(huán)境是 Mac,所以分析 ae_kqueue.c 文件。在 Linux 系統(tǒng)下可以分析 ae_epoll.c 文件。kqueue 的詳細(xì)介紹: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 編程思想-最全思維導(dǎo)圖-GitHub 下載鏈接,需要的小伙伴可以自取~

原創(chuàng)不易,希望大家轉(zhuǎn)載時請先聯(lián)系我,并標(biāo)注原文鏈接。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容