Top Description Fields Constructors Methods
com.github.benmanes.caffeine.cache

pack-priv abstract Class StripedBuffer<E>

extends Object
implements Buffer<E>
Class Inheritance
All Implemented Interfaces
com.github.benmanes.caffeine.cache.Buffer
Known Direct Subclasses
com.github.benmanes.caffeine.cache.BoundedBuffer
Static Imports
com.github.benmanes.caffeine.cache.Caffeine.ceilingPowerOfTwo

A base class providing the mechanics for supporting dynamic striping of bounded buffers. This implementation is an adaption of the numeric 64-bit java.util.concurrent.atomic.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.
Authors
[email protected] (Doug Lea), [email protected] (Ben Manes)

Field Summary

Modifier and TypeField and Description
pack-priv static final int
ATTEMPTS

The maximum number of attempts when trying to expand the table.

pack-priv static final int
MAXIMUM_TABLE_SIZE

The bound on the table size.

pack-priv static final int
NCPU

Number of CPUS.

pack-priv volatile @Nullable Buffer<E>[]
table

Table of buffers.

pack-priv static final VarHandle
pack-priv volatile int
tableBusy

Spinlock (locked via CAS) used when resizing and/or creating Buffers.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv final boolean
casTableBusy()

CASes the tableBusy field from 0 to 1 to acquire lock.

protected abstract Buffer<E>

Returns:

a newly created buffer populated with a single element
create
(E
the producer's element
e
)

Creates a new buffer instance after resizing to accommodate a producer.

public void
drainTo(Consumer<E>
the action to perform on each element
consumer
)

Implements com.github.benmanes.caffeine.cache.Buffer.drainTo.

Drains the buffer, sending each element to the consumer for processing.
pack-priv final int

Returns:

Buffer.SUCCESS, Buffer.FAILED, or Buffer.FULL
expandOrRetry
(E
the element to add
e
,
int
the thread's hash
h
,
int
the amount to increment by when rehashing
increment
,
boolean
false if CAS failed before this call
wasUncontended
)

Handles cases of updates involving initialization, resizing, creating new Buffers, and/or contention.

pack-priv static long
mix64(long z)

Computes Stafford variant 13 of 64-bit mix function.

public int
offer(E
the element to add
e
)

Implements com.github.benmanes.caffeine.cache.Buffer.offer.

Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.
public long
reads()

Implements com.github.benmanes.caffeine.cache.Buffer.reads.

Returns the number of elements that have been read from the buffer.
public long
writes()

Implements com.github.benmanes.caffeine.cache.Buffer.writes.

Returns the number of elements that have been written to the buffer.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ATTEMPTSback to summary
pack-priv static final int ATTEMPTS

The maximum number of attempts when trying to expand the table.

MAXIMUM_TABLE_SIZEback to summary
pack-priv static final int MAXIMUM_TABLE_SIZE

The bound on the table size.

NCPUback to summary
pack-priv static final int NCPU

Number of CPUS.

tableback to summary
pack-priv volatile @Nullable Buffer<E>[] table

Table of buffers. When non-null, size is a power of 2.

TABLE_BUSYback to summary
pack-priv static final VarHandle TABLE_BUSY
tableBusyback to summary
pack-priv volatile int tableBusy

Spinlock (locked via CAS) used when resizing and/or creating Buffers.

Constructor Detail

StripedBufferback to summary
pack-priv StripedBuffer()

Method Detail

casTableBusyback to summary
pack-priv final boolean casTableBusy()

CASes the tableBusy field from 0 to 1 to acquire lock.

createback to summary
protected abstract Buffer<E> create(E e)

Creates a new buffer instance after resizing to accommodate a producer.

Parameters
e:E

the producer's element

Returns:Buffer<E>

a newly created buffer populated with a single element

drainToback to summary
public void drainTo(Consumer<E> consumer)

Implements com.github.benmanes.caffeine.cache.Buffer.drainTo.

Doc from com.github.benmanes.caffeine.cache.Buffer.drainTo.

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.

Parameters
consumer:Consumer<E>

the action to perform on each element

Annotations
@Override
expandOrRetryback to summary
pack-priv final int expandOrRetry(E e, int h, int increment, boolean wasUncontended)

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:E

the element to add

h:int

the thread's hash

increment:int

the amount to increment by when rehashing

wasUncontended:boolean

false if CAS failed before this call

Returns:int

Buffer.SUCCESS, Buffer.FAILED, or Buffer.FULL

mix64back to summary
pack-priv static long mix64(long z)

Computes Stafford variant 13 of 64-bit mix function.

offerback to summary
public int offer(E e)

Implements com.github.benmanes.caffeine.cache.Buffer.offer.

Doc from com.github.benmanes.caffeine.cache.Buffer.offer.

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.

Parameters
e:E

the element to add

Returns:int

Buffer.SUCCESS, Buffer.FAILED, or Buffer.FULL

Annotations
@Override
readsback to summary
public long reads()

Implements com.github.benmanes.caffeine.cache.Buffer.reads.

Doc from com.github.benmanes.caffeine.cache.Buffer.reads.

Returns the number of elements that have been read from the buffer.

Returns:long

the number of elements read from this buffer

Annotations
@Override
writesback to summary
public long writes()

Implements com.github.benmanes.caffeine.cache.Buffer.writes.

Doc from com.github.benmanes.caffeine.cache.Buffer.writes.

Returns the number of elements that have been written to the buffer.

Returns:long

the number of elements written to this buffer

Annotations
@Override