< prev index next >

src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java

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


3290             if (tb != null && tb.next != t)
3291                 return false;
3292             if (tn != null && tn.prev != t)
3293                 return false;
3294             if (tp != null && t != tp.left && t != tp.right)
3295                 return false;
3296             if (tl != null && (tl.parent != t || tl.hash > t.hash))
3297                 return false;
3298             if (tr != null && (tr.parent != t || tr.hash < t.hash))
3299                 return false;
3300             if (t.red && tl != null && tl.red && tr != null && tr.red)
3301                 return false;
3302             if (tl != null && !checkInvariants(tl))
3303                 return false;
3304             if (tr != null && !checkInvariants(tr))
3305                 return false;
3306             return true;
3307         }
3308 
3309         private static final Unsafe U = Unsafe.getUnsafe();
3310         private static final long LOCKSTATE;
3311         static {
3312             try {
3313                 LOCKSTATE = U.objectFieldOffset
3314                     (TreeBin.class.getDeclaredField("lockState"));
3315             } catch (ReflectiveOperationException e) {
3316                 throw new Error(e);
3317             }
3318         }
3319     }
3320 
3321     /* ----------------Table Traversal -------------- */
3322 
3323     /**
3324      * Records the table, its length, and current traversal index for a
3325      * traverser that must process a region of a forwarded table before
3326      * proceeding with current table.
3327      */
3328     static final class TableStack<K,V> {
3329         int length;
3330         int index;
3331         Node<K,V>[] tab;
3332         TableStack<K,V> next;
3333     }
3334 
3335     /**
3336      * Encapsulates traversal for methods such as containsValue; also
3337      * serves as a base class for other iterators and spliterators.
3338      *


6364                     while (s != null) {
6365                         t.result = reducer.applyAsInt(t.result, s.result);
6366                         s = t.rights = s.nextRight;
6367                     }
6368                 }
6369             }
6370         }
6371     }
6372 
6373     // Unsafe mechanics
6374     private static final Unsafe U = Unsafe.getUnsafe();
6375     private static final long SIZECTL;
6376     private static final long TRANSFERINDEX;
6377     private static final long BASECOUNT;
6378     private static final long CELLSBUSY;
6379     private static final long CELLVALUE;
6380     private static final int ABASE;
6381     private static final int ASHIFT;
6382 
6383     static {
6384         try {
6385             SIZECTL = U.objectFieldOffset
6386                 (ConcurrentHashMap.class.getDeclaredField("sizeCtl"));
6387             TRANSFERINDEX = U.objectFieldOffset
6388                 (ConcurrentHashMap.class.getDeclaredField("transferIndex"));
6389             BASECOUNT = U.objectFieldOffset
6390                 (ConcurrentHashMap.class.getDeclaredField("baseCount"));
6391             CELLSBUSY = U.objectFieldOffset
6392                 (ConcurrentHashMap.class.getDeclaredField("cellsBusy"));
6393 
6394             CELLVALUE = U.objectFieldOffset
6395                 (CounterCell.class.getDeclaredField("value"));
6396 
6397             ABASE = U.arrayBaseOffset(Node[].class);
6398             int scale = U.arrayIndexScale(Node[].class);
6399             if ((scale & (scale - 1)) != 0)
6400                 throw new Error("array index scale not a power of two");
6401             ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6402         } catch (ReflectiveOperationException e) {
6403             throw new Error(e);
6404         }
6405 
6406         // Reduce the risk of rare disastrous classloading in first call to
6407         // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
6408         Class<?> ensureLoaded = LockSupport.class;
6409     }
6410 }


3290             if (tb != null && tb.next != t)
3291                 return false;
3292             if (tn != null && tn.prev != t)
3293                 return false;
3294             if (tp != null && t != tp.left && t != tp.right)
3295                 return false;
3296             if (tl != null && (tl.parent != t || tl.hash > t.hash))
3297                 return false;
3298             if (tr != null && (tr.parent != t || tr.hash < t.hash))
3299                 return false;
3300             if (t.red && tl != null && tl.red && tr != null && tr.red)
3301                 return false;
3302             if (tl != null && !checkInvariants(tl))
3303                 return false;
3304             if (tr != null && !checkInvariants(tr))
3305                 return false;
3306             return true;
3307         }
3308 
3309         private static final Unsafe U = Unsafe.getUnsafe();
3310         private static final long LOCKSTATE
3311                 = U.objectFieldOffset(TreeBin.class, "lockState");







3312     }
3313 
3314     /* ----------------Table Traversal -------------- */
3315 
3316     /**
3317      * Records the table, its length, and current traversal index for a
3318      * traverser that must process a region of a forwarded table before
3319      * proceeding with current table.
3320      */
3321     static final class TableStack<K,V> {
3322         int length;
3323         int index;
3324         Node<K,V>[] tab;
3325         TableStack<K,V> next;
3326     }
3327 
3328     /**
3329      * Encapsulates traversal for methods such as containsValue; also
3330      * serves as a base class for other iterators and spliterators.
3331      *


6357                     while (s != null) {
6358                         t.result = reducer.applyAsInt(t.result, s.result);
6359                         s = t.rights = s.nextRight;
6360                     }
6361                 }
6362             }
6363         }
6364     }
6365 
6366     // Unsafe mechanics
6367     private static final Unsafe U = Unsafe.getUnsafe();
6368     private static final long SIZECTL;
6369     private static final long TRANSFERINDEX;
6370     private static final long BASECOUNT;
6371     private static final long CELLSBUSY;
6372     private static final long CELLVALUE;
6373     private static final int ABASE;
6374     private static final int ASHIFT;
6375 
6376     static {

6377         SIZECTL = U.objectFieldOffset
6378             (ConcurrentHashMap.class, "sizeCtl");
6379         TRANSFERINDEX = U.objectFieldOffset
6380             (ConcurrentHashMap.class, "transferIndex");
6381         BASECOUNT = U.objectFieldOffset
6382             (ConcurrentHashMap.class, "baseCount");
6383         CELLSBUSY = U.objectFieldOffset
6384             (ConcurrentHashMap.class, "cellsBusy");
6385 
6386         CELLVALUE = U.objectFieldOffset
6387             (CounterCell.class, "value");
6388 
6389         ABASE = U.arrayBaseOffset(Node[].class);
6390         int scale = U.arrayIndexScale(Node[].class);
6391         if ((scale & (scale - 1)) != 0)
6392             throw new Error("array index scale not a power of two");
6393         ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);



6394 
6395         // Reduce the risk of rare disastrous classloading in first call to
6396         // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
6397         Class<?> ensureLoaded = LockSupport.class;
6398     }
6399 }
< prev index next >