Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Jul 2004 23:49:47 -0700
From:      Paul Querna <chip@force-elite.com>
To:        freebsd-hackers@freebsd.org
Subject:   KQueue and Threads?
Message-ID:  <1090133387.16090.45.camel@localhost>

next in thread | raw e-mail | index | archive | help
Hello,

I have a question about using KQueue() in multi-threaded situations. A
couple months ago I wrote the KQueue support for Apache2/APR. I am now
investigating a pseudo Event/Worker MPM to better handle KeepAlive
Requests. (support for KQueue in Apache is available in our CVS HEAD and
via an experimental patch in the FreeBSD Ports...  Feedback would be
nice.)

For my example, say I have two threads.

Thread A is in a kevent() waiting for events to happen on a set of FDs.
Thread B has an FD that it would like to add to the KQueue that Thread A
is using.  

What is the best way to get that FD into the KQueue, avoiding as much
context switching as possible?  

Is it possible for Thread B to just call kevent w/ EV_ADD and Thread A
will get proper notifications from that FD in the changed KQueue?  

I know this is possible with Linux's sys_epoll() implementation. Other
threads can just add FDs to the epoll, and the thread waiting in the
epoll will get notified of any activity on those new FDs, just like the
existing FDs. 

If this is not possible with KQueue, I guess the best alternative is to
have a pipe in the Set that Thread A is waiting on.  Other threads will
add their FD to a Queue, and then write to this pipe. That will awaken
thread A, which then can add any  FDs to it's pollset.  That is less
than ideal on a very busy server.  

An alternative is to set the timeout in Thread A very low.  Say every
1/10 of a second, it would timeout, and add any outstanding FDs to the
KQueue.

Opinions?

Thanks,
-Paul Querna





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1090133387.16090.45.camel>