jimu-decompiled/sources/io/reactivex/internal/queue/SpscArrayQueue.java
2025-05-13 19:24:51 +02:00

103 lines
3.1 KiB
Java

package io.reactivex.internal.queue;
import com.ubt.jimu.base.util.FileUtil;
import io.reactivex.internal.fuseable.SimplePlainQueue;
import io.reactivex.internal.util.Pow2;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceArray;
/* loaded from: classes2.dex */
public final class SpscArrayQueue<E> extends AtomicReferenceArray<E> implements SimplePlainQueue<E> {
private static final Integer MAX_LOOK_AHEAD_STEP = Integer.getInteger("jctools.spsc.max.lookahead.step", FileUtil.ZIP_BUFFER_SIZE);
private static final long serialVersionUID = -1296597691183856449L;
final AtomicLong consumerIndex;
final int lookAheadStep;
final int mask;
final AtomicLong producerIndex;
long producerLookAhead;
public SpscArrayQueue(int i) {
super(Pow2.a(i));
this.mask = length() - 1;
this.producerIndex = new AtomicLong();
this.consumerIndex = new AtomicLong();
this.lookAheadStep = Math.min(i / 4, MAX_LOOK_AHEAD_STEP.intValue());
}
int calcElementOffset(long j) {
return this.mask & ((int) j);
}
int calcElementOffset(long j, int i) {
return ((int) j) & i;
}
@Override // io.reactivex.internal.fuseable.SimpleQueue
public void clear() {
while (true) {
if (poll() == null && isEmpty()) {
return;
}
}
}
@Override // io.reactivex.internal.fuseable.SimpleQueue
public boolean isEmpty() {
return this.producerIndex.get() == this.consumerIndex.get();
}
E lvElement(int i) {
return get(i);
}
@Override // io.reactivex.internal.fuseable.SimpleQueue
public boolean offer(E e) {
if (e == null) {
throw new NullPointerException("Null is not a valid element");
}
int i = this.mask;
long j = this.producerIndex.get();
int calcElementOffset = calcElementOffset(j, i);
if (j >= this.producerLookAhead) {
long j2 = this.lookAheadStep + j;
if (lvElement(calcElementOffset(j2, i)) == null) {
this.producerLookAhead = j2;
} else if (lvElement(calcElementOffset) != null) {
return false;
}
}
soElement(calcElementOffset, e);
soProducerIndex(j + 1);
return true;
}
@Override // io.reactivex.internal.fuseable.SimplePlainQueue, io.reactivex.internal.fuseable.SimpleQueue
public E poll() {
long j = this.consumerIndex.get();
int calcElementOffset = calcElementOffset(j);
E lvElement = lvElement(calcElementOffset);
if (lvElement == null) {
return null;
}
soConsumerIndex(j + 1);
soElement(calcElementOffset, null);
return lvElement;
}
void soConsumerIndex(long j) {
this.consumerIndex.lazySet(j);
}
void soElement(int i, E e) {
lazySet(i, e);
}
void soProducerIndex(long j) {
this.producerIndex.lazySet(j);
}
public boolean offer(E e, E e2) {
return offer(e) && offer(e2);
}
}