Class StripedBuffer<E>
java.lang.Object
com.github.benmanes.caffeine.cache.StripedBuffer<E>
- All Implemented Interfaces:
Buffer<E>
- Direct Known Subclasses:
BoundedBuffer
A base class providing the mechanics for supporting dynamic striping of bounded buffers. This
implementation is an adaption of the numeric 64-bit
Striped64
class, which is used by atomic counters. The approach was modified to lazily grow an array of
buffers in order to minimize memory usage for caches that are not heavily contended on.-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final int
The maximum number of attempts when trying to expand the table.(package private) static final int
The bound on the table size.(package private) static final int
Number of CPUS.(package private) static final long
Table of buffers.(package private) static final long
(package private) int
Spinlock (locked via CAS) used when resizing and/or creating Buffers. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static final int
advanceProbe
(int probe) Pseudo-randomly advances and records the given probe value for the given thread.(package private) final boolean
CASes the tableBusy field from 0 to 1 to acquire lock.Creates a new buffer instance after resizing to accommodate a producer.void
Drains the buffer, sending each element to the consumer for processing.(package private) final void
expandOrRetry
(E e, boolean wasUncontended) Handles cases of updates involving initialization, resizing, creating new Buffers, and/or contention.(package private) static final int
getProbe()
Returns the probe value for the current thread.int
Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.int
reads()
Returns the number of elements that have been read from the buffer.int
writes()
Returns the number of elements that have been written to the buffer.
-
Field Details
-
TABLE_BUSY
static final long TABLE_BUSY -
PROBE
static final long PROBE -
NCPU
static final int NCPUNumber of CPUS. -
MAXIMUM_TABLE_SIZE
static final int MAXIMUM_TABLE_SIZEThe bound on the table size. -
ATTEMPTS
static final int ATTEMPTSThe maximum number of attempts when trying to expand the table.- See Also:
-
table
Table of buffers. When non-null, size is a power of 2. -
tableBusy
transient volatile int tableBusySpinlock (locked via CAS) used when resizing and/or creating Buffers.
-
-
Constructor Details
-
StripedBuffer
StripedBuffer()
-
-
Method Details
-
casTableBusy
final boolean casTableBusy()CASes the tableBusy field from 0 to 1 to acquire lock. -
getProbe
static final int getProbe()Returns the probe value for the current thread. Duplicated from ThreadLocalRandom because of packaging restrictions. -
advanceProbe
static final int advanceProbe(int probe) Pseudo-randomly advances and records the given probe value for the given thread. Duplicated from ThreadLocalRandom because of packaging restrictions. -
create
Creates a new buffer instance after resizing to accommodate a producer.- Parameters:
e
- the producer's element- Returns:
- a newly created buffer populated with a single element
-
offer
Description copied from interface:Buffer
Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions. The addition is allowed to fail spuriously if multiple threads insert concurrently. -
drainTo
Description copied from interface:Buffer
Drains the buffer, sending each element to the consumer for processing. The caller must ensure that a consumer has exclusive read access to the buffer. -
reads
public int reads()Description copied from interface:Buffer
Returns the number of elements that have been read from the buffer. -
writes
public int writes()Description copied from interface:Buffer
Returns the number of elements that have been written to the buffer. -
expandOrRetry
Handles cases of updates involving initialization, resizing, creating new Buffers, 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:
e
- the element to addwasUncontended
- false if CAS failed before call
-