Permalink
Browse files

Refactor fieldOffset calc to a new method and cleanup user code

  • Loading branch information...
nitsanw committed Mar 9, 2018
1 parent 9915e40 commit 2ac1a663b3a378225ea1269e25b6383751954c4c
Showing with 159 additions and 606 deletions.
  1. +7 −33 jctools-benchmarks/src/main/java/org/jctools/maps/cat_test/UnsafeCounter.java
  2. +4 −10 jctools-channels/src/main/java/org/jctools/util/UnsafeDirectByteBuffer.java
  3. +3 −8 jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMap.java
  4. +8 −27 jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMapLong.java
  5. +8 −22 jctools-core/src/main/java/org/jctools/maps/NonBlockingIdentityHashMap.java
  6. +5 −18 jctools-core/src/main/java/org/jctools/maps/NonBlockingSetInt.java
  7. +5 −28 jctools-core/src/main/java/org/jctools/queues/BaseLinkedQueue.java
  8. +4 −42 jctools-core/src/main/java/org/jctools/queues/BaseMpscLinkedArrayQueue.java
  9. +3 −28 jctools-core/src/main/java/org/jctools/queues/BaseSpscLinkedArrayQueue.java
  10. +2 −13 jctools-core/src/main/java/org/jctools/queues/LinkedQueueNode.java
  11. +3 −28 jctools-core/src/main/java/org/jctools/queues/MpmcArrayQueue.java
  12. +4 −42 jctools-core/src/main/java/org/jctools/queues/MpscArrayQueue.java
  13. +3 −28 jctools-core/src/main/java/org/jctools/queues/SpmcArrayQueue.java
  14. +3 −28 jctools-core/src/main/java/org/jctools/queues/SpscArrayQueue.java
  15. +12 −0 jctools-core/src/main/java/org/jctools/util/UnsafeAccess.java
  16. +1 −2 jctools-experimental/src/main/java/org/jctools/counters/FixedSizeStripedLongCounter.java
  17. +4 −17 jctools-experimental/src/main/java/org/jctools/queues/FFBuffer.java
  18. +22 −23 jctools-experimental/src/main/java/org/jctools/queues/InlinedCountersSpscConcurrentArrayQueue.java
  19. +5 −18 jctools-experimental/src/main/java/org/jctools/queues/MpmcConcurrentQueueStateMarkers.java
  20. +2 −9 jctools-experimental/src/main/java/org/jctools/queues/MpscOnSpscQueue.java
  21. +12 −57 jctools-experimental/src/main/java/org/jctools/queues/MpscRelaxedArrayQueue.java
  22. +5 −18 jctools-experimental/src/main/java/org/jctools/queues/MpscSequencedArrayQueue.java
  23. +5 −18 jctools-experimental/src/main/java/org/jctools/queues/alt/MpmcArrayConcurrentQueue.java
  24. +5 −18 jctools-experimental/src/main/java/org/jctools/queues/alt/MpscArrayConcurrentQueue.java
  25. +5 −18 jctools-experimental/src/main/java/org/jctools/queues/alt/SpscArrayConcurrentQueue.java
  26. +5 −20 jctools-experimental/src/main/java/org/jctools/queues/intrusive/MpscIntrusiveLinkedQueue.java
  27. +12 −25 jctools-experimental/src/main/java/org/jctools/sets/SingleWriterHashSet.java
  28. +2 −8 jctools-experimental/src/main/java/org/jctools/util/VolatileLongCell.java
