From owner-freebsd-hackers@FreeBSD.ORG Sun Jul 18 06:49:50 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 21AA016A4CE for ; Sun, 18 Jul 2004 06:49:50 +0000 (GMT) Received: from utopia.in.force-elite.com (force-elite.com [216.255.199.145]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9DEB643D55 for ; Sun, 18 Jul 2004 06:49:47 +0000 (GMT) (envelope-from chip@force-elite.com) X-AuthUser: chip@force-elite.com Received: from [10.0.0.22] (10.0.0.22:34966)Server] ; Sat, 17 Jul 2004 23:49:47 -0700 From: Paul Querna To: freebsd-hackers@freebsd.org Content-Type: text/plain Date: Sat, 17 Jul 2004 23:49:47 -0700 Message-Id: <1090133387.16090.45.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 1.5.90 Content-Transfer-Encoding: 7bit Subject: KQueue and Threads? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 18 Jul 2004 06:49:50 -0000 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