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 intThe maximum number of attempts when trying to expand the table.(package private) static final intThe bound on the table size.(package private) static final intNumber of CPUS.(package private) static final longTable of buffers.(package private) static final long(package private) intSpinlock (locked via CAS) used when resizing and/or creating Buffers. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static final intadvanceProbe(int probe) Pseudo-randomly advances and records the given probe value for the given thread.(package private) final booleanCASes the tableBusy field from 0 to 1 to acquire lock.Creates a new buffer instance after resizing to accommodate a producer.voidDrains the buffer, sending each element to the consumer for processing.(package private) final voidexpandOrRetry(E e, boolean wasUncontended) Handles cases of updates involving initialization, resizing, creating new Buffers, and/or contention.(package private) static final intgetProbe()Returns the probe value for the current thread.intInserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.intreads()Returns the number of elements that have been read from the buffer.intwrites()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
-
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
-
offer
Description copied from interface:BufferInserts 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:BufferDrains 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
-
writes
-
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
-