|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.jgroups.util.RingBufferLockless<T>
public class RingBufferLockless<T>
Ring buffer, implemented with a circular array. Designed for multiple producers (add()) and a single consumer (remove()). Note that the remove() methods are not reentrant, so multiple consumers won't work correctly !
The buffer has a fixed capacity, and a low (LOW), highest delivered (HD) and highest received (HR) seqno. An element with a sequence number (seqno) > low + capacity or < HD will get discarded. Elements are added after HD, but cannot wrap around beyond LOW. Addition doesn't need to be sequential, e.g. adding 5, 6, 8 is OK (as long as a seqno doesn't pass LOW). Addition may advance HR. Addition of elements that are already present is a no-op, and will not set the element again. Removal of elements starts at HD+1; any non-null element is removed and HD is advanced accordingly. If a remove method is called with nullify=true, then removed elements are nulled and LOW is advanced as well (LOW=HD). Note that all removals in a given RingBufferLockless must either have nullify=true, or all must be false. It is not permitted to do some removals with nullify=true, and others with nullify=false, in the same RingBufferLockless. Thestable(long)
method is called periodically; it nulls all elements between LOW and HD and advances LOW
to HD.
The design of RingBufferLockless is discussed in doc/design/RingBufferLockless.txt.
Nested Class Summary | |
---|---|
protected class |
RingBufferLockless.RingBufferIterator<T>
|
Field Summary | |
---|---|
protected java.util.concurrent.atomic.AtomicReferenceArray<T> |
buf
Atomic ref array so that elements can be checked for null and set atomically |
protected java.util.concurrent.locks.Condition |
buffer_full
|
protected long |
hd
The highest delivered seqno. |
protected java.util.concurrent.atomic.AtomicLong |
hr
The highest received seqno. |
protected java.util.concurrent.locks.Lock |
lock
Lock for adders to block on when the buffer is full |
protected long |
low
The lowest seqno. |
protected long |
offset
|
protected java.util.concurrent.atomic.AtomicBoolean |
processing
|
protected boolean |
running
|
Constructor Summary | |
---|---|
RingBufferLockless(int capacity,
long offset)
Creates a RingBuffer |
Method Summary | |
---|---|
T |
_get(long seqno)
Only used for testing !! |
boolean |
add(long seqno,
T element)
|
boolean |
add(long seqno,
T element,
boolean block)
Adds a new element to the buffer |
protected boolean |
block(long seqno)
|
int |
capacity()
|
protected int |
count(boolean missing)
|
void |
destroy()
|
T |
get(long seqno)
|
java.util.List<T> |
get(long from,
long to)
Returns a list of messages in the range [from .. |
long[] |
getDigest()
|
long |
getHighestDelivered()
|
long |
getHighestReceived()
|
long |
getLow()
|
SeqnoList |
getMissing()
|
java.util.concurrent.atomic.AtomicBoolean |
getProcessing()
|
protected int |
index(long seqno)
|
java.util.Iterator<T> |
iterator()
Returns an iterator over the elements of the ring buffer in the range [HD+1 .. |
int |
missing()
|
T |
remove()
Removes the next element (at hd +1). |
T |
remove(boolean nullify)
Removes the next element (at hd +1). |
java.util.List<T> |
removeMany(java.util.concurrent.atomic.AtomicBoolean processing,
boolean nullify,
int max_results)
|
java.util.List<T> |
removeMany(boolean nullify,
int max_results)
|
double |
saturation()
|
void |
setHighestDelivered(long hd)
|
int |
size()
|
int |
spaceUsed()
|
void |
stable(long seqno)
Nulls elements between low and seqno and forwards low |
java.lang.String |
toString()
|
protected static void |
validate(long seqno)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected final java.util.concurrent.atomic.AtomicReferenceArray<T> buf
protected volatile long low
protected volatile long hd
protected final java.util.concurrent.atomic.AtomicLong hr
protected final long offset
protected final java.util.concurrent.locks.Lock lock
protected final java.util.concurrent.locks.Condition buffer_full
protected volatile boolean running
protected final java.util.concurrent.atomic.AtomicBoolean processing
Constructor Detail |
---|
public RingBufferLockless(int capacity, long offset)
capacity
- The number of elements the ring buffer's array should holdoffset
- The offset. The first element to be added has to be offset +1.Method Detail |
---|
public long getLow()
public long getHighestDelivered()
public void setHighestDelivered(long hd)
public long getHighestReceived()
public long[] getDigest()
public java.util.concurrent.atomic.AtomicBoolean getProcessing()
public boolean add(long seqno, T element)
public boolean add(long seqno, T element, boolean block)
seqno
- The seqno of the elementelement
- The elementblock
- If true, add() will block when the buffer is full until there is space. Else, add() will
return immediately, either successfully or unsuccessfully (if the buffer is full)
public T remove(boolean nullify)
nullify
- Nulls the element in the array if true
public T remove()
public java.util.List<T> removeMany(boolean nullify, int max_results)
public java.util.List<T> removeMany(java.util.concurrent.atomic.AtomicBoolean processing, boolean nullify, int max_results)
public T get(long seqno)
public T _get(long seqno)
public java.util.List<T> get(long from, long to)
from
- to
-
public void stable(long seqno)
public void destroy()
public final int capacity()
public int size()
public int missing()
public int spaceUsed()
public double saturation()
public SeqnoList getMissing()
public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
java.util.NoSuchElementException
- is HD is moved forward during the iterationpublic java.lang.String toString()
toString
in class java.lang.Object
protected static final void validate(long seqno)
protected int index(long seqno)
protected boolean block(long seqno)
protected int count(boolean missing)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |