< prev index next >

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

Print this page




2608     /**
2609      * Returns the ServiceCatalog for modules defined to this class loader,
2610      * creating it if it doesn't already exist.
2611      */
2612     ServicesCatalog createOrGetServicesCatalog() {
2613         ServicesCatalog catalog = servicesCatalog;
2614         if (catalog == null) {
2615             catalog = new ServicesCatalog();
2616             boolean set = trySetObjectField("servicesCatalog", catalog);
2617             if (!set) {
2618                 // beaten by someone else
2619                 catalog = servicesCatalog;
2620             }
2621         }
2622         return catalog;
2623     }
2624 
2625     // the ServiceCatalog for modules associated with this class loader.
2626     private volatile ServicesCatalog servicesCatalog;
2627 



















2628 
2629     /**
2630      * Attempts to atomically set a volatile field in this object. Returns
2631      * {@code true} if not beaten by another thread. Avoids the use of
2632      * AtomicReferenceFieldUpdater in this class.
2633      */
2634     private boolean trySetObjectField(String name, Object obj) {
2635         Unsafe unsafe = Unsafe.getUnsafe();
2636         Class<?> k = ClassLoader.class;
2637         long offset;
2638         try {
2639             Field f = k.getDeclaredField(name);
2640             offset = unsafe.objectFieldOffset(f);
2641         } catch (NoSuchFieldException e) {
2642             throw new InternalError(e);
2643         }
2644         return unsafe.compareAndSwapObject(this, offset, null, obj);
2645     }
2646 }
2647 




2608     /**
2609      * Returns the ServiceCatalog for modules defined to this class loader,
2610      * creating it if it doesn't already exist.
2611      */
2612     ServicesCatalog createOrGetServicesCatalog() {
2613         ServicesCatalog catalog = servicesCatalog;
2614         if (catalog == null) {
2615             catalog = new ServicesCatalog();
2616             boolean set = trySetObjectField("servicesCatalog", catalog);
2617             if (!set) {
2618                 // beaten by someone else
2619                 catalog = servicesCatalog;
2620             }
2621         }
2622         return catalog;
2623     }
2624 
2625     // the ServiceCatalog for modules associated with this class loader.
2626     private volatile ServicesCatalog servicesCatalog;
2627 
2628     /**
2629      * Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s)
2630      * associated with this ClassLoader, creating it if it doesn't already exist.
2631      */
2632     ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap() {
2633         ConcurrentHashMap<?, ?> map = classLoaderValueMap;
2634         if (map == null) {
2635             map = new ConcurrentHashMap<>();
2636             boolean set = trySetObjectField("classLoaderValueMap", map);
2637             if (!set) {
2638                 // beaten by someone else
2639                 map = classLoaderValueMap;
2640             }
2641         }
2642         return map;
2643     }
2644 
2645     // the storage for ClassLoaderValue(s) associated with this ClassLoader
2646     private volatile ConcurrentHashMap<?, ?> classLoaderValueMap;
2647 
2648     /**
2649      * Attempts to atomically set a volatile field in this object. Returns
2650      * {@code true} if not beaten by another thread. Avoids the use of
2651      * AtomicReferenceFieldUpdater in this class.
2652      */
2653     private boolean trySetObjectField(String name, Object obj) {
2654         Unsafe unsafe = Unsafe.getUnsafe();
2655         Class<?> k = ClassLoader.class;
2656         long offset;
2657         try {
2658             Field f = k.getDeclaredField(name);
2659             offset = unsafe.objectFieldOffset(f);
2660         } catch (NoSuchFieldException e) {
2661             throw new InternalError(e);
2662         }
2663         return unsafe.compareAndSwapObject(this, offset, null, obj);
2664     }
2665 }
2666 


< prev index next >