Skip to content

Commit

Permalink
pp
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Nov 19, 2016
1 parent d7eaa1c commit c67c695
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/org/jgroups/util/RingBuffer.java
@@ -1,23 +1,25 @@
package org.jgroups.util; package org.jgroups.util;


import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;



/** /**
* Ring buffer of fixed capacity designed for multiple writers but only a single reader. Advancing the read or * Ring buffer of fixed capacity designed for multiple writers but only a single reader. Advancing the read or
* write index blocks until it is possible to do so. * write index blocks until it is possible to do so.
* @author Bela Ban * @author Bela Ban
* @since 4.0 * @since 4.0
*/ */
public class RingBuffer<T> { public class RingBuffer<T> {
protected final T[] buf; protected final T[] buf;
protected int ri, wi; // read and write indices protected int ri, wi; // read and write indices
protected int count; // number of elements available to be read protected int count; // number of elements available to be read
protected final Lock lock=new ReentrantLock(); protected final Lock lock=new ReentrantLock();
protected final java.util.concurrent.locks.Condition not_empty=lock.newCondition(); // reader can block on this protected final Condition not_empty=lock.newCondition(); // reader can block on this
protected final java.util.concurrent.locks.Condition not_full=lock.newCondition(); // writes can block on this protected final Condition not_full=lock.newCondition(); // writes can block on this


public RingBuffer(Class<T> element_type, int capacity) { public RingBuffer(Class<T> element_type, int capacity) {
int c=Util.getNextHigherPowerOfTwo(capacity); // power of 2 for faster mod operation int c=Util.getNextHigherPowerOfTwo(capacity); // power of 2 for faster mod operation
Expand Down

0 comments on commit c67c695

Please sign in to comment.