I'm struggling to draw a parallel between epoll and kqueue flags, specifically EPOLLONESHOT EPOLLET EPOLLEXCLUSIVE
and EV_CLEAR/EV_DISPATCH/EV_ONESHOT. I'm investigating the kqueue for the first time; I only had an experience with epoll.
EV_DISPATCH
It feels like the mix of EPOLLEXCLUSIVE and EPOLLONESHOT flags; from the kqueue documentation:
EV_DISPATCH Disable the event source immediately after delivery of an
event. See EV_DISABLE above.
EV_DISABLE Disable the event so kevent() will not return it. The fil-
ter itself is not disabled.
Do I understand the documentation correctly that the event is signalled and then immediately discarded, if there was at least one kqueue instance which polled for this event? That is, if we poll a socket for EVFILT_READ on two kqueues, only one will receive it, and then, until the same event is set with EVFILT_ENABLE, there won't be any further events at all, even if new data comes to socket?
EV_CLEAR
Looks like it is close to EPOLLET; from the kqueue documentation:
EV_CLEAR After the event is retrieved by the user, its state is
reset. This is useful for filters which report state tran-
sitions instead of the current state. Note that some fil-
ters may automatically set this flag internally.
So, for example, given the same socket with EVFILT_READ, all kqueues, which poll it simultaneously, will wake up with EVFILT_READ. If, however, not all data is read (i.e. until EAGAIN), no further events are reported. If and only if all the data was read and a new data arrives, a new EVFILT_READ event would be triggered. Is it correct?
EV_ONESHOT
Looks like it maps to EPOLLONESHOT; from the kqueue documentation:
EV_ONESHOT Causes the event to return only the first occurrence of the
filter being triggered. After the user retrieves the event
from the kqueue, it is deleted.
Questions
So, the questions:
- Is my understanding correct? Did I understand these special flags right, compared to epoll? The documentation seems to be a bit tricky to me; perhaps the problem is that I've only used epoll before and didn't yet played with kqueue.
- Could you please provide good sources or examples to see kqueue techniques? It would be nice if it would be not that complex like Boost.Asio; it would be also nice these sources would be written in C.
- Can these flags be combined together? For example, EPOLLONESHOT cannot be combined with EPOLLEXCLUSIVE, but EV_DISPATCH seems to be exactly something in the middle between these flags.
Thank you for your help!
References
kqueue(2): FreeBSD System Calls Manual
EV_CLEAR
is not equal toEPOLLET
, e.g. some listen socket has 5 pending connections, and you don't consume all of them(accept
untilEAGAIN
), then withEV_CLEAR
, you won't getEVFILT_READ
event fromkevent
until the 6th connection appears.EPOLLEXCLUSIVE
is used for CPU binding, it isn't related toEV_DISPATCH
.EV_ONESHOT
means deleteknote
after the specific event is triggered, whileEV_DISPATCH
only disable it.If one socket fd is registered to several kqueues, then the event is broadcasted while the event is triggered.
EV_ONESHOT
is almost equal toEPOLLONESHOT
, it is useful in the case that different threads need to callkevent
with same kqueue fd.