package o;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.e60, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public class C1071e60 {
    public static final /* synthetic */ AtomicReferenceFieldUpdater c = AtomicReferenceFieldUpdater.newUpdater(C1071e60.class, Object.class, "head$volatile");
    public static final /* synthetic */ AtomicLongFieldUpdater d = AtomicLongFieldUpdater.newUpdater(C1071e60.class, "deqIdx$volatile");
    public static final /* synthetic */ AtomicReferenceFieldUpdater e = AtomicReferenceFieldUpdater.newUpdater(C1071e60.class, Object.class, "tail$volatile");
    public static final /* synthetic */ AtomicLongFieldUpdater f = AtomicLongFieldUpdater.newUpdater(C1071e60.class, "enqIdx$volatile");
    public static final /* synthetic */ AtomicIntegerFieldUpdater g = AtomicIntegerFieldUpdater.newUpdater(C1071e60.class, "_availablePermits$volatile");
    private volatile /* synthetic */ int _availablePermits$volatile;
    public final int a;
    public final C2353rg b;
    private volatile /* synthetic */ long deqIdx$volatile;
    private volatile /* synthetic */ long enqIdx$volatile;
    private volatile /* synthetic */ Object head$volatile;
    private volatile /* synthetic */ Object tail$volatile;

    public C1071e60(int i, int i2) {
        this.a = i;
        if (i <= 0) {
            throw new IllegalArgumentException(AbstractC0126Ew.j(i, "Semaphore should have at least 1 permit, but had ").toString());
        }
        if (i2 < 0 || i2 > i) {
            throw new IllegalArgumentException(AbstractC0126Ew.j(i, "The number of acquired permits should be in 0..").toString());
        }
        C1359h60 c1359h60 = new C1359h60(0L, null, 2);
        this.head$volatile = c1359h60;
        this.tail$volatile = c1359h60;
        this._availablePermits$volatile = i - i2;
        this.b = new C2353rg(4, this);
    }

    public final boolean a(Ik0 ik0) {
        Object objT;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = e;
        C1359h60 c1359h60 = (C1359h60) atomicReferenceFieldUpdater.get(this);
        long andIncrement = f.getAndIncrement(this);
        C0880c60 c0880c60 = C0880c60.t;
        long j = andIncrement / AbstractC1263g60.f;
        loop0: while (true) {
            objT = AbstractC2841wm0.t(c1359h60, j, c0880c60);
            if (!AbstractC2539te.I(objT)) {
                AbstractC2591u50 abstractC2591u50E = AbstractC2539te.E(objT);
                while (true) {
                    AbstractC2591u50 abstractC2591u50 = (AbstractC2591u50) atomicReferenceFieldUpdater.get(this);
                    if (abstractC2591u50.c >= abstractC2591u50E.c) {
                        break loop0;
                    }
                    if (!abstractC2591u50E.j()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, abstractC2591u50, abstractC2591u50E)) {
                        if (atomicReferenceFieldUpdater.get(this) != abstractC2591u50) {
                            if (abstractC2591u50E.f()) {
                                abstractC2591u50E.e();
                            }
                        }
                    }
                    if (abstractC2591u50.f()) {
                        abstractC2591u50.e();
                    }
                }
            } else {
                break;
            }
        }
        C1359h60 c1359h602 = (C1359h60) AbstractC2539te.E(objT);
        AtomicReferenceArray atomicReferenceArray = c1359h602.e;
        int i = (int) (andIncrement % AbstractC1263g60.f);
        while (!atomicReferenceArray.compareAndSet(i, null, ik0)) {
            if (atomicReferenceArray.get(i) != null) {
                C2943xq c2943xq = AbstractC1263g60.b;
                C2943xq c2943xq2 = AbstractC1263g60.c;
                while (!atomicReferenceArray.compareAndSet(i, c2943xq, c2943xq2)) {
                    if (atomicReferenceArray.get(i) != c2943xq) {
                        return false;
                    }
                }
                ((InterfaceC2259qg) ik0).s(Oh0.a, this.b);
                return true;
            }
        }
        ik0.a(c1359h602, i);
        return true;
    }

    public final void b() {
        int i;
        Object objT;
        boolean z;
        do {
            AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = g;
            int andIncrement = atomicIntegerFieldUpdater.getAndIncrement(this);
            int i2 = this.a;
            if (andIncrement >= i2) {
                do {
                    i = atomicIntegerFieldUpdater.get(this);
                    if (i <= i2) {
                        break;
                    }
                } while (!atomicIntegerFieldUpdater.compareAndSet(this, i, i2));
                throw new IllegalStateException(("The number of released permits cannot be greater than " + i2).toString());
            }
            if (andIncrement >= 0) {
                return;
            }
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = c;
            C1359h60 c1359h60 = (C1359h60) atomicReferenceFieldUpdater.get(this);
            long andIncrement2 = d.getAndIncrement(this);
            long j = andIncrement2 / AbstractC1263g60.f;
            C0976d60 c0976d60 = C0976d60.t;
            while (true) {
                objT = AbstractC2841wm0.t(c1359h60, j, c0976d60);
                if (AbstractC2539te.I(objT)) {
                    break;
                }
                AbstractC2591u50 abstractC2591u50E = AbstractC2539te.E(objT);
                while (true) {
                    AbstractC2591u50 abstractC2591u50 = (AbstractC2591u50) atomicReferenceFieldUpdater.get(this);
                    if (abstractC2591u50.c >= abstractC2591u50E.c) {
                        break;
                    }
                    if (!abstractC2591u50E.j()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, abstractC2591u50, abstractC2591u50E)) {
                        if (atomicReferenceFieldUpdater.get(this) != abstractC2591u50) {
                            if (abstractC2591u50E.f()) {
                                abstractC2591u50E.e();
                            }
                        }
                    }
                    if (abstractC2591u50.f()) {
                        abstractC2591u50.e();
                    }
                }
            }
            C1359h60 c1359h602 = (C1359h60) AbstractC2539te.E(objT);
            c1359h602.b();
            AtomicReferenceArray atomicReferenceArray = c1359h602.e;
            z = false;
            if (c1359h602.c <= j) {
                int i3 = (int) (andIncrement2 % AbstractC1263g60.f);
                Object andSet = atomicReferenceArray.getAndSet(i3, AbstractC1263g60.b);
                if (andSet == null) {
                    int i4 = AbstractC1263g60.a;
                    for (int i5 = 0; i5 < i4; i5++) {
                        if (atomicReferenceArray.get(i3) == AbstractC1263g60.c) {
                            z = true;
                            break;
                        }
                    }
                    C2943xq c2943xq = AbstractC1263g60.b;
                    C2943xq c2943xq2 = AbstractC1263g60.d;
                    while (true) {
                        if (!atomicReferenceArray.compareAndSet(i3, c2943xq, c2943xq2)) {
                            if (atomicReferenceArray.get(i3) != c2943xq) {
                                break;
                            }
                        } else {
                            z = true;
                            break;
                        }
                    }
                    z = !z;
                } else if (andSet != AbstractC1263g60.e) {
                    if (!(andSet instanceof InterfaceC2259qg)) {
                        throw new IllegalStateException(("unexpected: " + andSet).toString());
                    }
                    InterfaceC2259qg interfaceC2259qg = (InterfaceC2259qg) andSet;
                    C2943xq c2943xqU = interfaceC2259qg.u(Oh0.a, this.b);
                    if (c2943xqU != null) {
                        interfaceC2259qg.v(c2943xqU);
                        z = true;
                        break;
                        break;
                    }
                }
            }
        } while (!z);
    }
}
