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。
參考鏈接
- 極客時間:09 | Redis 事件驅(qū)動框架(上):何時使用 select、poll、epoll?
- 深入剖析 Netty 源碼設(shè)計(jì)(一)——深入理解 select poll epoll 機(jī)制
- 網(wǎng)絡(luò) IO 演變發(fā)展過程和模型介紹
- Kernel Queues and Events
- Kernel Queues: An Alternative to File System Events
Redis 源碼簡潔剖析系列
Java 編程思想-最全思維導(dǎo)圖-GitHub 下載鏈接,需要的小伙伴可以自取~
原創(chuàng)不易,希望大家轉(zhuǎn)載時請先聯(lián)系我,并標(biāo)注原文鏈接。