226 lines
6.7 KiB
Java
226 lines
6.7 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 SpscLinkedArrayQueue<T> implements SimplePlainQueue<T> {
|
|
static final int i = Integer.getInteger("jctools.spsc.max.lookahead.step", FileUtil.ZIP_BUFFER_SIZE).intValue();
|
|
private static final Object j = new Object();
|
|
int b;
|
|
long c;
|
|
final int d;
|
|
AtomicReferenceArray<Object> e;
|
|
final int f;
|
|
AtomicReferenceArray<Object> g;
|
|
final AtomicLong a = new AtomicLong();
|
|
final AtomicLong h = new AtomicLong();
|
|
|
|
public SpscLinkedArrayQueue(int i2) {
|
|
int a = Pow2.a(Math.max(8, i2));
|
|
int i3 = a - 1;
|
|
AtomicReferenceArray<Object> atomicReferenceArray = new AtomicReferenceArray<>(a + 1);
|
|
this.e = atomicReferenceArray;
|
|
this.d = i3;
|
|
a(a);
|
|
this.g = atomicReferenceArray;
|
|
this.f = i3;
|
|
this.c = i3 - 1;
|
|
b(0L);
|
|
}
|
|
|
|
private boolean a(AtomicReferenceArray<Object> atomicReferenceArray, T t, long j2, int i2) {
|
|
a(atomicReferenceArray, i2, t);
|
|
b(j2 + 1);
|
|
return true;
|
|
}
|
|
|
|
private static int b(int i2) {
|
|
return i2;
|
|
}
|
|
|
|
private AtomicReferenceArray<Object> b(AtomicReferenceArray<Object> atomicReferenceArray, int i2) {
|
|
b(i2);
|
|
AtomicReferenceArray<Object> atomicReferenceArray2 = (AtomicReferenceArray) a(atomicReferenceArray, i2);
|
|
a(atomicReferenceArray, i2, (Object) null);
|
|
return atomicReferenceArray2;
|
|
}
|
|
|
|
private long c() {
|
|
return this.h.get();
|
|
}
|
|
|
|
private long d() {
|
|
return this.a.get();
|
|
}
|
|
|
|
private long e() {
|
|
return this.h.get();
|
|
}
|
|
|
|
private long f() {
|
|
return this.a.get();
|
|
}
|
|
|
|
@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 f() == e();
|
|
}
|
|
|
|
@Override // io.reactivex.internal.fuseable.SimpleQueue
|
|
public boolean offer(T t) {
|
|
if (t == null) {
|
|
throw new NullPointerException("Null is not a valid element");
|
|
}
|
|
AtomicReferenceArray<Object> atomicReferenceArray = this.e;
|
|
long d = d();
|
|
int i2 = this.d;
|
|
int a = a(d, i2);
|
|
if (d < this.c) {
|
|
return a(atomicReferenceArray, t, d, a);
|
|
}
|
|
long j2 = this.b + d;
|
|
if (a(atomicReferenceArray, a(j2, i2)) == null) {
|
|
this.c = j2 - 1;
|
|
return a(atomicReferenceArray, t, d, a);
|
|
}
|
|
if (a(atomicReferenceArray, a(1 + d, i2)) == null) {
|
|
return a(atomicReferenceArray, t, d, a);
|
|
}
|
|
a(atomicReferenceArray, d, a, t, i2);
|
|
return true;
|
|
}
|
|
|
|
@Override // io.reactivex.internal.fuseable.SimplePlainQueue, io.reactivex.internal.fuseable.SimpleQueue
|
|
public T poll() {
|
|
AtomicReferenceArray<Object> atomicReferenceArray = this.g;
|
|
long c = c();
|
|
int i2 = this.f;
|
|
int a = a(c, i2);
|
|
T t = (T) a(atomicReferenceArray, a);
|
|
boolean z = t == j;
|
|
if (t == null || z) {
|
|
if (z) {
|
|
return b(b(atomicReferenceArray, i2 + 1), c, i2);
|
|
}
|
|
return null;
|
|
}
|
|
a(atomicReferenceArray, a, (Object) null);
|
|
a(c + 1);
|
|
return t;
|
|
}
|
|
|
|
private void a(AtomicReferenceArray<Object> atomicReferenceArray, long j2, int i2, T t, long j3) {
|
|
AtomicReferenceArray<Object> atomicReferenceArray2 = new AtomicReferenceArray<>(atomicReferenceArray.length());
|
|
this.e = atomicReferenceArray2;
|
|
this.c = (j3 + j2) - 1;
|
|
a(atomicReferenceArray2, i2, t);
|
|
a(atomicReferenceArray, atomicReferenceArray2);
|
|
a(atomicReferenceArray, i2, j);
|
|
b(j2 + 1);
|
|
}
|
|
|
|
private T b(AtomicReferenceArray<Object> atomicReferenceArray, long j2, int i2) {
|
|
this.g = atomicReferenceArray;
|
|
int a = a(j2, i2);
|
|
T t = (T) a(atomicReferenceArray, a);
|
|
if (t != null) {
|
|
a(atomicReferenceArray, a, (Object) null);
|
|
a(j2 + 1);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
public int b() {
|
|
long e = e();
|
|
while (true) {
|
|
long f = f();
|
|
long e2 = e();
|
|
if (e == e2) {
|
|
return (int) (f - e2);
|
|
}
|
|
e = e2;
|
|
}
|
|
}
|
|
|
|
private void a(AtomicReferenceArray<Object> atomicReferenceArray, AtomicReferenceArray<Object> atomicReferenceArray2) {
|
|
int length = atomicReferenceArray.length() - 1;
|
|
b(length);
|
|
a(atomicReferenceArray, length, atomicReferenceArray2);
|
|
}
|
|
|
|
private void b(long j2) {
|
|
this.a.lazySet(j2);
|
|
}
|
|
|
|
public T a() {
|
|
AtomicReferenceArray<Object> atomicReferenceArray = this.g;
|
|
long c = c();
|
|
int i2 = this.f;
|
|
T t = (T) a(atomicReferenceArray, a(c, i2));
|
|
return t == j ? a(b(atomicReferenceArray, i2 + 1), c, i2) : t;
|
|
}
|
|
|
|
private T a(AtomicReferenceArray<Object> atomicReferenceArray, long j2, int i2) {
|
|
this.g = atomicReferenceArray;
|
|
return (T) a(atomicReferenceArray, a(j2, i2));
|
|
}
|
|
|
|
private void a(int i2) {
|
|
this.b = Math.min(i2 / 4, i);
|
|
}
|
|
|
|
private void a(long j2) {
|
|
this.h.lazySet(j2);
|
|
}
|
|
|
|
private static int a(long j2, int i2) {
|
|
int i3 = ((int) j2) & i2;
|
|
b(i3);
|
|
return i3;
|
|
}
|
|
|
|
private static void a(AtomicReferenceArray<Object> atomicReferenceArray, int i2, Object obj) {
|
|
atomicReferenceArray.lazySet(i2, obj);
|
|
}
|
|
|
|
private static <E> Object a(AtomicReferenceArray<Object> atomicReferenceArray, int i2) {
|
|
return atomicReferenceArray.get(i2);
|
|
}
|
|
|
|
public boolean a(T t, T t2) {
|
|
AtomicReferenceArray<Object> atomicReferenceArray = this.e;
|
|
long f = f();
|
|
int i2 = this.d;
|
|
long j2 = 2 + f;
|
|
if (a(atomicReferenceArray, a(j2, i2)) == null) {
|
|
int a = a(f, i2);
|
|
a(atomicReferenceArray, a + 1, t2);
|
|
a(atomicReferenceArray, a, t);
|
|
b(j2);
|
|
return true;
|
|
}
|
|
AtomicReferenceArray<Object> atomicReferenceArray2 = new AtomicReferenceArray<>(atomicReferenceArray.length());
|
|
this.e = atomicReferenceArray2;
|
|
int a2 = a(f, i2);
|
|
a(atomicReferenceArray2, a2 + 1, t2);
|
|
a(atomicReferenceArray2, a2, t);
|
|
a(atomicReferenceArray, atomicReferenceArray2);
|
|
a(atomicReferenceArray, a2, j);
|
|
b(j2);
|
|
return true;
|
|
}
|
|
}
|