< prev index next >

src/java.base/share/classes/java/net/InetAddress.java

Print this page
*** 43,13 ***
--- 43,15 ---
  import java.util.concurrent.ConcurrentHashMap;
  import java.util.concurrent.ConcurrentMap;
  import java.util.concurrent.ConcurrentSkipListSet;
  import java.util.concurrent.atomic.AtomicLong;
  import java.util.Arrays;
+ import java.util.concurrent.locks.ReentrantLock;
  
  import jdk.internal.access.JavaNetInetAddressAccess;
  import jdk.internal.access.SharedSecrets;
+ import jdk.internal.misc.Blocker;
  import sun.security.action.*;
  import sun.net.InetAddressCachePolicy;
  import sun.net.util.IPAddressUtil;
  import sun.nio.cs.UTF_8;
  

*** 820,10 ***
--- 822,11 ---
      // a name service lookup based Addresses implementation which replaces itself
      // in cache when the result is obtained
      private static final class NameServiceAddresses implements Addresses {
          private final String host;
          private final InetAddress reqAddr;
+         private final ReentrantLock lookupLock = new ReentrantLock();
  
          NameServiceAddresses(String host, InetAddress reqAddr) {
              this.host = host;
              this.reqAddr = reqAddr;
          }

*** 831,11 ***
          @Override
          public InetAddress[] get() throws UnknownHostException {
              Addresses addresses;
              // only one thread is doing lookup to name service
              // for particular host at any time.
!             synchronized (this) {
                  // re-check that we are still us + re-install us if slot empty
                  addresses = cache.putIfAbsent(host, this);
                  if (addresses == null) {
                      // this can happen when we were replaced by CachedAddresses in
                      // some other thread, then CachedAddresses expired and were
--- 834,12 ---
          @Override
          public InetAddress[] get() throws UnknownHostException {
              Addresses addresses;
              // only one thread is doing lookup to name service
              // for particular host at any time.
!             lookupLock.lock();
+             try {
                  // re-check that we are still us + re-install us if slot empty
                  addresses = cache.putIfAbsent(host, this);
                  if (addresses == null) {
                      // this can happen when we were replaced by CachedAddresses in
                      // some other thread, then CachedAddresses expired and were

*** 879,10 ***
--- 883,12 ---
                          throw ex == null ? new UnknownHostException(host) : ex;
                      }
                      return inetAddresses;
                  }
                  // else addresses != this
+             } finally {
+                 lookupLock.unlock();
              }
              // delegate to different addresses when we are already replaced
              // but outside of synchronized block to avoid any chance of dead-locking
              return addresses.get();
          }

*** 928,17 ***
      private static final class PlatformNameService implements NameService {
  
          public InetAddress[] lookupAllHostAddr(String host)
              throws UnknownHostException
          {
!             return impl.lookupAllHostAddr(host);
          }
  
          public String getHostByAddr(byte[] addr)
              throws UnknownHostException
          {
!             return impl.getHostByAddr(addr);
          }
      }
  
      /**
       * The HostsFileNameService provides host address mapping
--- 934,25 ---
      private static final class PlatformNameService implements NameService {
  
          public InetAddress[] lookupAllHostAddr(String host)
              throws UnknownHostException
          {
!             if (Thread.currentThread().isVirtual()) {
+                 return Blocker.managedBlock(() -> impl.lookupAllHostAddr(host));
+             } else {
+                 return impl.lookupAllHostAddr(host);
+             }
          }
  
          public String getHostByAddr(byte[] addr)
              throws UnknownHostException
          {
!             if (Thread.currentThread().isVirtual()) {
+                 return Blocker.managedBlock(() ->  impl.getHostByAddr(addr));
+             } else {
+                 return impl.getHostByAddr(addr);
+             }
          }
      }
  
      /**
       * The HostsFileNameService provides host address mapping
< prev index next >