< prev index next >

src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java

Print this page
@@ -23,10 +23,11 @@
   * questions.
   */
  
  package sun.nio.fs;
  
+ import jdk.internal.misc.Blocker;
  import jdk.internal.misc.Unsafe;
  
  import static sun.nio.fs.WindowsConstants.*;
  
  /**

@@ -64,20 +65,27 @@
                             long lpSecurityAttributes,
                             int dwCreationDisposition,
                             int dwFlagsAndAttributes)
          throws WindowsException
      {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             return CreateFile0(buffer.address(),
-                                dwDesiredAccess,
-                                dwShareMode,
-                                lpSecurityAttributes,
-                                dwCreationDisposition,
-                                dwFlagsAndAttributes);
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 return Blocker.managedBlock(() ->
+                         CreateFile0(buffer.address(),
+                                     dwDesiredAccess,
+                                     dwShareMode,
+                                     lpSecurityAttributes,
+                                     dwCreationDisposition,
+                                     dwFlagsAndAttributes));
+             } else {
+                 return CreateFile0(buffer.address(),
+                                    dwDesiredAccess,
+                                    dwShareMode,
+                                    lpSecurityAttributes,
+                                    dwCreationDisposition,
+                                    dwFlagsAndAttributes);
+             }
          }
      }
      static long CreateFile(String path,
                             int dwDesiredAccess,
                             int dwShareMode,

@@ -107,15 +115,16 @@
       * DeleteFile(
       *   LPCTSTR lpFileName
       * )
       */
      static void DeleteFile(String path) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             DeleteFile0(buffer.address());
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> DeleteFile0(buffer.address()));
+             } else {
+                 DeleteFile0(buffer.address());
+             }
          }
      }
      private static native void DeleteFile0(long lpFileName)
          throws WindowsException;
  

@@ -124,15 +133,16 @@
       *   LPCTSTR lpPathName,
       *   LPSECURITY_ATTRIBUTES lpSecurityAttributes
       * )
       */
      static void CreateDirectory(String path, long lpSecurityAttributes) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             CreateDirectory0(buffer.address(), lpSecurityAttributes);
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> CreateDirectory0(buffer.address(), lpSecurityAttributes));
+             } else {
+                 CreateDirectory0(buffer.address(), lpSecurityAttributes);
+             }
          }
      }
      private static native void CreateDirectory0(long lpFileName, long lpSecurityAttributes)
          throws WindowsException;
  

@@ -140,15 +150,16 @@
       * RemoveDirectory(
       *   LPCTSTR lpPathName
       * )
       */
      static void RemoveDirectory(String path) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             RemoveDirectory0(buffer.address());
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> RemoveDirectory0(buffer.address()));
+             } else {
+                 RemoveDirectory0(buffer.address());
+             }
          }
      }
      private static native void RemoveDirectory0(long lpFileName)
          throws WindowsException;
  

@@ -187,17 +198,18 @@
       *   LPCTSTR lpFileName,
       *   LPWIN32_FIND_DATA lpFindFileData
       * )
       */
      static FirstFile FindFirstFile(String path) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
              FirstFile data = new FirstFile();
-             FindFirstFile0(buffer.address(), data);
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> FindFirstFile0(buffer.address(), data));
+             } else {
+                 FindFirstFile0(buffer.address(), data);
+             }
              return data;
-         } finally {
-             buffer.release();
          }
      }
      static class FirstFile {
          private long handle;
          private String name;

@@ -216,15 +228,16 @@
       *   LPCTSTR lpFileName,
       *   LPWIN32_FIND_DATA lpFindFileData
       * )
       */
      static long FindFirstFile(String path, long address) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             return FindFirstFile1(buffer.address(), address);
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 return Blocker.managedBlock(() -> FindFirstFile1(buffer.address(), address));
+             } else {
+                 return FindFirstFile1(buffer.address(), address);
+             }
          }
      }
      private static native long FindFirstFile1(long lpFileName, long address)
          throws WindowsException;
  

