Class Striped64

java.lang.Object
java.lang.Number
com.google.common.cache.Striped64
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
LongAdder

abstract class Striped64 extends 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 final class 
    Padded variant of AtomicLong supporting only raw accesses plus CAS.
  • Field Summary

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

    Constructors
    Constructor
    Description
    Package-private default constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) final boolean
    casBase(long cmp, long val)
    CASes the base field.
    (package private) final boolean
    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
    Returns a sun.misc.Unsafe.
    (package private) final void
    internalReset(long initialValue)
    Sets base and all cells to the given value.
    (package private) final 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 Details

    • threadHashCode

      static final 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 Random rng
      Generator of new random hash codes
    • NCPU

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

      @CheckForNull 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 Details

    • Striped64

      Striped64()
      Package-private default constructor
  • Method Details

    • 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, @CheckForNull 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