package kotlinx.coroutines.sync;

import com.google.android.gms.measurement.internal.u1;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlinx.coroutines.internal.s;
import kotlinx.coroutines.v1;
import kotlinx.coroutines.y;
import sd.l;

/* JADX INFO: loaded from: classes.dex */
public class e {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29547c = AtomicReferenceFieldUpdater.newUpdater(e.class, Object.class, "head$volatile");

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f29548d = AtomicLongFieldUpdater.newUpdater(e.class, "deqIdx$volatile");

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29549e = AtomicReferenceFieldUpdater.newUpdater(e.class, Object.class, "tail$volatile");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f29550f = AtomicLongFieldUpdater.newUpdater(e.class, "enqIdx$volatile");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f29551g = AtomicIntegerFieldUpdater.newUpdater(e.class, "_availablePermits$volatile");
    private volatile /* synthetic */ int _availablePermits$volatile;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final l f29553b;
    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 e(int i10, int i11) {
        this.f29552a = i10;
        if (i10 <= 0) {
            throw new IllegalArgumentException(u1.g("Semaphore should have at least 1 permit, but had ", i10).toString());
        }
        if (i11 < 0 || i11 > i10) {
            throw new IllegalArgumentException(u1.g("The number of acquired permits should be in 0..", i10).toString());
        }
        g gVar = new g(0L, null, 2);
        this.head$volatile = gVar;
        this.tail$volatile = gVar;
        this._availablePermits$volatile = i10 - i11;
        this.f29553b = new l() { // from class: kotlinx.coroutines.sync.SemaphoreImpl$onCancellationRelease$1
            {
                super(1);
            }

            @Override // sd.l
            public final Object invoke(Object obj) {
                this.this$0.b();
                return kd.e.f29109a;
            }
        };
    }

    public final boolean a(v1 v1Var) {
        Object objC;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29549e;
        g gVar = (g) atomicReferenceFieldUpdater.get(this);
        long andIncrement = f29550f.getAndIncrement(this);
        SemaphoreImpl$addAcquireToQueue$createNewSegment$1 semaphoreImpl$addAcquireToQueue$createNewSegment$1 = SemaphoreImpl$addAcquireToQueue$createNewSegment$1.f29540c;
        long j10 = andIncrement / ((long) f.f29559f);
        loop0: while (true) {
            objC = kotlinx.coroutines.internal.a.c(gVar, j10, semaphoreImpl$addAcquireToQueue$createNewSegment$1);
            if (!y.I(objC)) {
                s sVarC = y.C(objC);
                while (true) {
                    s sVar = (s) atomicReferenceFieldUpdater.get(this);
                    if (sVar.f29487c >= sVarC.f29487c) {
                        break loop0;
                    }
                    if (!sVarC.i()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, sVar, sVarC)) {
                        if (atomicReferenceFieldUpdater.get(this) != sVar) {
                            if (sVarC.e()) {
                                sVarC.d();
                            }
                        }
                    }
                    if (sVar.e()) {
                        sVar.d();
                    }
                }
            } else {
                break;
            }
        }
        g gVar2 = (g) y.C(objC);
        int i10 = (int) (andIncrement % ((long) f.f29559f));
        AtomicReferenceArray atomicReferenceArray = gVar2.f29560e;
        while (!atomicReferenceArray.compareAndSet(i10, null, v1Var)) {
            if (atomicReferenceArray.get(i10) != null) {
                la.e eVar = f.f29555b;
                la.e eVar2 = f.f29556c;
                while (!atomicReferenceArray.compareAndSet(i10, eVar, eVar2)) {
                    if (atomicReferenceArray.get(i10) != eVar) {
                        return false;
                    }
                }
                ((kotlinx.coroutines.g) v1Var).d(kd.e.f29109a, this.f29553b);
                return true;
            }
        }
        v1Var.a(gVar2, i10);
        return true;
    }

    public final void b() {
        int i10;
        Object objC;
        while (true) {
            AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = f29551g;
            int andIncrement = atomicIntegerFieldUpdater.getAndIncrement(this);
            int i11 = this.f29552a;
            if (andIncrement >= i11) {
                do {
                    i10 = atomicIntegerFieldUpdater.get(this);
                    if (i10 <= i11) {
                        break;
                    }
                } while (!atomicIntegerFieldUpdater.compareAndSet(this, i10, i11));
                throw new IllegalStateException(("The number of released permits cannot be greater than " + i11).toString());
            }
            if (andIncrement >= 0) {
                return;
            }
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29547c;
            g gVar = (g) atomicReferenceFieldUpdater.get(this);
            long andIncrement2 = f29548d.getAndIncrement(this);
            long j10 = andIncrement2 / ((long) f.f29559f);
            SemaphoreImpl$tryResumeNextFromQueue$createNewSegment$1 semaphoreImpl$tryResumeNextFromQueue$createNewSegment$1 = SemaphoreImpl$tryResumeNextFromQueue$createNewSegment$1.f29541c;
            while (true) {
                objC = kotlinx.coroutines.internal.a.c(gVar, j10, semaphoreImpl$tryResumeNextFromQueue$createNewSegment$1);
                if (y.I(objC)) {
                    break;
                }
                s sVarC = y.C(objC);
                while (true) {
                    s sVar = (s) atomicReferenceFieldUpdater.get(this);
                    if (sVar.f29487c >= sVarC.f29487c) {
                        break;
                    }
                    if (!sVarC.i()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, sVar, sVarC)) {
                        if (atomicReferenceFieldUpdater.get(this) != sVar) {
                            if (sVarC.e()) {
                                sVarC.d();
                            }
                        }
                    }
                    if (sVar.e()) {
                        sVar.d();
                    }
                }
            }
            g gVar2 = (g) y.C(objC);
            gVar2.a();
            if (gVar2.f29487c <= j10) {
                int i12 = (int) (andIncrement2 % ((long) f.f29559f));
                la.e eVar = f.f29555b;
                AtomicReferenceArray atomicReferenceArray = gVar2.f29560e;
                Object andSet = atomicReferenceArray.getAndSet(i12, eVar);
                if (andSet == null) {
                    int i13 = f.f29554a;
                    boolean z10 = false;
                    for (int i14 = 0; i14 < i13; i14++) {
                        if (atomicReferenceArray.get(i12) == f.f29556c) {
                            return;
                        }
                    }
                    la.e eVar2 = f.f29555b;
                    la.e eVar3 = f.f29557d;
                    while (true) {
                        if (!atomicReferenceArray.compareAndSet(i12, eVar2, eVar3)) {
                            if (atomicReferenceArray.get(i12) != eVar2) {
                                break;
                            }
                        } else {
                            z10 = true;
                            break;
                        }
                    }
                    if (!z10) {
                        return;
                    }
                } else if (andSet == f.f29558e) {
                    continue;
                } else {
                    if (!(andSet instanceof kotlinx.coroutines.g)) {
                        throw new IllegalStateException(("unexpected: " + andSet).toString());
                    }
                    kotlinx.coroutines.g gVar3 = (kotlinx.coroutines.g) andSet;
                    la.e eVarH = gVar3.h(kd.e.f29109a, this.f29553b);
                    if (eVarH != null) {
                        gVar3.k(eVarH);
                        return;
                    }
                }
            }
        }
    }
}
