Package com.codahale.metrics
Class Striped64
- java.lang.Object
-
- java.lang.Number
-
- com.codahale.metrics.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.(package private) static class
Striped64.HashCode
Holder for the thread-local hash code.(package private) static class
Striped64.ThreadHashCode
The corresponding ThreadLocal class
-
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 Striped64.ThreadHashCode
threadHashCode
Static per-thread hash codes.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, Striped64.HashCode hc, boolean wasUncontended)
Handles cases of updates involving initialization, resizing, creating new Cells, and/or contention.
-
-
-
Field Detail
-
threadHashCode
static final Striped64.ThreadHashCode threadHashCode
Static per-thread hash codes. Shared across all instances to reduce ThreadLocal pollution and because adjustments due to collisions in one table are likely to be appropriate for others.
-
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
-
-
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, Striped64.HashCode 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.
-
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
-
-