Skip to content

1.x Provide public constant instead of UnsafeAccess.isUnsafeAvailable() #3815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public ConcatMapSubscriber(Subscriber<? super R> actual,
this.wip = new AtomicInteger();
this.error = new AtomicReference<Throwable>();
Queue<Object> q;
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
q = new SpscArrayQueue<Object>(prefetch);
} else {
q = new SpscAtomicArrayQueue<Object>(prefetch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public OnSubscribePublishMulticast(int prefetch, boolean delayError) {
}
this.prefetch = prefetch;
this.delayError = delayError;
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
this.queue = new SpscArrayQueue<T>(prefetch);
} else {
this.queue = new SpscAtomicArrayQueue<T>(prefetch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public EagerInnerSubscriber(EagerOuterSubscriber<?, T> parent, int bufferSize) {
super();
this.parent = parent;
Queue<Object> q;
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
q = new SpscArrayQueue<Object>(bufferSize);
} else {
q = new SpscAtomicArrayQueue<Object>(bufferSize);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/operators/OperatorMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ protected void queueScalar(T value) {
q = new SpscUnboundedAtomicArrayQueue<Object>(RxRingBuffer.SIZE);
} else {
if (Pow2.isPowerOfTwo(mc)) {
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
q = new SpscArrayQueue<Object>(mc);
} else {
q = new SpscAtomicArrayQueue<Object>(mc);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/operators/OperatorObserveOn.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public ObserveOnSubscriber(Scheduler scheduler, Subscriber<? super T> child, boo
this.delayError = delayError;
this.on = NotificationLite.instance();
this.bufferSize = (bufferSize > 0) ? bufferSize : RxRingBuffer.SIZE;
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
queue = new SpscArrayQueue<Object>(this.bufferSize);
} else {
queue = new SpscAtomicArrayQueue<Object>(this.bufferSize);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/operators/OperatorPublish.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static final class PublishSubscriber<T> extends Subscriber<T> implements Subscri
boolean missed;

public PublishSubscriber(AtomicReference<PublishSubscriber<T>> current) {
this.queue = UnsafeAccess.isUnsafeAvailable()
this.queue = UnsafeAccess.IS_UNSAFE_AVAILABLE
? new SpscArrayQueue<Object>(RxRingBuffer.SIZE)
: new SynchronizedQueue<Object>(RxRingBuffer.SIZE);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/operators/OperatorScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public InitialProducer(R initialValue, Subscriber<? super R> child) {
this.child = child;
Queue<Object> q;
// TODO switch to the linked-array based queue once available
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
q = new SpscLinkedQueue<Object>(); // new SpscUnboundedArrayQueue<R>(8);
} else {
q = new SpscLinkedAtomicQueue<Object>(); // new SpscUnboundedAtomicArrayQueue<R>(8);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rx/internal/operators/UnicastSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ public State(int capacityHint, Action0 onTerminated) {

Queue<Object> q;
if (capacityHint > 1) {
q = UnsafeAccess.isUnsafeAvailable()
q = UnsafeAccess.IS_UNSAFE_AVAILABLE
? new SpscUnboundedArrayQueue<Object>(capacityHint)
: new SpscUnboundedAtomicArrayQueue<Object>(capacityHint);
} else {
q = UnsafeAccess.isUnsafeAvailable()
q = UnsafeAccess.IS_UNSAFE_AVAILABLE
? new SpscLinkedQueue<Object>()
: new SpscLinkedAtomicQueue<Object>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/producers/QueuedProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class QueuedProducer<T> extends AtomicLong implements Producer, Obs
* @param child the target child subscriber
*/
public QueuedProducer(Subscriber<? super T> child) {
this(child, UnsafeAccess.isUnsafeAvailable()
this(child, UnsafeAccess.IS_UNSAFE_AVAILABLE
? new SpscLinkedQueue<Object>() : new SpscLinkedAtomicQueue<Object>());
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class QueuedValueProducer<T> extends AtomicLong implements Producer
* @param child the target child subscriber
*/
public QueuedValueProducer(Subscriber<? super T> child) {
this(child, UnsafeAccess.isUnsafeAvailable()
this(child, UnsafeAccess.IS_UNSAFE_AVAILABLE
? new SpscLinkedQueue<Object>() : new SpscLinkedAtomicQueue<Object>());
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/util/ObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void call() {
protected abstract T createObject();

private void initialize(final int min) {
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
pool = new MpmcArrayQueue<T>(Math.max(maxSize, 1024));
} else {
pool = new ConcurrentLinkedQueue<T>();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rx/internal/util/RxRingBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
public class RxRingBuffer implements Subscription {

public static RxRingBuffer getSpscInstance() {
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return new RxRingBuffer(SPSC_POOL, SIZE);
} else {
return new RxRingBuffer();
}
}

public static RxRingBuffer getSpmcInstance() {
if (UnsafeAccess.isUnsafeAvailable()) {
if (UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return new RxRingBuffer(SPMC_POOL, SIZE);
} else {
return new RxRingBuffer();
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/rx/internal/util/unsafe/UnsafeAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
import sun.misc.Unsafe;

/**
* All use of this class MUST first check that UnsafeAccess.isUnsafeAvailable() == true
* All use of this class MUST first check that {@code UnsafeAccess.IS_UNSAFE_AVAILABLE == true}
* otherwise NPEs will happen in environments without "suc.misc.Unsafe" such as Android.
*/
public final class UnsafeAccess {
private UnsafeAccess() {
throw new IllegalStateException("No instances!");
}

public static final Unsafe UNSAFE;
static final Unsafe UNSAFE;
public static final boolean IS_UNSAFE_AVAILABLE;

static {
Unsafe u = null;
try {
Expand All @@ -45,10 +47,7 @@ private UnsafeAccess() {
// do nothing, hasUnsafe() will return false
}
UNSAFE = u;
}

public static boolean isUnsafeAvailable() {
return UNSAFE != null;
IS_UNSAFE_AVAILABLE = UNSAFE != null;
}

/*
Expand Down
44 changes: 22 additions & 22 deletions src/test/java/rx/internal/util/JCToolsQueueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void await(CyclicBarrier cb) {
}
@Test
public void casBasedUnsafe() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
long offset = UnsafeAccess.addressOf(IntField.class, "value");
Expand Down Expand Up @@ -74,7 +74,7 @@ public void powerOfTwo() {

@Test(expected = NullPointerException.class)
public void testMpmcArrayQueueNull() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
MpmcArrayQueue<Integer> q = new MpmcArrayQueue<Integer>(16);
Expand All @@ -83,7 +83,7 @@ public void testMpmcArrayQueueNull() {

@Test(expected = UnsupportedOperationException.class)
public void testMpmcArrayQueueIterator() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
MpmcArrayQueue<Integer> q = new MpmcArrayQueue<Integer>(16);
Expand All @@ -92,7 +92,7 @@ public void testMpmcArrayQueueIterator() {

@Test
public void testMpmcArrayQueueOfferPoll() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
Queue<Integer> q = new MpmcArrayQueue<Integer>(128);
Expand All @@ -102,7 +102,7 @@ public void testMpmcArrayQueueOfferPoll() {

@Test
public void testMpmcOfferUpToCapacity() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
int n = 128;
Expand Down Expand Up @@ -176,7 +176,7 @@ public void run() {

@Test(expected = UnsupportedOperationException.class)
public void testMpscLinkedQueueIterator() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
MpscLinkedQueue<Integer> q = new MpscLinkedQueue<Integer>();
Expand All @@ -185,7 +185,7 @@ public void testMpscLinkedQueueIterator() {

@Test(expected = NullPointerException.class)
public void testMpscLinkedQueueNull() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
MpscLinkedQueue<Integer> q = new MpscLinkedQueue<Integer>();
Expand All @@ -194,7 +194,7 @@ public void testMpscLinkedQueueNull() {

@Test
public void testMpscLinkedQueueOfferPoll() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
MpscLinkedQueue<Integer> q = new MpscLinkedQueue<Integer>();
Expand All @@ -203,7 +203,7 @@ public void testMpscLinkedQueueOfferPoll() {
}
@Test(timeout = 2000)
public void testMpscLinkedQueuePipelined() throws InterruptedException {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
final MpscLinkedQueue<Integer> q = new MpscLinkedQueue<Integer>();
Expand Down Expand Up @@ -273,7 +273,7 @@ protected void testOfferPoll(Queue<Integer> q) {

@Test(expected = NullPointerException.class)
public void testSpmcArrayQueueNull() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpmcArrayQueue<Integer> q = new SpmcArrayQueue<Integer>(16);
Expand All @@ -282,7 +282,7 @@ public void testSpmcArrayQueueNull() {

@Test
public void testSpmcArrayQueueOfferPoll() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
Queue<Integer> q = new SpmcArrayQueue<Integer>(128);
Expand All @@ -291,7 +291,7 @@ public void testSpmcArrayQueueOfferPoll() {
}
@Test(expected = UnsupportedOperationException.class)
public void testSpmcArrayQueueIterator() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpmcArrayQueue<Integer> q = new SpmcArrayQueue<Integer>(16);
Expand All @@ -300,7 +300,7 @@ public void testSpmcArrayQueueIterator() {

@Test
public void testSpmcOfferUpToCapacity() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
int n = 128;
Expand All @@ -313,7 +313,7 @@ public void testSpmcOfferUpToCapacity() {

@Test(expected = NullPointerException.class)
public void testSpscArrayQueueNull() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpscArrayQueue<Integer> q = new SpscArrayQueue<Integer>(16);
Expand All @@ -322,7 +322,7 @@ public void testSpscArrayQueueNull() {

@Test
public void testSpscArrayQueueOfferPoll() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
Queue<Integer> q = new SpscArrayQueue<Integer>(128);
Expand All @@ -331,7 +331,7 @@ public void testSpscArrayQueueOfferPoll() {
}
@Test(expected = UnsupportedOperationException.class)
public void testSpscArrayQueueIterator() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpscArrayQueue<Integer> q = new SpscArrayQueue<Integer>(16);
Expand Down Expand Up @@ -384,7 +384,7 @@ public void run() {

@Test(expected = UnsupportedOperationException.class)
public void testSpscLinkedQueueIterator() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpscLinkedQueue<Integer> q = new SpscLinkedQueue<Integer>();
Expand All @@ -393,7 +393,7 @@ public void testSpscLinkedQueueIterator() {

@Test(expected = NullPointerException.class)
public void testSpscLinkedQueueNull() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpscLinkedQueue<Integer> q = new SpscLinkedQueue<Integer>();
Expand All @@ -402,7 +402,7 @@ public void testSpscLinkedQueueNull() {

@Test
public void testSpscLinkedQueueOfferPoll() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
SpscLinkedQueue<Integer> q = new SpscLinkedQueue<Integer>();
Expand All @@ -412,7 +412,7 @@ public void testSpscLinkedQueueOfferPoll() {

@Test(timeout = 2000)
public void testSpscLinkedQueuePipelined() throws InterruptedException {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
final SpscLinkedQueue<Integer> q = new SpscLinkedQueue<Integer>();
Expand Down Expand Up @@ -442,7 +442,7 @@ public void run() {

@Test
public void testSpscOfferUpToCapacity() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
int n = 128;
Expand All @@ -455,7 +455,7 @@ public void testSpscOfferUpToCapacity() {

@Test(expected = InternalError.class)
public void testUnsafeAccessAddressOf() {
if (!UnsafeAccess.isUnsafeAvailable()) {
if (!UnsafeAccess.IS_UNSAFE_AVAILABLE) {
return;
}
UnsafeAccess.addressOf(Object.class, "field");
Expand Down