@@ -1,46 +1,20 @@
package org.jctools.maps.cat_test;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
import org.jctools.util.UnsafeAccess;
import static org.jctools.util.UnsafeAccess.fieldOffset;
public final class UnsafeCounter extends Counter {
public String name() { return "Unsafe"; }
private static final Unsafe _unsafe = UtilUnsafe.getUnsafe();
private static final long CNT_OFFSET;
static { { // <clinit>
Field f = null;
try {
f = UnsafeCounter.class.getDeclaredField("_cnt");
} catch( java.lang.NoSuchFieldException e ) {
throw new Error(e);
}
CNT_OFFSET = _unsafe.objectFieldOffset(f);
}
}
private static final long CNT_OFFSET = fieldOffset(UnsafeCounter.class, "_cnt");
private long _cnt;
public long get(){ return _cnt; }
public void add( final long x ) {
long cnt=0;
do {
cnt = _cnt;
} while( !_unsafe.compareAndSwapLong(this,CNT_OFFSET,cnt,cnt+x) );
} while( !UnsafeAccess.UNSAFE.compareAndSwapLong(this,CNT_OFFSET,cnt,cnt+x) );
}
private static class UtilUnsafe {
private UtilUnsafe() { } // dummy private constructor
public static Unsafe getUnsafe() {
// Not on bootclasspath
if( UtilUnsafe.class.getClassLoader() == null )
return Unsafe.getUnsafe();
try {
final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
fld.setAccessible(true);
return (Unsafe) fld.get(UtilUnsafe.class);
} catch (Exception e) {
throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
}
}
}
}
@@ -3,20 +3,14 @@
import java.nio.Buffer;
import java.nio.ByteBuffer;
import static org.jctools.util.UnsafeAccess.UNSAFE;
public class UnsafeDirectByteBuffer
{
private static final long addressOffset;
static {
try {
addressOffset = UnsafeAccess.UNSAFE.objectFieldOffset(Buffer.class
.getDeclaredField("address"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static final long addressOffset = UnsafeAccess.fieldOffset(Buffer.class, "address");
public static long getAddress(ByteBuffer buffy) {
return UnsafeAccess.UNSAFE.getLong(buffy, addressOffset);
return UNSAFE.getLong(buffy, addressOffset);
}
public static ByteBuffer allocateAlignedByteBuffer(int capacity, long align) {
@@ -17,13 +17,13 @@
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
/**
* A lock-free alternate implementation of {@link java.util.concurrent.ConcurrentHashMap}
@@ -100,13 +100,8 @@ private static long rawIndex(final Object[] ary, final int idx) {
}
// --- Setup to use Unsafe
private static final long _kvs_offset;
static { // <clinit>
Field f = null;
try { f = NonBlockingHashMap.class.getDeclaredField("_kvs"); }
catch( java.lang.NoSuchFieldException e ) { throw new RuntimeException(e); }
_kvs_offset = UNSAFE.objectFieldOffset(f);
}
private static final long _kvs_offset = fieldOffset(NonBlockingHashMap.class, "_kvs");
private final boolean CAS_kvs( final Object[] oldkvs, final Object[] newkvs ) {
return UNSAFE.compareAndSwapObject(this, _kvs_offset, oldkvs, newkvs );
}
@@ -13,27 +13,17 @@
*/
package org.jctools.maps;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import org.jctools.util.RangeUtil;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.jctools.util.RangeUtil;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
/**
@@ -125,18 +115,9 @@ private static long rawIndex(final long[] ary, final int idx) {
}
// --- Bits to allow Unsafe CAS'ing of the CHM field
private static final long _chm_offset;
private static final long _val_1_offset;
static { // <clinit>
Field f;
try { f = NonBlockingHashMapLong.class.getDeclaredField("_chm"); }
catch( java.lang.NoSuchFieldException e ) { throw new RuntimeException(e); }
_chm_offset = UNSAFE.objectFieldOffset(f);
try { f = NonBlockingHashMapLong.class.getDeclaredField("_val_1"); }
catch( java.lang.NoSuchFieldException e ) { throw new RuntimeException(e); }
_val_1_offset = UNSAFE.objectFieldOffset(f);
}
private static final long _chm_offset = fieldOffset(NonBlockingHashMapLong.class, "_chm");
private static final long _val_1_offset = fieldOffset(NonBlockingHashMapLong.class, "_val_1");
private final boolean CAS( final long offset, final Object old, final Object nnn ) {
return UNSAFE.compareAndSwapObject(this, offset, old, nnn );
}
@@ -12,27 +12,18 @@
* limitations under the License.
*/
package org.jctools.maps;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import org.jctools.util.RangeUtil;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.jctools.util.RangeUtil;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
/**
* A lock-free alternate implementation of {@link java.util.concurrent.ConcurrentHashMap}
@@ -110,13 +101,8 @@ private static long rawIndex(final Object[] ary, final int idx) {
}
// --- Setup to use Unsafe
private static final long _kvs_offset;
static { // <clinit>
Field f = null;
try { f = NonBlockingHashMap.class.getDeclaredField("_kvs"); }
catch( java.lang.NoSuchFieldException e ) { throw new RuntimeException(e); }
_kvs_offset = UNSAFE.objectFieldOffset(f);
}
private static final long _kvs_offset = fieldOffset(NonBlockingHashMap.class, "_kvs");
private final boolean CAS_kvs( final Object[] oldkvs, final Object[] newkvs ) {
return UNSAFE.compareAndSwapObject(this, _kvs_offset, oldkvs, newkvs );
}
@@ -13,6 +13,7 @@
*/
package org.jctools.maps;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
import java.io.IOException;
import java.io.Serializable;
@@ -50,15 +51,8 @@
private static final long serialVersionUID = 1234123412341234123L;
// --- Bits to allow atomic update of the NBSI
private static final long _nbsi_offset;
static { // <clinit>
Field f = null;
try {
f = NonBlockingSetInt.class.getDeclaredField("_nbsi");
} catch( java.lang.NoSuchFieldException e ) {
}
_nbsi_offset = UNSAFE.objectFieldOffset(f);
}
private static final long _nbsi_offset = fieldOffset(NonBlockingSetInt.class, "_nbsi");
private final boolean CAS_nbsi( NBSI old, NBSI nnn ) {
return UNSAFE.compareAndSwapObject(this, _nbsi_offset, old, nnn );
}
@@ -232,15 +226,8 @@ private final boolean CAS( int idx, long old, long nnn ) {
// The New Table, only set once to non-zero during a resize.
// Must be atomically set.
private NBSI _new;
private static final long _new_offset;
static { // <clinit>
Field f = null;
try {
f = NBSI.class.getDeclaredField("_new");
} catch( java.lang.NoSuchFieldException e ) {
}
_new_offset = UNSAFE.objectFieldOffset(f);
}
private static final long _new_offset = fieldOffset(NBSI.class, "_new");
private final boolean CAS_new( NBSI nnn ) {
return UNSAFE.compareAndSwapObject(this, _new_offset, null, nnn );
}
@@ -13,11 +13,14 @@
*/
package org.jctools.queues;
import org.jctools.util.UnsafeAccess;
import java.lang.reflect.Field;
import java.util.AbstractQueue;
import java.util.Iterator;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
abstract class BaseLinkedQueuePad0<E> extends AbstractQueue<E> implements MessagePassingQueue<E>
{
@@ -28,20 +31,7 @@
// $gen:ordered-fields
abstract class BaseLinkedQueueProducerNodeRef<E> extends BaseLinkedQueuePad0<E>
{
protected final static long P_NODE_OFFSET;
static
{
try
{
final Field pNodeField = BaseLinkedQueueProducerNodeRef.class.getDeclaredField("producerNode");
P_NODE_OFFSET = UNSAFE.objectFieldOffset(pNodeField);
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
protected final static long P_NODE_OFFSET = fieldOffset(BaseLinkedQueueProducerNodeRef.class, "producerNode");
protected LinkedQueueNode<E> producerNode;
@@ -77,20 +67,7 @@ protected final boolean casProducerNode(LinkedQueueNode<E> expect, LinkedQueueNo
//$gen:ordered-fields
abstract class BaseLinkedQueueConsumerNodeRef<E> extends BaseLinkedQueuePad1<E>
{
protected final static long C_NODE_OFFSET;
static
{
try
{
final Field cNodeField = BaseLinkedQueueConsumerNodeRef.class.getDeclaredField("consumerNode");
C_NODE_OFFSET = UNSAFE.objectFieldOffset(cNodeField);
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
protected final static long C_NODE_OFFSET = fieldOffset(BaseLinkedQueueConsumerNodeRef.class,"consumerNode");
protected LinkedQueueNode<E> consumerNode;
@@ -26,6 +26,7 @@
import static org.jctools.queues.LinkedArrayQueueUtil.length;
import static org.jctools.queues.LinkedArrayQueueUtil.modifiedCalcElementOffset;
import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
import static org.jctools.util.UnsafeRefArrayAccess.lvElement;
import static org.jctools.util.UnsafeRefArrayAccess.soElement;
@@ -38,20 +39,7 @@
// $gen:ordered-fields
abstract class BaseMpscLinkedArrayQueueProducerFields<E> extends BaseMpscLinkedArrayQueuePad1<E>
{
private final static long P_INDEX_OFFSET;
static
{
try
{
Field iField = BaseMpscLinkedArrayQueueProducerFields.class.getDeclaredField("producerIndex");
P_INDEX_OFFSET = UNSAFE.objectFieldOffset(iField);
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
private final static long P_INDEX_OFFSET = fieldOffset(BaseMpscLinkedArrayQueueProducerFields.class, "producerIndex");
protected long producerIndex;
@@ -81,20 +69,7 @@ final boolean casProducerIndex(long expect, long newValue)
// $gen:ordered-fields
abstract class BaseMpscLinkedArrayQueueConsumerFields<E> extends BaseMpscLinkedArrayQueuePad2<E>
{
private final static long C_INDEX_OFFSET;
static
{
try
{
Field iField = BaseMpscLinkedArrayQueueConsumerFields.class.getDeclaredField("consumerIndex");
C_INDEX_OFFSET = UNSAFE.objectFieldOffset(iField);
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
private final static long C_INDEX_OFFSET = fieldOffset(BaseMpscLinkedArrayQueueConsumerFields.class,"consumerIndex");
protected long consumerMask;
protected E[] consumerBuffer;
@@ -121,20 +96,7 @@ final void soConsumerIndex(long newValue)
// $gen:ordered-fields
abstract class BaseMpscLinkedArrayQueueColdProducerFields<E> extends BaseMpscLinkedArrayQueuePad3<E>
{
private final static long P_LIMIT_OFFSET;
static
{
try
{
Field iField = BaseMpscLinkedArrayQueueColdProducerFields.class.getDeclaredField("producerLimit");
P_LIMIT_OFFSET = UNSAFE.objectFieldOffset(iField);
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
private final static long P_LIMIT_OFFSET = fieldOffset(BaseMpscLinkedArrayQueueColdProducerFields.class,"producerLimit");
private volatile long producerLimit;
protected long producerMask;
Oops, something went wrong.

0 comments on commit 2ac1a66

Please sign in to comment.