package o.a.h.f;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceArray;
import o.a.h.c.b;

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

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

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

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

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

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

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

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

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

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

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

    public a(int i2) {
        AtomicLong atomicLong = new AtomicLong();
        this.f6019n = atomicLong;
        this.f6026u = new AtomicLong();
        int iNumberOfLeadingZeros = 1 << (32 - Integer.numberOfLeadingZeros(Math.max(8, i2) - 1));
        int i3 = iNumberOfLeadingZeros - 1;
        AtomicReferenceArray<Object> atomicReferenceArray = new AtomicReferenceArray<>(iNumberOfLeadingZeros + 1);
        this.f6023r = atomicReferenceArray;
        this.f6022q = i3;
        this.f6020o = Math.min(iNumberOfLeadingZeros / 4, f6017f);
        this.f6025t = atomicReferenceArray;
        this.f6024s = i3;
        this.f6021p = i3 - 1;
        atomicLong.lazySet(0L);
    }

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

    @Override // o.a.h.c.b
    public boolean isEmpty() {
        return this.f6019n.get() == this.f6026u.get();
    }

    @Override // o.a.h.c.b
    public boolean offer(T t2) {
        Objects.requireNonNull(t2, "Null is not a valid element");
        AtomicReferenceArray<Object> atomicReferenceArray = this.f6023r;
        long j2 = this.f6019n.get();
        int i2 = this.f6022q;
        int i3 = ((int) j2) & i2;
        if (j2 < this.f6021p) {
            atomicReferenceArray.lazySet(i3, t2);
            this.f6019n.lazySet(j2 + 1);
            return true;
        }
        long j3 = ((long) this.f6020o) + j2;
        if (atomicReferenceArray.get(((int) j3) & i2) == null) {
            this.f6021p = j3 - 1;
            atomicReferenceArray.lazySet(i3, t2);
            this.f6019n.lazySet(j2 + 1);
            return true;
        }
        long j4 = j2 + 1;
        if (atomicReferenceArray.get(((int) j4) & i2) == null) {
            atomicReferenceArray.lazySet(i3, t2);
            this.f6019n.lazySet(j4);
            return true;
        }
        AtomicReferenceArray<Object> atomicReferenceArray2 = new AtomicReferenceArray<>(atomicReferenceArray.length());
        this.f6023r = atomicReferenceArray2;
        this.f6021p = (((long) i2) + j2) - 1;
        atomicReferenceArray2.lazySet(i3, t2);
        atomicReferenceArray.lazySet(atomicReferenceArray.length() - 1, atomicReferenceArray2);
        atomicReferenceArray.lazySet(i3, f6018m);
        this.f6019n.lazySet(j4);
        return true;
    }

    @Override // o.a.h.c.b
    public T poll() {
        AtomicReferenceArray<Object> atomicReferenceArray = this.f6025t;
        long j2 = this.f6026u.get();
        int i2 = this.f6024s;
        int i3 = ((int) j2) & i2;
        T t2 = (T) atomicReferenceArray.get(i3);
        boolean z2 = t2 == f6018m;
        if (t2 != null && !z2) {
            atomicReferenceArray.lazySet(i3, null);
            this.f6026u.lazySet(j2 + 1);
            return t2;
        }
        if (!z2) {
            return null;
        }
        int i4 = i2 + 1;
        AtomicReferenceArray<Object> atomicReferenceArray2 = (AtomicReferenceArray) atomicReferenceArray.get(i4);
        atomicReferenceArray.lazySet(i4, null);
        this.f6025t = atomicReferenceArray2;
        T t3 = (T) atomicReferenceArray2.get(i3);
        if (t3 != null) {
            atomicReferenceArray2.lazySet(i3, null);
            this.f6026u.lazySet(j2 + 1);
        }
        return t3;
    }
}
