package d.a.s.f;

import a.a.a.a.c.n0;
import d.a.s.c.c;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceArray;

/* JADX INFO: loaded from: classes.dex */
public final class b<T> implements c<T> {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final int f5806b = Integer.getInteger("jctools.spsc.max.lookahead.step", 4096).intValue();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Object f5807c = new Object();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AtomicLong f5808d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f5809e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f5810f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final int f5811g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public AtomicReferenceArray<Object> f5812h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final int f5813i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public AtomicReferenceArray<Object> f5814j;
    public final AtomicLong k;

    public b(int i2) {
        AtomicLong atomicLong = new AtomicLong();
        this.f5808d = atomicLong;
        this.k = new AtomicLong();
        int iS = n0.s(Math.max(8, i2));
        int i3 = iS - 1;
        AtomicReferenceArray<Object> atomicReferenceArray = new AtomicReferenceArray<>(iS + 1);
        this.f5812h = atomicReferenceArray;
        this.f5811g = i3;
        this.f5809e = Math.min(iS / 4, f5806b);
        this.f5814j = atomicReferenceArray;
        this.f5813i = i3;
        this.f5810f = i3 - 1;
        atomicLong.lazySet(0L);
    }

    @Override // d.a.s.c.d
    public void clear() {
        while (true) {
            if (poll() == null && isEmpty()) {
                return;
            }
        }
    }

    @Override // d.a.s.c.d
    public boolean isEmpty() {
        return this.f5808d.get() == this.k.get();
    }

    @Override // d.a.s.c.d
    public boolean offer(T t) {
        Objects.requireNonNull(t, "Null is not a valid element");
        AtomicReferenceArray<Object> atomicReferenceArray = this.f5812h;
        long j2 = this.f5808d.get();
        int i2 = this.f5811g;
        int i3 = ((int) j2) & i2;
        if (j2 < this.f5810f) {
            atomicReferenceArray.lazySet(i3, t);
            this.f5808d.lazySet(j2 + 1);
            return true;
        }
        long j3 = ((long) this.f5809e) + j2;
        if (atomicReferenceArray.get(((int) j3) & i2) == null) {
            this.f5810f = j3 - 1;
            atomicReferenceArray.lazySet(i3, t);
            this.f5808d.lazySet(j2 + 1);
            return true;
        }
        long j4 = j2 + 1;
        if (atomicReferenceArray.get(((int) j4) & i2) == null) {
            atomicReferenceArray.lazySet(i3, t);
            this.f5808d.lazySet(j4);
            return true;
        }
        AtomicReferenceArray<Object> atomicReferenceArray2 = new AtomicReferenceArray<>(atomicReferenceArray.length());
        this.f5812h = atomicReferenceArray2;
        this.f5810f = (((long) i2) + j2) - 1;
        atomicReferenceArray2.lazySet(i3, t);
        atomicReferenceArray.lazySet(atomicReferenceArray.length() - 1, atomicReferenceArray2);
        atomicReferenceArray.lazySet(i3, f5807c);
        this.f5808d.lazySet(j4);
        return true;
    }

    @Override // d.a.s.c.c, d.a.s.c.d
    public T poll() {
        AtomicReferenceArray<Object> atomicReferenceArray = this.f5814j;
        long j2 = this.k.get();
        int i2 = this.f5813i;
        int i3 = ((int) j2) & i2;
        T t = (T) atomicReferenceArray.get(i3);
        boolean z = t == f5807c;
        if (t != null && !z) {
            atomicReferenceArray.lazySet(i3, null);
            this.k.lazySet(j2 + 1);
            return t;
        }
        if (!z) {
            return null;
        }
        int i4 = i2 + 1;
        AtomicReferenceArray<Object> atomicReferenceArray2 = (AtomicReferenceArray) atomicReferenceArray.get(i4);
        atomicReferenceArray.lazySet(i4, null);
        this.f5814j = atomicReferenceArray2;
        T t2 = (T) atomicReferenceArray2.get(i3);
        if (t2 != null) {
            atomicReferenceArray2.lazySet(i3, null);
            this.k.lazySet(j2 + 1);
        }
        return t2;
    }
}
