How do "special" epoll flags correspond to kqueue ones?

970 views Asked by At

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:

  1. 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.
  2. 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.
  3. 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

epoll(7): Linux Programmer's Manual

epoll_ctl(7): Linux Programmer's Manual

1

There are 1 answers

1
CodeSun On
  1. EV_CLEAR is not equal to EPOLLET, e.g. some listen socket has 5 pending connections, and you don't consume all of them(accept until EAGAIN), then with EV_CLEAR, you won't get EVFILT_READ event from kevent until the 6th connection appears.

  2. EPOLLEXCLUSIVE is used for CPU binding, it isn't related to EV_DISPATCH.

  3. EV_ONESHOT means delete knote after the specific event is triggered, while EV_DISPATCH only disable it.

  4. If one socket fd is registered to several kqueues, then the event is broadcasted while the event is triggered.

  5. EV_ONESHOT is almost equal to EPOLLONESHOT, it is useful in the case that different threads need to call kevent with same kqueue fd.