@@ -234,11 +247,18 @@
       *   LPWIN32_FIND_DATA lpFindFileData
       * )
       *
       * @return  lpFindFileData->cFileName or null
       */
-     static native String FindNextFile(long handle, long address)
+     static String FindNextFile(long handle, long address) throws WindowsException {
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> FindNextFile0(handle, address));
+         } else {
+             return FindNextFile0(handle, address);
+         }
+     }
+     private static native String FindNextFile0(long handle, long address)
          throws WindowsException;
  
      /**
       * HANDLE FindFirstStreamW(
       *   LPCWSTR lpFileName,

@@ -246,19 +266,20 @@
       *   LPVOID lpFindStreamData,
       *   DWORD dwFlags
       * )
       */
      static FirstStream FindFirstStream(String path) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
              FirstStream data = new FirstStream();
-             FindFirstStream0(buffer.address(), data);
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> FindFirstStream0(buffer.address(), data));
+             } else {
+                 FindFirstStream0(buffer.address(), data);
+             }
              if (data.handle() == WindowsConstants.INVALID_HANDLE_VALUE)
                  return null;
              return data;
-         } finally {
-             buffer.release();
          }
      }
      static class FirstStream {
          private long handle;
          private String name;

@@ -274,11 +295,18 @@
       * FindNextStreamW(
       *   HANDLE hFindStream,
       *   LPVOID lpFindStreamData
       * )
       */
-     static native String FindNextStream(long handle) throws WindowsException;
+     static String FindNextStream(long handle) throws WindowsException {
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> FindNextStream0(handle));
+         } else {
+             return FindNextStream0(handle);
+         }
+     }
+     private static native String FindNextStream0(long handle) throws WindowsException;
  
      /**
       * FindClose(
       *   HANDLE hFindFile
       * )

@@ -289,11 +317,20 @@
       * GetFileInformationByHandle(
       *   HANDLE hFile,
       *   LPBY_HANDLE_FILE_INFORMATION lpFileInformation
       * )
       */
-     static native void GetFileInformationByHandle(long handle, long address)
+     static void GetFileInformationByHandle(long handle, long address)
+         throws WindowsException
+     {
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> GetFileInformationByHandle0(handle, address));
+         } else {
+             GetFileInformationByHandle0(handle, address);
+         }
+     }
+     private static native void GetFileInformationByHandle0(long handle, long address)
          throws WindowsException;
  
      /**
       * CopyFileEx(
       *   LPCWSTR lpExistingFileName

@@ -306,18 +343,20 @@
       */
      static void CopyFileEx(String source, String target, int flags,
                             long addressToPollForCancel)
          throws WindowsException
      {
-         NativeBuffer sourceBuffer = asNativeBuffer(source);
-         NativeBuffer targetBuffer = asNativeBuffer(target);
-         try {
-             CopyFileEx0(sourceBuffer.address(), targetBuffer.address(), flags,
-                         addressToPollForCancel);
-         } finally {
-             targetBuffer.release();
-             sourceBuffer.release();
+         try (NativeBuffer sourceBuffer = asNativeBuffer(source);
+              NativeBuffer targetBuffer = asNativeBuffer(target)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() ->
+                     CopyFileEx0(sourceBuffer.address(), targetBuffer.address(), flags,
+                                 addressToPollForCancel));
+             } else {
+                 CopyFileEx0(sourceBuffer.address(), targetBuffer.address(), flags,
+                             addressToPollForCancel);
+             }
          }
      }
      private static native void CopyFileEx0(long existingAddress, long newAddress,
          int flags, long addressToPollForCancel) throws WindowsException;
  

