package g.a.n2;

import g.a.l0;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: LockFreeTaskQueue.kt */
/* JADX INFO: loaded from: classes.dex */
public final class r<E> {
    private volatile /* synthetic */ Object _next = null;
    private volatile /* synthetic */ long _state = 0;
    private final int a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final boolean f4096b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final int f4097c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private /* synthetic */ AtomicReferenceArray f4098d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final a f4092e = new a(null);

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final b0 f4095h = new b0("REMOVE_FROZEN");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f4093f = AtomicReferenceFieldUpdater.newUpdater(r.class, Object.class, "_next");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static final /* synthetic */ AtomicLongFieldUpdater f4094g = AtomicLongFieldUpdater.newUpdater(r.class, "_state");

    /* JADX INFO: compiled from: LockFreeTaskQueue.kt */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(f.z.d.g gVar) {
            this();
        }

        public final int a(long j2) {
            return (j2 & 2305843009213693952L) != 0 ? 2 : 1;
        }

        public final long b(long j2, int i2) {
            return d(j2, 1073741823L) | (((long) i2) << 0);
        }

        public final long c(long j2, int i2) {
            return d(j2, 1152921503533105152L) | (((long) i2) << 30);
        }

        public final long d(long j2, long j3) {
            return j2 & (~j3);
        }
    }

    /* JADX INFO: compiled from: LockFreeTaskQueue.kt */
    public static final class b {
        public final int a;

        public b(int i2) {
            this.a = i2;
        }
    }

    public r(int i2, boolean z) {
        this.a = i2;
        this.f4096b = z;
        this.f4097c = i2 - 1;
        this.f4098d = new AtomicReferenceArray(this.a);
        if (!(this.f4097c <= 1073741823)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        if (!((this.a & this.f4097c) == 0)) {
            throw new IllegalStateException("Check failed.".toString());
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final r<E> b(long j2) {
        r<E> rVar = new r<>(this.a * 2, this.f4096b);
        int i2 = (int) ((1073741823 & j2) >> 0);
        int i3 = (int) ((1152921503533105152L & j2) >> 30);
        while (true) {
            int i4 = this.f4097c;
            if ((i2 & i4) == (i3 & i4)) {
                rVar._state = f4092e.d(j2, 1152921504606846976L);
                return rVar;
            }
            Object bVar = this.f4098d.get(i4 & i2);
            if (bVar == null) {
                bVar = new b(i2);
            }
            rVar.f4098d.set(rVar.f4097c & i2, bVar);
            i2++;
        }
    }

    private final r<E> c(long j2) {
        while (true) {
            r<E> rVar = (r) this._next;
            if (rVar != null) {
                return rVar;
            }
            f4093f.compareAndSet(this, null, b(j2));
        }
    }

    private final r<E> e(int i2, E e2) {
        Object obj = this.f4098d.get(this.f4097c & i2);
        if (!(obj instanceof b) || ((b) obj).a != i2) {
            return null;
        }
        this.f4098d.set(i2 & this.f4097c, e2);
        return this;
    }

    private final long h() {
        long j2;
        long j3;
        do {
            j2 = this._state;
            if ((j2 & 1152921504606846976L) != 0) {
                return j2;
            }
            j3 = j2 | 1152921504606846976L;
        } while (!f4094g.compareAndSet(this, j2, j3));
        return j3;
    }

    private final r<E> k(int i2, int i3) {
        long j2;
        int i4;
        do {
            j2 = this._state;
            i4 = (int) ((1073741823 & j2) >> 0);
            if (l0.a()) {
                if (!(i4 == i2)) {
                    throw new AssertionError();
                }
            }
            if ((1152921504606846976L & j2) != 0) {
                return i();
            }
        } while (!f4094g.compareAndSet(this, j2, f4092e.b(j2, i3)));
        this.f4098d.set(this.f4097c & i4, null);
        return null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x004c, code lost:
    
        return 1;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final int a(E r13) {
        /*
            r12 = this;
        L0:
            long r2 = r12._state
            r0 = 3458764513820540928(0x3000000000000000, double:1.727233711018889E-77)
            long r0 = r0 & r2
            r6 = 0
            int r0 = (r0 > r6 ? 1 : (r0 == r6 ? 0 : -1))
            if (r0 == 0) goto L12
            g.a.n2.r$a r13 = g.a.n2.r.f4092e
            int r13 = r13.a(r2)
            return r13
        L12:
            r0 = 1073741823(0x3fffffff, double:5.304989472E-315)
            long r0 = r0 & r2
            r8 = 0
            long r0 = r0 >> r8
            int r0 = (int) r0
            r4 = 1152921503533105152(0xfffffffc0000000, double:1.2882296003504729E-231)
            long r4 = r4 & r2
            r1 = 30
            long r4 = r4 >> r1
            int r9 = (int) r4
            int r10 = r12.f4097c
            int r1 = r9 + 2
            r1 = r1 & r10
            r4 = r0 & r10
            r5 = 1
            if (r1 != r4) goto L2e
            return r5
        L2e:
            boolean r1 = r12.f4096b
            r4 = 1073741823(0x3fffffff, float:1.9999999)
            if (r1 != 0) goto L4d
            java.util.concurrent.atomic.AtomicReferenceArray r1 = r12.f4098d
            r11 = r9 & r10
            java.lang.Object r1 = r1.get(r11)
            if (r1 == 0) goto L4d
            int r1 = r12.a
            r2 = 1024(0x400, float:1.435E-42)
            if (r1 < r2) goto L4c
            int r9 = r9 - r0
            r0 = r9 & r4
            int r1 = r1 >> 1
            if (r0 <= r1) goto L0
        L4c:
            return r5
        L4d:
            int r0 = r9 + 1
            r0 = r0 & r4
            java.util.concurrent.atomic.AtomicLongFieldUpdater r1 = g.a.n2.r.f4094g
            g.a.n2.r$a r4 = g.a.n2.r.f4092e
            long r4 = r4.c(r2, r0)
            r0 = r1
            r1 = r12
            boolean r0 = r0.compareAndSet(r1, r2, r4)
            if (r0 == 0) goto L0
            java.util.concurrent.atomic.AtomicReferenceArray r0 = r12.f4098d
            r1 = r9 & r10
            r0.set(r1, r13)
            r0 = r12
        L68:
            long r1 = r0._state
            r3 = 1152921504606846976(0x1000000000000000, double:1.2882297539194267E-231)
            long r1 = r1 & r3
            int r1 = (r1 > r6 ? 1 : (r1 == r6 ? 0 : -1))
            if (r1 == 0) goto L7b
            g.a.n2.r r0 = r0.i()
            g.a.n2.r r0 = r0.e(r9, r13)
            if (r0 != 0) goto L68
        L7b:
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: g.a.n2.r.a(java.lang.Object):int");
    }

    public final boolean d() {
        long j2;
        do {
            j2 = this._state;
            if ((j2 & 2305843009213693952L) != 0) {
                return true;
            }
            if ((1152921504606846976L & j2) != 0) {
                return false;
            }
        } while (!f4094g.compareAndSet(this, j2, j2 | 2305843009213693952L));
        return true;
    }

    public final int f() {
        long j2 = this._state;
        return (((int) ((j2 & 1152921503533105152L) >> 30)) - ((int) ((1073741823 & j2) >> 0))) & 1073741823;
    }

    public final boolean g() {
        long j2 = this._state;
        return ((int) ((1073741823 & j2) >> 0)) == ((int) ((j2 & 1152921503533105152L) >> 30));
    }

    public final r<E> i() {
        return c(h());
    }

    public final Object j() {
        while (true) {
            long j2 = this._state;
            if ((1152921504606846976L & j2) != 0) {
                return f4095h;
            }
            int i2 = (int) ((1073741823 & j2) >> 0);
            int i3 = this.f4097c;
            if ((((int) ((1152921503533105152L & j2) >> 30)) & i3) == (i2 & i3)) {
                return null;
            }
            Object obj = this.f4098d.get(i3 & i2);
            if (obj == null) {
                if (this.f4096b) {
                    return null;
                }
            } else {
                if (obj instanceof b) {
                    return null;
                }
                int i4 = (i2 + 1) & 1073741823;
                if (f4094g.compareAndSet(this, j2, f4092e.b(j2, i4))) {
                    this.f4098d.set(this.f4097c & i2, null);
                    return obj;
                }
                if (this.f4096b) {
                    r<E> rVarK = this;
                    do {
                        rVarK = rVarK.k(i2, i4);
                    } while (rVarK != null);
                    return obj;
                }
            }
        }
    }
}
