From owner-freebsd-hackers@FreeBSD.ORG Sun Sep 18 19:44:10 2011 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D01B2106566C for ; Sun, 18 Sep 2011 19:44:10 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) by mx1.freebsd.org (Postfix) with ESMTP id 6599F8FC08 for ; Sun, 18 Sep 2011 19:44:10 +0000 (UTC) Received: from turtle.stack.nl (turtle.stack.nl [IPv6:2001:610:1108:5010::132]) by mx1.stack.nl (Postfix) with ESMTP id B10121DD753; Sun, 18 Sep 2011 21:44:09 +0200 (CEST) Received: by turtle.stack.nl (Postfix, from userid 1677) id A823D17515; Sun, 18 Sep 2011 21:44:09 +0200 (CEST) Date: Sun, 18 Sep 2011 21:44:09 +0200 From: Jilles Tjoelker To: Richard Yao Message-ID: <20110918194409.GB36162@stack.nl> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Cc: hackers@freebsd.org Subject: Re: Mixing Asynchronous Network I/O and POSIX Threads X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 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 Sep 2011 19:44:10 -0000 On Sun, Sep 18, 2011 at 11:32:08AM -0400, Richard Yao wrote: > I wrote a program for Linux that uses Asynchronous Network I/O and > POSIX Threads. It uses a mix of gettid(), fcntl() and F_SETOWN to > specify which thread handles each connection's SIGIO interrupts. > gettid() is Linux-specific and I would prefer to do this in a way that > also works with FreeBSD. Is that possible? In FreeBSD, you can only F_SETOWN processes or process groups, not threads, so a direct port is probably not possible. Another Linux feature not in FreeBSD is F_SETSIG (to send a realtime signal instead of SIGIO and provide siginfo_t information). My recommendation is not to use SIGIO and SIGURG at all. If you use signal handlers, your program is very likely to suffer from race condition problems unless you unmask the signal only at well-defined safe points. If you use sigtimedwait() or similar, select()/poll() can provide similar functionality. Alternatively, if you have many descriptors to watch, use non-portable facilities like BSD kqueue, Linux epoll, Solaris ports or portable wrappers around them. Realtime signals are nasty for this -- the signal queue has a finite size and when it overflows you need a "resync" step that is expensive both in CPU and programmer time. -- Jilles Tjoelker