8048997: Enhance thread contexts in CORBA
authormsheppar
Thu, 02 Apr 2015 22:14:38 +0100
changeset 29937 c774371adf92
parent 29936 9edf96e1b418
child 29938 c4d54ad0280d
8048997: Enhance thread contexts in CORBA Reviewed-by: chegar
corba/src/java.corba/share/classes/com/sun/corba/se/impl/corba/RequestImpl.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/javax/rmi/CORBA/Util.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAImpl.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAManagerImpl.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAPolicyMediatorImpl_R_USM.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/orb/ORBImpl.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/orbutil/threadpool/ThreadPoolImpl.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/transport/ManagedLocalsThread.java
corba/src/java.corba/share/classes/com/sun/corba/se/impl/transport/SelectorImpl.java
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/corba/RequestImpl.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/corba/RequestImpl.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -65,6 +65,7 @@
 import com.sun.corba.se.spi.logging.CORBALogDomains;
 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
 import com.sun.corba.se.impl.corba.AsynchInvoke;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
 
 public class RequestImpl
     extends Request
@@ -255,7 +256,7 @@
     public synchronized void send_deferred()
     {
         AsynchInvoke invokeObject = new AsynchInvoke(_orb, this, false);
-        new Thread(invokeObject).start();
+        new ManagedLocalsThread(invokeObject).start();
     }
 
     public synchronized boolean poll_response()
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/javax/rmi/CORBA/Util.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/javax/rmi/CORBA/Util.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -112,6 +112,8 @@
 import com.sun.corba.se.impl.logging.UtilSystemException;
 import com.sun.corba.se.spi.logging.CORBALogDomains;
 import sun.corba.SharedSecrets;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
