Mixing Asynchronous Network I/O and POSIX Threads

Jilles Tjoelker jilles at stack.nl
Sun Sep 18 19:44:10 UTC 2011


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


More information about the freebsd-hackers mailing list