Number of kevents registered in kqueue

Vlad GALU vladgalu at gmail.com
Fri Dec 16 14:25:30 PST 2005


On 12/17/05, Seán C. Farley <sean-freebsd at farley.org> wrote:
> I may have missed it in the man page, but I am unable to find a way to
> determine how many kevents are currently registered within a kqueue.  If
> there is no method for a count, how about a way to find if a kqueue is
> empty or not.  Besides tracking what events are still within a kqueue,
> this would make for an easier way to write an event loop.  Currently,
> calling kevent() on an empty kqueue will still block.

   kevent() returns the number of events from the changelist, which is
at most nevents long. So you know already how many events you've
registered and how many of them were yielded. As for blocking on an
empty queue, you can get rid of it by passing a pointer to a zeroed
struct timespec as the last argument to kevent(). This usually works:

-- cut here --
 int ev = kevent(kq, NULL, 0, events, maxevents, &timeout); for (int i
= 0; i < ev; i++) ...
-- and here --

 where kq is the kqueue descriptor, events is a pointer to a struct
kevent array, maxevents is the maximum number of watched events and
timeout is a struct timespec with all members set to 0.
 For examples you can check thttpd/lighttpd and/org libevent, they all
provide wrappers around kqueue.



> Also, I recommend that the man page mention that a kqueue may be
> close(2)'d.  The only reference that it can be is that the manual page
> says the kqueue() call returns a descriptor.
>
> Seán
> --
> sean-freebsd at farley.org
>
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
>
>


--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.


More information about the freebsd-hackers mailing list