Class Striped64

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    LongAdder

    abstract class Striped64
    extends java.lang.Number
    A package-local class holding common representation and mechanics for classes supporting dynamic striping on 64bit values. The class extends Number so that concrete subclasses must publicly do so.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  Striped64.Cell
      Padded variant of AtomicLong supporting only raw accesses plus CAS.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) long base
      Base value, used mainly when there is no contention, but also as a fallback during table initialization races.
      private static long baseOffset  
      (package private) int busy
      Spinlock (locked via CAS) used when resizing and/or creating Cells.
      private static long busyOffset  
      (package private) Striped64.Cell[] cells
      Table of cells.
      (package private) static int NCPU
      Number of CPUS, to place bound on table size
      (package private) static java.util.Random rng
      Generator of new random hash codes
      (package private) static java.lang.ThreadLocal<int[]> threadHashCode
      ThreadLocal holding a single-slot int array holding hash code.
      private static sun.misc.Unsafe UNSAFE  
    • Constructor Summary

      Constructors 
      Constructor Description
      Striped64()
      Package-private default constructor
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) boolean casBase​(long cmp, long val)
      CASes the base field.
      (package private) boolean casBusy()
      CASes the busy field from 0 to 1 to acquire lock.
      (package private) abstract long fn​(long currentValue, long newValue)
      Computes the function of current and new value.
      private static sun.misc.Unsafe getUnsafe()
      Returns a sun.misc.Unsafe.
      (package private) void internalReset​(long initialValue)
      Sets base and all cells to the given value.
      (package private) void retryUpdate​(long x, int[] hc, boolean wasUncontended)
      Handles cases of updates involving initialization, resizing, creating new Cells, and/or contention.
      • Methods inherited from class java.lang.Number

        byteValue, doubleValue, floatValue, intValue, longValue, shortValue
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • threadHashCode

        static final java.lang.ThreadLocal<int[]> threadHashCode
        ThreadLocal holding a single-slot int array holding hash code. Unlike the JDK8 version of this class, we use a suboptimal int[] representation to avoid introducing a new type that can impede class-unloading when ThreadLocals are not removed.
      • rng

        static final java.util.Random rng
        Generator of new random hash codes
      • NCPU

        static final int NCPU
        Number of CPUS, to place bound on table size
      • cells

        transient volatile Striped64.Cell[] cells
        Table of cells. When non-null, size is a power of 2.
      • base

        transient volatile long base
        Base value, used mainly when there is no contention, but also as a fallback during table initialization races. Updated via CAS.
      • busy

        transient volatile int busy
        Spinlock (locked via CAS) used when resizing and/or creating Cells.
      • UNSAFE

        private static final sun.misc.Unsafe UNSAFE
      • baseOffset

        private static final long baseOffset
      • busyOffset

        private static final long busyOffset
    • Constructor Detail

      • Striped64

        Striped64()
        Package-private default constructor
    • Method Detail

      • casBase

        final boolean casBase​(long cmp,
                              long val)
        CASes the base field.
      • casBusy

        final boolean casBusy()
        CASes the busy field from 0 to 1 to acquire lock.
      • fn

        abstract long fn​(long currentValue,
                         long newValue)
        Computes the function of current and new value. Subclasses should open-code this update function for most uses, but the virtualized form is needed within retryUpdate.
        Parameters:
        currentValue - the current value (of either base or a cell)
        newValue - the argument from a user update call
        Returns:
        result of the update function
      • retryUpdate

        final void retryUpdate​(long x,
                               int[] hc,
                               boolean wasUncontended)
        Handles cases of updates involving initialization, resizing, creating new Cells, and/or contention. See above for explanation. This method suffers the usual non-modularity problems of optimistic retry code, relying on rechecked sets of reads.
        Parameters:
        x - the value
        hc - the hash code holder
        wasUncontended - false if CAS failed before call
      • internalReset

        final void internalReset​(long initialValue)
        Sets base and all cells to the given value.
      • getUnsafe

        private static sun.misc.Unsafe getUnsafe()
        Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple call to Unsafe.getUnsafe when integrating into a jdk.
        Returns:
        a sun.misc.Unsafe