Is there any way to emulate epoll_wait with kqueue/kevent?
I have a list of a bunch of file descriptors that I have created kevents for, and I'm trying to figure out if there's any way to get the number of them that are ready for read or write access.
Is there any way to get a list of "ready" file开发者_C百科 descriptors, like what epoll_wait provides?
Events that occurred are placed into the eventlist
buffer passed to the kevent
call. So making this buffer sufficiently large will give you the list you are seeking. The return
value of the kevent
call will tell you have many events
are in the eventlist
buffer.
If using a large buffer is not feasible for some reason,
you can always do a loop calling kevent
with a zero timeout
and a smaller buffer, until you get zero events in the eventlist.
To give a little more context...
One of the expected scenarios with kevent() is that you will thread pool calls to it. If you had 3 thread pools all asking for 4 events, the OS would like to be able to pool and dispatch the actual events as it sees fit.
If 7 events are available the OS might want to dispatch to 3 threads, or it might want to dispatch to all 3 threads if it believed it had empty cores and less overhead.
I'm not saying your scenario is invalid at all; just that the system is more or less designed to keep that information away from you so it doesn't get into scenarios of saying 'well, 12 descriptors are ready. Oh, hrm, I just told you that but 3 of them got surfaced before you had a chance to do anything'.
Grrr pretty much nailed the scenario. You register/deregister your descriptors once and the relevent descriptor will be provided back to you with the event when the event triggers.
精彩评论