イベント多重分離
from ノンブロッキングI/O
同期イベント多重分離
synchronous event demultiplexing
イベント通知インターフェース
event notification interface
ビジーウェイトよりも効率的なノンブロッキングI/Oの実現方法
Event Loopをする
擬似コード ref
code:example
socketA, pipeB;
watchedList.add(socketA, FOR_READ);
watchedList.add(pipeB, FOR_READ);
while(events = demultiplexer.watch(watchedList){ // ここでwatchedListのいずれかのリソースが読み込み可能になるまでブロックする
foreach(event in events){
/*non blocking read always returns data*/
data = event.resource.read();
if(data===RESOURCE_CLOSED){
/*stop watching if resource closed*/
demultiplexer.unwatch(event.resource);
}
else
consumeData(data);
}
})
実装はOSごとに異なる
Linuxはepoll
macOSはkqueue
WindowsはIOCP API
Node.jsでは、
これらを抽象化したlibuvを使う
Bunでは、
macはkqueue
Linuxはio_uring
#??
上の擬似コードのwhileの条件節のところでブロックされるのは大丈夫なの?