From owner-freebsd-hackers@FreeBSD.ORG Sun Sep 18 21:45:27 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 6FAE5106566B for ; Sun, 18 Sep 2011 21:45:27 +0000 (UTC) (envelope-from shiningarcanine@gmail.com) Received: from mail-iy0-f182.google.com (mail-iy0-f182.google.com [209.85.210.182]) by mx1.freebsd.org (Postfix) with ESMTP id 3B0D68FC0A for ; Sun, 18 Sep 2011 21:45:27 +0000 (UTC) Received: by iadk27 with SMTP id k27so7273650iad.13 for ; Sun, 18 Sep 2011 14:45:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=vK+H5Q/9QczvtF7LJqKrx5m7sI5n0Tx4wB9VTI67HPw=; b=gs17ftwWQMTyBRjwS/wNFH6tQE819/bw4JMQTr5zzShx++2/ghNK2DKaMQaD4yNjVJ Kt3q6nSWH/qnH/dgquriTdKrjEn6R173kzSbPRXzm0OdrAOqZRzprLLJQHxHlzxIqgAQ taIYCYN3EbxP2M58ksJM2+qnaYppzAMXgKU7w= MIME-Version: 1.0 Received: by 10.42.159.201 with SMTP id m9mr3357908icx.10.1316382326752; Sun, 18 Sep 2011 14:45:26 -0700 (PDT) Sender: shiningarcanine@gmail.com Received: by 10.42.219.201 with HTTP; Sun, 18 Sep 2011 14:45:26 -0700 (PDT) In-Reply-To: References: Date: Sun, 18 Sep 2011 17:45:26 -0400 X-Google-Sender-Auth: y3fT0oPxOSlkIKi69FiF88L9ywE Message-ID: From: Richard Yao To: Jilles Tjoelker Content-Type: text/plain; charset=ISO-8859-1 X-Mailman-Approved-At: Sun, 18 Sep 2011 23:57:43 +0000 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 21:45:27 -0000 Dear Jilles, I am using sigwaitinfo() with all interrupts masked to avoid the possibility of race conditions in signal handlers, but I have not used any realtime signals. Linux 2.6.35 found a way to invoke the SIGIO handler despite it being masked, but that issue would not occur under production conditions and that is a bug for a different mailing list. Being unable to F_SETOWN individual threads would cause problems because it causes network traffic to become serialized. My code must run on Linux, but if I were to write FreeBSD-specific code so it would also work on FreeBSD, would using kqueue enable me to specify which threads handle events on specific file descriptors? Yours truly, Richard Yao On Sun, Sep 18, 2011 at 3:44 PM, Jilles Tjoelker wrote: > 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 >