< prev index next >

src/java.base/share/classes/java/lang/ClassLoader.java

Print this page
rev 17358 : 8182487: Add Unsafe.objectFieldOffset(Class, String)
Reviewed-by: dsimms, twisti, bchristi, mgerdin


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.UncheckedIOException;
  31 import java.io.File;
  32 import java.lang.reflect.Constructor;
  33 import java.lang.reflect.Field;
  34 import java.net.URL;
  35 import java.security.AccessController;
  36 import java.security.AccessControlContext;
  37 import java.security.CodeSource;
  38 import java.security.PrivilegedAction;
  39 import java.security.ProtectionDomain;
  40 import java.security.cert.Certificate;
  41 import java.util.Collections;
  42 import java.util.Enumeration;
  43 import java.util.HashMap;
  44 import java.util.Hashtable;
  45 import java.util.Map;
  46 import java.util.NoSuchElementException;
  47 import java.util.Objects;
  48 import java.util.Set;
  49 import java.util.Spliterator;
  50 import java.util.Spliterators;
  51 import java.util.Stack;
  52 import java.util.Vector;
  53 import java.util.WeakHashMap;


2861             if (!set) {
2862                 // beaten by someone else
2863                 map = classLoaderValueMap;
2864             }
2865         }
2866         return map;
2867     }
2868 
2869     // the storage for ClassLoaderValue(s) associated with this ClassLoader
2870     private volatile ConcurrentHashMap<?, ?> classLoaderValueMap;
2871 
2872     /**
2873      * Attempts to atomically set a volatile field in this object. Returns
2874      * {@code true} if not beaten by another thread. Avoids the use of
2875      * AtomicReferenceFieldUpdater in this class.
2876      */
2877     private boolean trySetObjectField(String name, Object obj) {
2878         Unsafe unsafe = Unsafe.getUnsafe();
2879         Class<?> k = ClassLoader.class;
2880         long offset;
2881         try {
2882             Field f = k.getDeclaredField(name);
2883             offset = unsafe.objectFieldOffset(f);
2884         } catch (NoSuchFieldException e) {
2885             throw new InternalError(e);
2886         }
2887         return unsafe.compareAndSetObject(this, offset, null, obj);
2888     }
2889 }
2890 
2891 /*
2892  * A utility class that will enumerate over an array of enumerations.
2893  */
2894 final class CompoundEnumeration<E> implements Enumeration<E> {
2895     private final Enumeration<E>[] enums;
2896     private int index;
2897 
2898     public CompoundEnumeration(Enumeration<E>[] enums) {
2899         this.enums = enums;
2900     }
2901 
2902     private boolean next() {
2903         while (index < enums.length) {
2904             if (enums[index] != null && enums[index].hasMoreElements()) {
2905                 return true;
2906             }


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.UncheckedIOException;
  31 import java.io.File;
  32 import java.lang.reflect.Constructor;

  33 import java.net.URL;
  34 import java.security.AccessController;
  35 import java.security.AccessControlContext;
  36 import java.security.CodeSource;
  37 import java.security.PrivilegedAction;
  38 import java.security.ProtectionDomain;
  39 import java.security.cert.Certificate;
  40 import java.util.Collections;
  41 import java.util.Enumeration;
  42 import java.util.HashMap;
  43 import java.util.Hashtable;
  44 import java.util.Map;
  45 import java.util.NoSuchElementException;
  46 import java.util.Objects;
  47 import java.util.Set;
  48 import java.util.Spliterator;
  49 import java.util.Spliterators;
  50 import java.util.Stack;
  51 import java.util.Vector;
  52 import java.util.WeakHashMap;


2860             if (!set) {
2861                 // beaten by someone else
2862                 map = classLoaderValueMap;
2863             }
2864         }
2865         return map;
2866     }
2867 
2868     // the storage for ClassLoaderValue(s) associated with this ClassLoader
2869     private volatile ConcurrentHashMap<?, ?> classLoaderValueMap;
2870 
2871     /**
2872      * Attempts to atomically set a volatile field in this object. Returns
2873      * {@code true} if not beaten by another thread. Avoids the use of
2874      * AtomicReferenceFieldUpdater in this class.
2875      */
2876     private boolean trySetObjectField(String name, Object obj) {
2877         Unsafe unsafe = Unsafe.getUnsafe();
2878         Class<?> k = ClassLoader.class;
2879         long offset;
2880         offset = unsafe.objectFieldOffset(k, name);





2881         return unsafe.compareAndSetObject(this, offset, null, obj);
2882     }
2883 }
2884 
2885 /*
2886  * A utility class that will enumerate over an array of enumerations.
2887  */
2888 final class CompoundEnumeration<E> implements Enumeration<E> {
2889     private final Enumeration<E>[] enums;
2890     private int index;
2891 
2892     public CompoundEnumeration(Enumeration<E>[] enums) {
2893         this.enums = enums;
2894     }
2895 
2896     private boolean next() {
2897         while (index < enums.length) {
2898             if (enums[index] != null && enums[index].hasMoreElements()) {
2899                 return true;
2900             }
< prev index next >