< prev index next >

src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java

Print this page
8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
Reviewed-by: martin, psandoz

 127         final boolean cas(long cmp, long val) {
 128             return VALUE.compareAndSet(this, cmp, val);
 129         }
 130         final void reset() {
 131             VALUE.setVolatile(this, 0L);
 132         }
 133         final void reset(long identity) {
 134             VALUE.setVolatile(this, identity);
 135         }
 136         final long getAndSet(long val) {
 137             return (long)VALUE.getAndSet(this, val);
 138         }
 139 
 140         // VarHandle mechanics
 141         private static final VarHandle VALUE;
 142         static {
 143             try {
 144                 MethodHandles.Lookup l = MethodHandles.lookup();
 145                 VALUE = l.findVarHandle(Cell.class, "value", long.class);
 146             } catch (ReflectiveOperationException e) {
 147                 throw new Error(e);
 148             }
 149         }
 150     }
 151 
 152     /** Number of CPUS, to place bound on table size */
 153     static final int NCPU = Runtime.getRuntime().availableProcessors();
 154 
 155     /**
 156      * Table of cells. When non-null, size is a power of 2.
 157      */
 158     transient volatile Cell[] cells;
 159 
 160     /**
 161      * Base value, used mainly when there is no contention, but also as
 162      * a fallback during table initialization races. Updated via CAS.
 163      */
 164     transient volatile long base;
 165 
 166     /**
 167      * Spinlock (locked via CAS) used when resizing and/or creating Cells.


 379         }
 380     }
 381 
 382     // VarHandle mechanics
 383     private static final VarHandle BASE;
 384     private static final VarHandle CELLSBUSY;
 385     private static final VarHandle THREAD_PROBE;
 386     static {
 387         try {
 388             MethodHandles.Lookup l = MethodHandles.lookup();
 389             BASE = l.findVarHandle(Striped64.class,
 390                     "base", long.class);
 391             CELLSBUSY = l.findVarHandle(Striped64.class,
 392                     "cellsBusy", int.class);
 393             l = java.security.AccessController.doPrivileged(
 394                     new java.security.PrivilegedAction<>() {
 395                         public MethodHandles.Lookup run() {
 396                             try {
 397                                 return MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup());
 398                             } catch (ReflectiveOperationException e) {
 399                                 throw new Error(e);
 400                             }
 401                         }});
 402             THREAD_PROBE = l.findVarHandle(Thread.class,
 403                     "threadLocalRandomProbe", int.class);
 404         } catch (ReflectiveOperationException e) {
 405             throw new Error(e);
 406         }
 407     }
 408 
 409 }

 127         final boolean cas(long cmp, long val) {
 128             return VALUE.compareAndSet(this, cmp, val);
 129         }
 130         final void reset() {
 131             VALUE.setVolatile(this, 0L);
 132         }
 133         final void reset(long identity) {
 134             VALUE.setVolatile(this, identity);
 135         }
 136         final long getAndSet(long val) {
 137             return (long)VALUE.getAndSet(this, val);
 138         }
 139 
 140         // VarHandle mechanics
 141         private static final VarHandle VALUE;
 142         static {
 143             try {
 144                 MethodHandles.Lookup l = MethodHandles.lookup();
 145                 VALUE = l.findVarHandle(Cell.class, "value", long.class);
 146             } catch (ReflectiveOperationException e) {
 147                 throw new ExceptionInInitializerError(e);
 148             }
 149         }
 150     }
 151 
 152     /** Number of CPUS, to place bound on table size */
 153     static final int NCPU = Runtime.getRuntime().availableProcessors();
 154 
 155     /**
 156      * Table of cells. When non-null, size is a power of 2.
 157      */
 158     transient volatile Cell[] cells;
 159 
 160     /**
 161      * Base value, used mainly when there is no contention, but also as
 162      * a fallback during table initialization races. Updated via CAS.
 163      */
 164     transient volatile long base;
 165 
 166     /**
 167      * Spinlock (locked via CAS) used when resizing and/or creating Cells.


 379         }
 380     }
 381 
 382     // VarHandle mechanics
 383     private static final VarHandle BASE;
 384     private static final VarHandle CELLSBUSY;
 385     private static final VarHandle THREAD_PROBE;
 386     static {
 387         try {
 388             MethodHandles.Lookup l = MethodHandles.lookup();
 389             BASE = l.findVarHandle(Striped64.class,
 390                     "base", long.class);
 391             CELLSBUSY = l.findVarHandle(Striped64.class,
 392                     "cellsBusy", int.class);
 393             l = java.security.AccessController.doPrivileged(
 394                     new java.security.PrivilegedAction<>() {
 395                         public MethodHandles.Lookup run() {
 396                             try {
 397                                 return MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup());
 398                             } catch (ReflectiveOperationException e) {
 399                                 throw new ExceptionInInitializerError(e);
 400                             }
 401                         }});
 402             THREAD_PROBE = l.findVarHandle(Thread.class,
 403                     "threadLocalRandomProbe", int.class);
 404         } catch (ReflectiveOperationException e) {
 405             throw new ExceptionInInitializerError(e);
 406         }
 407     }
 408 
 409 }
< prev index next >