Package io.prometheus.client
Class Striped64
java.lang.Object
java.lang.Number
io.prometheus.client.Striped64
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DoubleAdder
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 ClassesModifier and TypeClassDescription(package private) static final class
Padded variant of AtomicLong supporting only raw accesses plus CAS. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) long
Base value, used mainly when there is no contention, but also as a fallback during table initialization races.(package private) int
Spinlock (locked via CAS) used when resizing and/or creating Cells.private static final AtomicLongFieldUpdater
<Striped64> private static final AtomicIntegerFieldUpdater
<Striped64> (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. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) final boolean
casBase
(long cmp, long val) CASes the base field.(package private) final 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.(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
-
Field Details
-
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
Generator of new random hash codes -
NCPU
static final int NCPUNumber of CPUS, to place bound on table size -
cells
Table of cells. When non-null, size is a power of 2. -
base
transient volatile long baseBase value, used mainly when there is no contention, but also as a fallback during table initialization races. Updated via CAS. -
busy
transient volatile int busySpinlock (locked via CAS) used when resizing and/or creating Cells. -
CAS_BASE
-
CAS_BUSY
-
-
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, 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 valuehc
- the hash code holderwasUncontended
- false if CAS failed before call
-
internalReset
final void internalReset(long initialValue) Sets base and all cells to the given value.
-