Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jiachun.fjc committed Jun 11, 2018
1 parent 00e97eb commit b727143
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package org.jupiter.common.concurrent.queues;

import org.jupiter.common.util.internal.UnsafeUtil;
import sun.misc.Unsafe;

import java.util.AbstractQueue;
import java.util.Iterator;
Expand All @@ -45,7 +46,11 @@ abstract class BaseLinkedQueuePad0<E> extends AbstractQueue<E> implements Messag

// $gen:ordered-fields
abstract class BaseLinkedQueueProducerNodeRef<E> extends BaseLinkedQueuePad0<E> {
final static long P_NODE_OFFSET = UnsafeUtil.objectFieldOffset(BaseLinkedQueueProducerNodeRef.class, "producerNode");

static final long P_NODE_OFFSET =
UnsafeUtil.objectFieldOffset(BaseLinkedQueueProducerNodeRef.class, "producerNode");

static final Unsafe unsafe = UnsafeUtil.getUnsafe();

private LinkedQueueNode<E> producerNode;

Expand All @@ -55,12 +60,12 @@ final void spProducerNode(LinkedQueueNode<E> newValue) {

@SuppressWarnings("unchecked")
final LinkedQueueNode<E> lvProducerNode() {
return (LinkedQueueNode<E>) UnsafeUtil.getUnsafe().getObjectVolatile(this, P_NODE_OFFSET);
return (LinkedQueueNode<E>) unsafe.getObjectVolatile(this, P_NODE_OFFSET);
}

@SuppressWarnings("unchecked")
final boolean casProducerNode(LinkedQueueNode<E> expect, LinkedQueueNode<E> newValue) {
return UnsafeUtil.getUnsafe().compareAndSwapObject(this, P_NODE_OFFSET, expect, newValue);
return unsafe.compareAndSwapObject(this, P_NODE_OFFSET, expect, newValue);
}

final LinkedQueueNode<E> lpProducerNode() {
Expand All @@ -76,7 +81,9 @@ abstract class BaseLinkedQueuePad1<E> extends BaseLinkedQueueProducerNodeRef<E>

//$gen:ordered-fields
abstract class BaseLinkedQueueConsumerNodeRef<E> extends BaseLinkedQueuePad1<E> {
private final static long C_NODE_OFFSET = UnsafeUtil.objectFieldOffset(BaseLinkedQueueConsumerNodeRef.class, "consumerNode");

private static final long C_NODE_OFFSET =
UnsafeUtil.objectFieldOffset(BaseLinkedQueueConsumerNodeRef.class, "consumerNode");

private LinkedQueueNode<E> consumerNode;

Expand All @@ -86,7 +93,7 @@ final void spConsumerNode(LinkedQueueNode<E> newValue) {

@SuppressWarnings("unchecked")
final LinkedQueueNode<E> lvConsumerNode() {
return (LinkedQueueNode<E>) UnsafeUtil.getUnsafe().getObjectVolatile(this, C_NODE_OFFSET);
return (LinkedQueueNode<E>) unsafe.getObjectVolatile(this, C_NODE_OFFSET);
}

final LinkedQueueNode<E> lpConsumerNode() {
Expand Down Expand Up @@ -116,7 +123,7 @@ public final Iterator<E> iterator() {

@Override
public String toString() {
return this.getClass().getName();
return getClass().getName();
}

protected final LinkedQueueNode<E> newNode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
package org.jupiter.common.concurrent.queues;

import org.jupiter.common.util.internal.UnsafeUtil;
import sun.misc.Unsafe;

/**
* Forked from <a href="https://github.com/JCTools/JCTools">JCTools</a>.
*/
final class LinkedQueueNode<E> {

private final static long NEXT_OFFSET = UnsafeUtil.objectFieldOffset(LinkedQueueNode.class, "next");
private static final long NEXT_OFFSET = UnsafeUtil.objectFieldOffset(LinkedQueueNode.class, "next");

private static final Unsafe unsafe = UnsafeUtil.getUnsafe();

private E value;
@SuppressWarnings("unused")
Expand Down Expand Up @@ -70,7 +73,7 @@ public void spValue(E newValue) {
}

public void soNext(LinkedQueueNode<E> n) {
UnsafeUtil.getUnsafe().putOrderedObject(this, NEXT_OFFSET, n);
unsafe.putOrderedObject(this, NEXT_OFFSET, n);
}

public LinkedQueueNode<E> lvNext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
package org.jupiter.common.concurrent.queues;

import org.jupiter.common.util.internal.UnsafeUtil;
import sun.misc.Unsafe;

/**
* This is a direct Java port of the MPSC algorithm as presented
Expand Down Expand Up @@ -229,10 +229,11 @@ public void fill(Supplier<E> s, WaitStrategy wait, ExitCondition exit) {
// $gen:ignore
@SuppressWarnings("unchecked")
protected LinkedQueueNode<E> xchgProducerNode(LinkedQueueNode<E> nextNode) {
final Unsafe us = unsafe; // stack copy
Object oldNode;
do {
oldNode = lvProducerNode();
} while (!UnsafeUtil.getUnsafe().compareAndSwapObject(this, P_NODE_OFFSET, oldNode, nextNode));
} while (!us.compareAndSwapObject(this, P_NODE_OFFSET, oldNode, nextNode));
return (LinkedQueueNode<E>) oldNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

// Fairly fast random numbers
public final class SimpleRandom {
private final static long multiplier = 0x5DEECE66DL;
private final static long addend = 0xBL;
private final static long mask = (1L << 48) - 1;
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
static final AtomicLong seq = new AtomicLong(-715159705);
private long seed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
*/
public class HackSystem {

public final static InputStream in = System.in;
public static final InputStream in = System.in;

private static final UnsafeReferenceFieldUpdater<ByteArrayOutputStream, byte[]> bufUpdater =
UnsafeUpdater.newReferenceFieldUpdater(ByteArrayOutputStream.class, "buf");

private static ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);

public final static PrintStream out = new PrintStream(buf);
public static final PrintStream out = new PrintStream(buf);
@SuppressWarnings("unused")
public final static PrintStream err = out;
public static final PrintStream err = out;

public static String getBufString() {
String value = buf.toString();
Expand Down

0 comments on commit b727143

Please sign in to comment.