@@ -329,17 +368,18 @@
       * )
       */
      static void MoveFileEx(String source, String target, int flags)
          throws WindowsException
      {
-         NativeBuffer sourceBuffer = asNativeBuffer(source);
-         NativeBuffer targetBuffer = asNativeBuffer(target);
-         try {
-             MoveFileEx0(sourceBuffer.address(), targetBuffer.address(), flags);
-         } finally {
-             targetBuffer.release();
-             sourceBuffer.release();
+         try (NativeBuffer sourceBuffer = asNativeBuffer(source);
+              NativeBuffer targetBuffer = asNativeBuffer(target)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() ->
+                     MoveFileEx0(sourceBuffer.address(), targetBuffer.address(), flags));
+             } else {
+                 MoveFileEx0(sourceBuffer.address(), targetBuffer.address(), flags);
+             }
          }
      }
      private static native void MoveFileEx0(long existingAddress, long newAddress,
          int flags) throws WindowsException;
  

@@ -347,15 +387,16 @@
       * DWORD GetFileAttributes(
       *   LPCTSTR lpFileName
       * )
       */
      static int GetFileAttributes(String path) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             return GetFileAttributes0(buffer.address());
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 return Blocker.managedBlock(() -> GetFileAttributes0(buffer.address()));
+             } else {
+                 return GetFileAttributes0(buffer.address());
+             }
          }
      }
      private static native int GetFileAttributes0(long lpFileName)
          throws WindowsException;
  

@@ -365,15 +406,16 @@
       *   DWORD dwFileAttributes
       */
      static void SetFileAttributes(String path, int dwFileAttributes)
          throws WindowsException
      {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             SetFileAttributes0(buffer.address(), dwFileAttributes);
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> SetFileAttributes0(buffer.address(), dwFileAttributes));
+             } else {
+                 SetFileAttributes0(buffer.address(), dwFileAttributes);
+             }
          }
      }
      private static native void SetFileAttributes0(long lpFileName,
          int dwFileAttributes) throws WindowsException;
  

@@ -383,31 +425,42 @@
       *   GET_FILEEX_INFO_LEVELS fInfoLevelId,
       *   LPVOID lpFileInformation
       * );
       */
      static void GetFileAttributesEx(String path, long address) throws WindowsException {
-         NativeBuffer buffer = asNativeBuffer(path);
-         try {
-             GetFileAttributesEx0(buffer.address(), address);
-         } finally {
-             buffer.release();
+         try (NativeBuffer buffer = asNativeBuffer(path)) {
+             if (Thread.currentThread().isVirtual()) {
+                 Blocker.managedBlock(() -> GetFileAttributesEx0(buffer.address(), address));
+             } else {
+                 GetFileAttributesEx0(buffer.address(), address);
+             }
          }
      }
      private static native void GetFileAttributesEx0(long lpFileName, long address)
          throws WindowsException;
+ 
      /**
       * SetFileTime(
       *   HANDLE hFile,
       *   CONST FILETIME *lpCreationTime,
       *   CONST FILETIME *lpLastAccessTime,
       *   CONST FILETIME *lpLastWriteTime
       * )
       */
-     static native void SetFileTime(long handle,
-                                    long createTime,
-                                    long lastAccessTime,
-                                    long lastWriteTime)
+     static void SetFileTime(long handle, long createTime, long lastAccessTime, long lastWriteTime)
+         throws WindowsException
+     {
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> SetFileTime0(handle, createTime, lastAccessTime, lastWriteTime));
+         } else {
+             SetFileTime0(handle, createTime, lastAccessTime, lastWriteTime);
+         }
+     }
+     private static native void SetFileTime0(long handle,
+                                             long createTime,
+                                             long lastAccessTime,
+                                             long lastWriteTime)
          throws WindowsException;
  
      /**
       * SetEndOfFile(
       *   HANDLE hFile

@@ -618,12 +671,12 @@
                                  long pSecurityDescriptor)
          throws WindowsException
      {
          NativeBuffer buffer = asNativeBuffer(path);
          try {
-             SetFileSecurity0(buffer.address(), securityInformation,
-                 pSecurityDescriptor);
+             // may be called with elevated privileges so always run on current thread
+             SetFileSecurity0(buffer.address(), securityInformation, pSecurityDescriptor);
          } finally {
              buffer.release();
          }
      }
      static native void SetFileSecurity0(long lpFileName, int securityInformation,
< prev index next >