< prev index next >

src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java

Print this page
*** 44,10 ***
--- 44,11 ---
  import java.nio.channels.WritableByteChannel;
  import java.util.Objects;
  
  import jdk.internal.access.JavaIOFileDescriptorAccess;
  import jdk.internal.access.SharedSecrets;
+ import jdk.internal.misc.Blocker;
  import jdk.internal.misc.ExtendedMapMode;
  import jdk.internal.misc.Unsafe;
  import jdk.internal.misc.VM;
  import jdk.internal.misc.VM.BufferPool;
  import jdk.internal.ref.Cleaner;

*** 227,11 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     n = IOUtil.read(fd, dst, -1, direct, alignment, nd);
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);
--- 228,16 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     if (Thread.currentThread().isVirtual()) {
+                         n = Blocker.managedBlock(() ->
+                                 IOUtil.read(fd, dst, -1, direct, alignment, nd));
+                     } else {
+                         n = IOUtil.read(fd, dst, -1, direct, alignment, nd);
+                     }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);

*** 256,12 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     n = IOUtil.read(fd, dsts, offset, length,
!                             direct, alignment, nd);
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);
--- 262,16 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     if (Thread.currentThread().isVirtual()) {
!                         n = Blocker.managedBlock(() ->
+                                 IOUtil.read(fd, dsts, offset, length, direct, alignment, nd));
+                     } else {
+                         n = IOUtil.read(fd, dsts, offset, length, direct, alignment, nd);
+                     }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);

*** 283,11 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     n = IOUtil.write(fd, src, -1, direct, alignment, nd);
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);
--- 293,16 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     if (Thread.currentThread().isVirtual()) {
+                         n = Blocker.managedBlock(() ->
+                                 IOUtil.write(fd, src, -1, direct, alignment, nd));
+                     } else {
+                         n = IOUtil.write(fd, src, -1, direct, alignment, nd);
+                     }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);

*** 312,12 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     n = IOUtil.write(fd, srcs, offset, length,
!                             direct, alignment, nd);
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);
--- 327,16 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     if (Thread.currentThread().isVirtual()) {
!                         n = Blocker.managedBlock(() ->
+                                 IOUtil.write(fd, srcs, offset, length, direct, alignment, nd));
+                     } else {
+                         n = IOUtil.write(fd, srcs, offset, length, direct, alignment, nd);
+                     }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
                  endBlocking(n > 0);

*** 326,10 ***
--- 345,37 ---
          }
      }
  
      // -- Other operations --
  
