From owner-freebsd-java@FreeBSD.ORG Mon Jul 10 12:39:02 2006 Return-Path: X-Original-To: java@freebsd.org Delivered-To: freebsd-java@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0E5AF16A4E2 for ; Mon, 10 Jul 2006 12:39:02 +0000 (UTC) (envelope-from arnej@yahoo-inc.com) Received: from dev-arnej.trondheim.corp.yahoo.com (pat-gw.trondheim.corp.yahoo.com [217.144.236.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7796843D67 for ; Mon, 10 Jul 2006 12:38:58 +0000 (GMT) (envelope-from arnej@yahoo-inc.com) Received: (from arnej@localhost) by dev-arnej.trondheim.corp.yahoo.com (8.12.11/8.12.11) id k6ACcu0N039611; Mon, 10 Jul 2006 05:38:56 -0700 (PDT) (envelope-from arnej) Date: Mon, 10 Jul 2006 05:38:55 -0700 (PDT) From: Arne Juul X-X-Sender: arnej@dev-arnej.trondheim.corp.yahoo.com To: Landon Fuller In-Reply-To: <6E6948AF-73E6-4CFE-AA43-CD3508C009AA@opendarwin.org> Message-ID: <20060710053458.F37375@dev-arnej.trondheim.corp.yahoo.com> References: <20060626172546.P80831@dev-arnej.trondheim.corp.yahoo.com> <6E6948AF-73E6-4CFE-AA43-CD3508C009AA@opendarwin.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Cc: java@freebsd.org Subject: Re: non-threadsafe InetAddress.getHostName() X-BeenThere: freebsd-java@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting Java to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Jul 2006 12:39:02 -0000 On Fri, 30 Jun 2006, Landon Fuller wrote: > On Jun 26, 2006, at 17:38, Arne Juul wrote: > > > This happens on FreeBSD with jdk 1.4.2p8_2 at least. > What release of FreeBSD? It only happens when compiling on FreeBSD 4 (and probably non-FreeBSD builds). Now I've found that the JDK 1.5 patches already had this fixed, and the following backport patch fixes it for 1.4.2 as well. I guess this means the first XXXBSD comment can be removed, the mutex does work "as expected" :-) --- ../../j2se/src/solaris/native/java/net/Inet4AddressImpl.c.old Tue Jun 27 02:55:32 2006 +++ ../../j2se/src/solaris/native/java/net/Inet4AddressImpl.c Tue Jun 27 02:55:35 2006 @@ -13,6 +13,7 @@ #include #include #include +#include #if defined(_ALLBSD_SOURCE) #include #if defined(__FreeBSD__) && __FreeBSD__ > 4 @@ -312,6 +313,34 @@ struct hostent *hptr, HOST_R_ARGS); +/* + * BSDNOTE: Since we are emulating thread-safe functions, we need to + * ensure they're not running in parallel. So, setup mutexes + * + * XXXBSD: make sure it works as expected + * XXXBSD: move initialization to somethere earlier + */ + +pthread_mutex_t _net_mutex = PTHREAD_MUTEX_INITIALIZER; +static int _net_mutex_inited = 0; + +static int +_acquire_net_mutex() +{ + if (_net_mutex_inited == 0) { + if (pthread_mutex_init(&_net_mutex, NULL)) + return (EINVAL); + _net_mutex_inited = 1; + } + return (pthread_mutex_lock(&_net_mutex)); +} + +static int +_release_net_mutex() +{ + return (pthread_mutex_unlock(&_net_mutex)); +} + #endif /************************************************************************ @@ -355,15 +384,19 @@ #ifdef __GLIBC__ gethostbyname_r(hostname, &res, buf, sizeof(buf), &hp, &h_error); #else + _acquire_net_mutex(); hp = JDK_gethostbyname_r(hostname, &res, buf, sizeof(buf), &h_error); + _release_net_mutex(); #endif if (hp) { #ifdef __GLIBC__ gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET, &res2, buf2, sizeof(buf2), &hp, &h_error); #else + _acquire_net_mutex(); hp = JDK_gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET, &res2, buf2, sizeof(buf2), &h_error); + _release_net_mutex(); #endif if (hp) { /* @@ -427,8 +460,10 @@ /* Try once, with our static buffer. */ #ifdef __GLIBC__ gethostbyname_r(hostname, &res, buf, sizeof(buf), &hp, &h_error); -#else +#else + _acquire_net_mutex(); hp = JDK_gethostbyname_r(hostname, &res, buf, sizeof(buf), &h_error); + _release_net_mutex(); #endif /* With the re-entrant system calls, it's possible that the buffer @@ -442,8 +477,10 @@ gethostbyname_r(hostname, &res, tmp, BIG_HENT_BUF_SIZE, &hp, &h_error); #else + _acquire_net_mutex(); hp = JDK_gethostbyname_r(hostname, &res, tmp, BIG_HENT_BUF_SIZE, &h_error); + _release_net_mutex(); #endif } } @@ -531,8 +568,10 @@ gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &h_error); #else + _acquire_net_mutex(); hp = JDK_gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &h_error); + _release_net_mutex(); #endif /* With the re-entrant system calls, it's possible that the buffer * we pass to it is not large enough to hold an exceptionally @@ -545,8 +584,10 @@ gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent, tmp, BIG_HENT_BUF_SIZE, &hp, &h_error); #else + _acquire_net_mutex(); hp = JDK_gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent, tmp, BIG_HENT_BUF_SIZE, &h_error); + _release_net_mutex(); #endif } else { JNU_ThrowOutOfMemoryError(env, "getHostByAddr");