cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: [PATCH, RFC] Make hostthre.c work on POSIX

From: Jamie Lokier <jamie_at_shareable.org>
Date: Fri, 25 Sep 2009 18:22:19 +0100

Joshua Kwan wrote:
> I've reworked the configure script
> so that it will choose threaded resolve when pthreads are available but
> c-ares is not. I've also regression tested the whole thing on Windows XP
> and above.

In principle getaddrinfo's specification requires it to be thread safe.

On Windows, I think gethostbyname is thread safe too (required for pre-XP).
        
But I believe that will go horribly wrong on:

    - FreeBSD before 5.3
    - Some versions of Mac OS X / Darwin (I'm not sure when)
    - NetBSD, OpenBSD, DragonflyBSD, at least some older versions.

It's because all the free BSDs had thread-unsafe versions of
getaddrinfo, getnameinfo and the other DNS functions for a time.

One way to confirm a thread-safe DNS implementation _might_ be to
check if h_errno is a macro. If it's a macro, it's probably using
thread-local storage (similar to the errno macro).

I have something like this in my libraries, which is based on reading
things over the net and BSD source code, but I haven't tested
if the macro test is still correct on the latest versions:

/* `getaddrinfo' and `getnameinfo' should be thread-safe because that's
   part of their specification. However, all the free BSDs have had
   thread-unsafe implementations of these functions, and the same is
   true of `getipnodebyname' and `getipnodebyaddr'.

   This is fixed in FreeBSD 5.3-RELEASE, and some version of Darwin:
   thread-local storage is used, to make them thread safe and
   concurrent. Rather than testing versions, it seems reliable to test
   whether `h_errno' is a macro (and hence, using thread-local storage). */
#undef THREAD_UNSAFE_REENTRANT
#if defined (__NetBSD__) || defined (__OpenBSD__) || defined (__FreeBSD__) \
    || defined (__DragonFly__) || defined (__DARWIN__) || defined (__APPLE__)
# ifndef h_errno
/* The supposedly reentrant APIs getaddrinfo etc. are not thread safe. */
# define THREAD_UNSAFE_REENTRANT 1
# endif
#endif

Conversely, on Windows you can use gethostbyname in a thread-safe way
too. (The FIXME is that I haven't checked Cygwin, and haven't got
the HP-UX 11 test in there):

/* FIXME: `gethostbyname' and `gethostbyaddr' are reportedly thread-safe
   on 32/64-bit Windows, AIX 4.3, and HP-UX 11. The API's static buffer
   uses thread-local storage. It's definitely thread-safe on WIN32,
   but not necessarily on Cygwin, we haven't checked. */
#undef THREAD_SAFE_NON_REENTRANT
#if HAVE_WIN32_NATIVE || defined (_AIX43)
/* The supposedly non-reentrant APIs gethostbyname etc. are thread safe. */
# define THREAD_SAFE_NON_REENTRANT 1
#endif

-- Jamie
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2009-09-25