+
 
 /**
  * Provides utility methods that can be used by stubs and ties to
@@ -750,7 +752,7 @@
     }
 }
 
-class KeepAlive extends Thread
+class KeepAlive extends ManagedLocalsThread
 {
     boolean quit = false;
 
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAImpl.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAImpl.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -103,6 +103,7 @@
 import com.sun.corba.se.impl.orbutil.concurrent.SyncUtil ;
 import com.sun.corba.se.impl.orbutil.concurrent.ReentrantMutex ;
 import com.sun.corba.se.impl.orbutil.concurrent.CondVar ;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
 
 /**
  * POAImpl is the implementation of the Portable Object Adapter. It
@@ -516,7 +517,7 @@
 
     // Converted from anonymous class to local class
     // so that we can call performDestroy() directly.
-    static class DestroyThread extends Thread {
+    static class DestroyThread extends ManagedLocalsThread {
         private boolean wait ;
         private boolean etherealize ;
         private boolean debug ;
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAManagerImpl.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAManagerImpl.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,7 @@
 import com.sun.corba.se.impl.logging.POASystemException ;
 
 import com.sun.corba.se.impl.orbutil.ORBUtility ;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
 
 /** POAManagerImpl is the implementation of the POAManager interface.
  *  Its public methods are activate(), hold_requests(), discard_requests()
@@ -357,7 +358,7 @@
             if (wait_for_completion)
                 deactivator.run() ;
             else {
-                Thread thr = new Thread(deactivator) ;
+                Thread thr = new ManagedLocalsThread(deactivator) ;
                 thr.start() ;
             }
         } finally {
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAPolicyMediatorImpl_R_USM.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/oa/poa/POAPolicyMediatorImpl_R_USM.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,6 @@
 package com.sun.corba.se.impl.oa.poa ;
 
 import java.util.Set ;
-
 import org.omg.CORBA.SystemException ;
 
 import org.omg.PortableServer.ServantActivator ;
@@ -50,6 +49,7 @@
 
 import com.sun.corba.se.spi.oa.OAInvocationInfo ;
 import com.sun.corba.se.spi.oa.NullServant ;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
 
 /** Implementation of POARequesHandler that provides policy specific
  * operations on the POA.
@@ -303,13 +303,14 @@
         throw new WrongPolicy();
     }
 
-    class Etherealizer extends Thread {
+    class Etherealizer extends ManagedLocalsThread {
         private POAPolicyMediatorImpl_R_USM mediator ;
         private ActiveObjectMap.Key key ;
         private AOMEntry entry ;
         private Servant servant ;
         private boolean debug ;
 
+
         public Etherealizer( POAPolicyMediatorImpl_R_USM mediator,
             ActiveObjectMap.Key key, AOMEntry entry, Servant servant,
             boolean debug )
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/orb/ORBImpl.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/orb/ORBImpl.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -165,6 +165,7 @@
 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
 import com.sun.corba.se.impl.copyobject.CopierManagerImpl;
 import com.sun.corba.se.impl.presentation.rmi.PresentationManagerImpl;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
 
 /**
  * The JavaIDL ORB implementation.
@@ -691,7 +692,7 @@
         for (int i = 0; i < req.length; i++) {
             AsynchInvoke invokeObject = new AsynchInvoke( this,
                 (com.sun.corba.se.impl.corba.RequestImpl)req[i], true);
-            new Thread(invokeObject).start();
+            new ManagedLocalsThread(invokeObject).start();
         }
     }
 
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/orbutil/threadpool/ThreadPoolImpl.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/orbutil/threadpool/ThreadPoolImpl.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -54,6 +54,7 @@
 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
 import com.sun.corba.se.impl.orbutil.ORBConstants;
 import com.sun.corba.se.spi.logging.CORBALogDomains;
+import com.sun.corba.se.impl.transport.ManagedLocalsThread;
 
 public class ThreadPoolImpl implements ThreadPool
 {
@@ -459,7 +460,7 @@
     }
 
 
-    private class WorkerThread extends Thread implements Closeable
+    private class WorkerThread extends ManagedLocalsThread implements Closeable
     {
         private Work currentWork;
         private int threadId = 0; // unique id for the thread
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/transport/ManagedLocalsThread.java	Thu Apr 02 22:14:38 2015 +0100
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.corba.se.impl.transport;
+
+import sun.misc.Unsafe;
+import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * A thread that has it's thread locals, and inheritable thread
+ * locals erased on construction.
+ */
+public class ManagedLocalsThread extends Thread {
+    private static final Unsafe UNSAFE;
+    private static final long THREAD_LOCALS;
+    private static final long INHERITABLE_THREAD_LOCALS;
+
+    public ManagedLocalsThread () {
+        super();
+    }
+
+    public ManagedLocalsThread(String  name) {
+        super(name);
+        eraseThreadLocals();
+    }
+    public ManagedLocalsThread(Runnable target) {
+        super(target);
+        eraseThreadLocals();
+    }
+
+    public ManagedLocalsThread(Runnable target, String name) {
+        super(target, name);
+        eraseThreadLocals();
+    }
+
+    public ManagedLocalsThread(ThreadGroup group, Runnable target, String name) {
+        super(group, target, name);
+        eraseThreadLocals();
+    }
+
+    public ManagedLocalsThread(ThreadGroup group, String name) {
+        super(group, name);
+        eraseThreadLocals();
+    }
+
+    /**
+     * Drops all thread locals (and inherited thread locals).
+     */
+    public final void eraseThreadLocals() {
+                UNSAFE.putObject(this, THREAD_LOCALS, null);
+                UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
+    }
+
+    private static Unsafe getUnsafe() {
+        PrivilegedAction<Unsafe> pa = () -> {
+            Class<?> unsafeClass = sun.misc.Unsafe.class;
+            try {
+                Field f = unsafeClass.getDeclaredField("theUnsafe");
+                f.setAccessible(true);
+                return (Unsafe) f.get(null);
+            } catch (Exception e) {
+                throw new Error(e);
+            }
+        };
+        return AccessController.doPrivileged(pa);
+    }
+
+    private static long getThreadFieldOffset(String fieldName) {
+        PrivilegedAction<Long> pa = () -> {
+            Class<?> t = Thread.class;
+            long fieldOffset;
+            try {
+                fieldOffset = UNSAFE.objectFieldOffset(t
+                        .getDeclaredField("inheritableThreadLocals"));
+            } catch (Exception e) {
+                throw new Error(e);
+            }
+            return fieldOffset;
+        };
+        return AccessController.doPrivileged(pa);
+    }
+
+    static {
+        UNSAFE = getUnsafe();
+        try {
+            THREAD_LOCALS = getThreadFieldOffset("threadLocals");
+            INHERITABLE_THREAD_LOCALS = getThreadFieldOffset("inheritableThreadLocals");
+        } catch (Exception e) {
+            throw new Error(e);
+        }
+    }
+}
--- a/corba/src/java.corba/share/classes/com/sun/corba/se/impl/transport/SelectorImpl.java	Mon Apr 13 14:50:27 2015 +0100
+++ b/corba/src/java.corba/share/classes/com/sun/corba/se/impl/transport/SelectorImpl.java	Thu Apr 02 22:14:38 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -57,7 +57,7 @@
  */
 class SelectorImpl
     extends
-        Thread
+        ManagedLocalsThread
     implements
         com.sun.corba.se.pept.transport.Selector
 {