Cache native thread id to avoid call to JNI in blocking I/O ops niosocketimpl-branch
authoralanb
Tue, 05 Feb 2019 18:53:19 +0000
branchniosocketimpl-branch
changeset 57148 1a13164f3f4f
parent 57128 3d6cee596b33
child 57164 ed6f6950c757
Cache native thread id to avoid call to JNI in blocking I/O ops
src/java.base/share/classes/java/lang/System.java
src/java.base/share/classes/java/lang/Thread.java
src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java
src/java.base/unix/classes/sun/nio/ch/NativeThread.java
src/java.base/unix/native/libnio/ch/NativeThread.c
--- a/src/java.base/share/classes/java/lang/System.java	Thu Jan 31 09:01:10 2019 +0000
+++ b/src/java.base/share/classes/java/lang/System.java	Tue Feb 05 18:53:19 2019 +0000
@@ -2172,6 +2172,12 @@
             public void blockedOn(Interruptible b) {
                 Thread.blockedOn(b);
             }
+            public void setNativeTid(long tid) {
+                Thread.setNativeTid(tid);
+            }
+            public long nativeTid() {
+                return Thread.nativeTid();
+            }
             public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
                 Shutdown.add(slot, registerShutdownInProgress, hook);
             }
--- a/src/java.base/share/classes/java/lang/Thread.java	Thu Jan 31 09:01:10 2019 +0000
+++ b/src/java.base/share/classes/java/lang/Thread.java	Tue Feb 05 18:53:19 2019 +0000
@@ -241,6 +241,18 @@
     }
 
     /**
+     * Native thread id, cached here for use for threads are blocked in I/O
+     * operations.
+     */
+    private long nativeTid;
+    static void setNativeTid(long tid) {
+        Thread.currentThread().nativeTid = tid;
+    }
+    static long nativeTid() {
+        return Thread.currentThread().nativeTid;
+    }
+
+    /**
      * The minimum priority that a thread can have.
      */
     public static final int MIN_PRIORITY = 1;
--- a/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java	Thu Jan 31 09:01:10 2019 +0000
+++ b/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java	Tue Feb 05 18:53:19 2019 +0000
@@ -107,6 +107,16 @@
     void blockedOn(Interruptible b);
 
     /**
+     * Set the current thread's native ID
+     */
+    void setNativeTid(long tid);
+
+    /**
+     * Returns the current thread's native ID
+     */
+    long nativeTid();
+
+    /**
      * Registers a shutdown hook.
      *
      * It is expected that this method with registerShutdownInProgress=true
--- a/src/java.base/unix/classes/sun/nio/ch/NativeThread.java	Thu Jan 31 09:01:10 2019 +0000
+++ b/src/java.base/unix/classes/sun/nio/ch/NativeThread.java	Tue Feb 05 18:53:19 2019 +0000
@@ -25,7 +25,6 @@
 
 package sun.nio.ch;
 
-
 // Signalling operations on native threads
 //
 // On some operating systems (e.g., Linux), closing a channel while another
@@ -33,23 +32,35 @@
 // thread to be released.  This class provides access to the native threads
 // upon which Java threads are built, and defines a simple signal mechanism
 // that can be used to release a native thread from a blocking I/O operation.
-// On systems that do not require this type of signalling, the current() method
-// always returns -1 and the signal(long) method has no effect.
 
+import jdk.internal.access.JavaLangAccess;
+import jdk.internal.access.SharedSecrets;
 
 public class NativeThread {
+    private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 
-    // Returns an opaque token representing the native thread underlying the
-    // invoking Java thread.  On systems that do not require signalling, this
-    // method always returns -1.
-    //
-    public static native long current();
+    /**
+     * Returns the current thread's ID.
+     */
+    public static long current() {
+        long tid = JLA.nativeTid();
+        if (tid == 0) {
+            tid = current0();
+            JLA.setNativeTid(tid);
+        }
+        return tid;
+    }
 
-    // Signals the given native thread so as to release it from a blocking I/O
-    // operation.  On systems that do not require signalling, this method has
-    // no effect.
-    //
-    public static native void signal(long nt);
+    /**
+     * Signals the given thread.
+     */
+    public static void signal(long tid) {
+        signal0(tid);
+    }
+
+    private static native long current0();
+
+    private static native void signal0(long tid);
 
     private static native void init();
 
--- a/src/java.base/unix/native/libnio/ch/NativeThread.c	Thu Jan 31 09:01:10 2019 +0000
+++ b/src/java.base/unix/native/libnio/ch/NativeThread.c	Tue Feb 05 18:53:19 2019 +0000
@@ -77,7 +77,7 @@
 }
 
 JNIEXPORT jlong JNICALL
-Java_sun_nio_ch_NativeThread_current(JNIEnv *env, jclass cl)
+Java_sun_nio_ch_NativeThread_current0(JNIEnv *env, jclass cl)
 {
 #ifdef __solaris__
     return (jlong)thr_self();
@@ -87,7 +87,7 @@
 }
 
 JNIEXPORT void JNICALL
-Java_sun_nio_ch_NativeThread_signal(JNIEnv *env, jclass cl, jlong thread)
+Java_sun_nio_ch_NativeThread_signal0(JNIEnv *env, jclass cl, jlong thread)
 {
     int ret;
 #ifdef __solaris__