+     private long lockedSize() throws IOException {
+         assert Thread.holdsLock(positionLock);
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> nd.size(fd));
+         } else {
+             return nd.size(fd);
+         }
+     }
+ 
+     private long lockedSeek(long offset) throws IOException {
+         assert Thread.holdsLock(positionLock);
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> nd.seek(fd, offset));
+         } else {
+             return nd.seek(fd, offset);
+         }
+     }
+ 
+     private int lockedTruncate(long newSize) throws IOException {
+         assert Thread.holdsLock(positionLock);
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> nd.truncate(fd, newSize));
+         } else {
+             return nd.truncate(fd, newSize);
+         }
+     }
+ 
      public long position() throws IOException {
          ensureOpen();
          synchronized (positionLock) {
              long p = -1;
              int ti = -1;

*** 339,11 ***
                  if (!isOpen())
                      return 0;
                  boolean append = fdAccess.getAppend(fd);
                  do {
                      // in append-mode then position is advanced to end before writing
!                     p = (append) ? nd.size(fd) : nd.seek(fd, -1);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(p);
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);
--- 385,11 ---
                  if (!isOpen())
                      return 0;
                  boolean append = fdAccess.getAppend(fd);
                  do {
                      // in append-mode then position is advanced to end before writing
!                     p = (append) ? lockedSize() : lockedSeek(-1);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(p);
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);

*** 363,11 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return null;
                  do {
!                     p = nd.seek(fd, newPosition);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);
--- 409,11 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return null;
                  do {
!                     p = lockedSeek(newPosition);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);

*** 385,11 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return -1;
                  do {
!                     s = nd.size(fd);
                  } while ((s == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(s);
              } finally {
                  threads.remove(ti);
                  endBlocking(s > -1);
--- 431,11 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return -1;
                  do {
!                     s = lockedSize();
                  } while ((s == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(s);
              } finally {
                  threads.remove(ti);
                  endBlocking(s > -1);

*** 416,37 ***
                      return null;
  
                  // get current size
                  long size;
                  do {
!                     size = nd.size(fd);
                  } while ((size == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  // get current position
                  do {
!                     p = nd.seek(fd, -1);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
                  assert p >= 0;
  
                  // truncate file if given size is less than the current size
                  if (newSize < size) {
                      do {
!                         rv = nd.truncate(fd, newSize);
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  
                  // if position is beyond new size then adjust it
                  if (p > newSize)
                      p = newSize;
                  do {
!                     rp = nd.seek(fd, p);
                  } while ((rp == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(rv > -1);
--- 462,37 ---
                      return null;
  
                  // get current size
                  long size;
                  do {
!                     size = lockedSize();
                  } while ((size == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  // get current position
                  do {
!                     p = lockedSeek(-1);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
                  assert p >= 0;
  
                  // truncate file if given size is less than the current size
                  if (newSize < size) {
                      do {
!                         rv = lockedTruncate(newSize);
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  
                  // if position is beyond new size then adjust it
                  if (p > newSize)
                      p = newSize;
                  do {
!                     rp = lockedSeek(p);
                  } while ((rp == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(rv > -1);

*** 463,11 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return;
              do {
!                 rv = nd.force(fd, metaData);
              } while ((rv == IOStatus.INTERRUPTED) && isOpen());
          } finally {
              threads.remove(ti);
              endBlocking(rv > -1);
              assert IOStatus.check(rv);
--- 509,15 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return;
              do {
!                 if (Thread.currentThread().isVirtual()) {
+                     rv = Blocker.managedBlock(() -> nd.force(fd, metaData));
+                 } else {
+                     rv = nd.force(fd, metaData);
+                 }
              } while ((rv == IOStatus.INTERRUPTED) && isOpen());
          } finally {
              threads.remove(ti);
              endBlocking(rv > -1);
              assert IOStatus.check(rv);

*** 503,11 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 n = transferTo0(fd, position, icount, targetFD);
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              if (n == IOStatus.UNSUPPORTED_CASE) {
                  if (target instanceof SinkChannelImpl)
                      pipeSupported = false;
                  if (target instanceof FileChannelImpl)
--- 553,15 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 if (Thread.currentThread().isVirtual()) {
+                     n = Blocker.managedBlock(() -> transferTo0(fd, position, icount, targetFD));
+                 } else {
+                     n = transferTo0(fd, position, icount, targetFD);
+                 }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              if (n == IOStatus.UNSUPPORTED_CASE) {
                  if (target instanceof SinkChannelImpl)
                      pipeSupported = false;
                  if (target instanceof FileChannelImpl)

*** 834,11 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 n = IOUtil.read(fd, dst, position, direct, alignment, nd);
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);
              endBlocking(n > 0);
--- 888,16 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 if (Thread.currentThread().isVirtual()) {
+                     n = Blocker.managedBlock(() ->
+                             IOUtil.read(fd, dst, position, direct, alignment, nd));
+                 } else {
+                     n = IOUtil.read(fd, dst, position, direct, alignment, nd);
+                 }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);
              endBlocking(n > 0);

*** 873,11 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 n = IOUtil.write(fd, src, position, direct, alignment, nd);
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);
              endBlocking(n > 0);
--- 932,16 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 if (Thread.currentThread().isVirtual()) {
+                     n = Blocker.managedBlock(() ->
+                             IOUtil.write(fd, src, position, direct, alignment, nd));
+                 } else {
+                     n = IOUtil.write(fd, src, position, direct, alignment, nd);
+                 }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);
              endBlocking(n > 0);

*** 1081,11 ***
              long mapSize;
              int pagePosition;
              synchronized (positionLock) {
                  long filesize;
                  do {
!                     filesize = nd.size(fd);
                  } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  if (filesize < position + size) { // Extend file size
--- 1145,11 ---
              long mapSize;
              int pagePosition;
              synchronized (positionLock) {
                  long filesize;
                  do {
!                     filesize = lockedSize();
                  } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  if (filesize < position + size) { // Extend file size

*** 1093,11 ***
                          throw new IOException("Channel not open for writing " +
                              "- cannot extend file to required size");
                      }
                      int rv;
                      do {
!                         rv = nd.truncate(fd, position + size);
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  
--- 1157,11 ---
                          throw new IOException("Channel not open for writing " +
                              "- cannot extend file to required size");
                      }
                      int rv;
                      do {
!                         rv = lockedTruncate(position + size);
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  

*** 1281,11 ***
              ti = threads.add();
              if (!isOpen())
                  return null;
              int n;
              do {
!                 n = nd.lock(fd, true, position, size, shared);
              } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
              if (isOpen()) {
                  if (n == FileDispatcher.RET_EX_LOCK) {
                      assert shared;
                      FileLockImpl fli2 = new FileLockImpl(this, position, size,
--- 1345,15 ---
              ti = threads.add();
              if (!isOpen())
                  return null;
              int n;
              do {
!                 if (Thread.currentThread().isVirtual()) {
+                     n = Blocker.managedBlock(() -> nd.lock(fd, true, position, size, shared));
+                 } else {
+                     n = nd.lock(fd, true, position, size, shared);
+                 }
              } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
              if (isOpen()) {
                  if (n == FileDispatcher.RET_EX_LOCK) {
                      assert shared;
                      FileLockImpl fli2 = new FileLockImpl(this, position, size,
< prev index next >