package kotlinx.coroutines.sync;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000b\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\b\n\u0002\b\u0004\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\n\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\u0010\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0010\u0018\u00002\u00020\u0001B\u0017\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0004\u001a\u00020\u0002¢\u0006\u0004\b\u0005\u0010\u0006J\r\u0010\b\u001a\u00020\u0007¢\u0006\u0004\b\b\u0010\tJ\u0010\u0010\u000b\u001a\u00020\nH\u0086@¢\u0006\u0004\b\u000b\u0010\fJ\u001d\u0010\u000f\u001a\u00020\n2\f\u0010\u000e\u001a\b\u0012\u0004\u0012\u00020\n0\rH\u0005¢\u0006\u0004\b\u000f\u0010\u0010J%\u0010\u0014\u001a\u00020\n2\n\u0010\u0012\u001a\u0006\u0012\u0002\b\u00030\u00112\b\u0010\u0013\u001a\u0004\u0018\u00010\u0001H\u0004¢\u0006\u0004\b\u0014\u0010\u0015J\r\u0010\u0016\u001a\u00020\n¢\u0006\u0004\b\u0016\u0010\u0017J\u0010\u0010\u0018\u001a\u00020\nH\u0082@¢\u0006\u0004\b\u0018\u0010\fJ\u000f\u0010\u0019\u001a\u00020\u0002H\u0002¢\u0006\u0004\b\u0019\u0010\u001aJ\u000f\u0010\u001b\u001a\u00020\nH\u0002¢\u0006\u0004\b\u001b\u0010\u0017J\u0017\u0010\u001d\u001a\u00020\u00072\u0006\u0010\u000e\u001a\u00020\u001cH\u0002¢\u0006\u0004\b\u001d\u0010\u001eJ\u000f\u0010\u001f\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\u001f\u0010\tJ\u0013\u0010 \u001a\u00020\u0007*\u00020\u0001H\u0002¢\u0006\u0004\b \u0010!R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\"\u0010#R,\u0010)\u001a\u001a\u0012\u0004\u0012\u00020%\u0012\u0004\u0012\u00020\n\u0012\u0004\u0012\u00020&\u0012\u0004\u0012\u00020\n0$8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b'\u0010(R\u0011\u0010*\u001a\u00020\u00028F¢\u0006\u0006\u001a\u0004\b'\u0010\u001aR\u0011\u0010-\u001a\b\u0012\u0004\u0012\u00020,0+8\u0002X\u0082\u0004R\u000b\u0010/\u001a\u00020.8\u0002X\u0082\u0004R\u0011\u00100\u001a\b\u0012\u0004\u0012\u00020,0+8\u0002X\u0082\u0004R\u000b\u00101\u001a\u00020.8\u0002X\u0082\u0004R\u000b\u00103\u001a\u0002028\u0002X\u0082\u0004¨\u00064"}, d2 = {"Lkotlinx/coroutines/sync/SemaphoreAndMutexImpl;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "permits", "acquiredPermits", "<init>", "(II)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "u", "()Z", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "c", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "Lkotlinx/coroutines/CancellableContinuation;", "waiter", "h", "(Lkotlinx/coroutines/CancellableContinuation;)V", "Lkotlinx/coroutines/selects/SelectInstance;", "select", "ignoredParam", "s", "(Lkotlinx/coroutines/selects/SelectInstance;Ljava/lang/Object;)V", "release", "()V", "j", "m", "()I", "l", "Lkotlinx/coroutines/Waiter;", "k", "(Lkotlinx/coroutines/Waiter;)Z", "w", "v", "(Ljava/lang/Object;)Z", "a", "I", "Lkotlin/Function3;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lkotlin/coroutines/CoroutineContext;", "b", "Lkotlin/jvm/functions/Function3;", "onCancellationRelease", "availablePermits", "Lkotlinx/atomicfu/AtomicRef;", "Lkotlinx/coroutines/sync/SemaphoreSegment;", "head", "Lkotlinx/atomicfu/AtomicLong;", "deqIdx", "tail", "enqIdx", "Lkotlinx/atomicfu/AtomicInt;", "_availablePermits", "kotlinx-coroutines-core"}, k = 1, mv = {2, 1, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public class SemaphoreAndMutexImpl {
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicReferenceFieldUpdater c = java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(kotlinx.coroutines.sync.SemaphoreAndMutexImpl.class, java.lang.Object.class, "head$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicLongFieldUpdater d = java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater(kotlinx.coroutines.sync.SemaphoreAndMutexImpl.class, "deqIdx$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicReferenceFieldUpdater e = java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(kotlinx.coroutines.sync.SemaphoreAndMutexImpl.class, java.lang.Object.class, "tail$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicLongFieldUpdater f = java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater(kotlinx.coroutines.sync.SemaphoreAndMutexImpl.class, "enqIdx$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater g = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.sync.SemaphoreAndMutexImpl.class, "_availablePermits$volatile");
    private volatile /* synthetic */ int _availablePermits$volatile;

    /* JADX INFO: renamed from: a, reason: from kotlin metadata */
    private final int permits;

    /* JADX INFO: renamed from: b, reason: from kotlin metadata */
    private final kotlin.jvm.functions.Function3 onCancellationRelease;
    private volatile /* synthetic */ long deqIdx$volatile;
    private volatile /* synthetic */ long enqIdx$volatile;
    private volatile /* synthetic */ java.lang.Object head$volatile;
    private volatile /* synthetic */ java.lang.Object tail$volatile;

    public SemaphoreAndMutexImpl(int i, int i2) {
        this.permits = i;
        if (i <= 0) {
            throw new java.lang.IllegalArgumentException(("Semaphore should have at least 1 permit, but had " + i).toString());
        }
        if (i2 < 0 || i2 > i) {
            throw new java.lang.IllegalArgumentException(("The number of acquired permits should be in 0.." + i).toString());
        }
        kotlinx.coroutines.sync.SemaphoreSegment semaphoreSegment = new kotlinx.coroutines.sync.SemaphoreSegment(0L, null, 2);
        this.head$volatile = semaphoreSegment;
        this.tail$volatile = semaphoreSegment;
        this._availablePermits$volatile = i - i2;
        this.onCancellationRelease = new kotlin.jvm.functions.Function3() { // from class: kotlinx.coroutines.sync.e
            public final java.lang.Object invoke(java.lang.Object obj, java.lang.Object obj2, java.lang.Object obj3) {
                return kotlinx.coroutines.sync.SemaphoreAndMutexImpl.t(this.a, (java.lang.Throwable) obj, (kotlin.Unit) obj2, (kotlin.coroutines.CoroutineContext) obj3);
            }
        };
    }

    private final java.lang.Object j(kotlin.coroutines.Continuation continuation) {
        kotlinx.coroutines.CancellableContinuationImpl cancellableContinuationImplB = kotlinx.coroutines.CancellableContinuationKt.b(kotlin.coroutines.intrinsics.IntrinsicsKt.d(continuation));
        try {
            if (!k(cancellableContinuationImplB)) {
                h(cancellableContinuationImplB);
            }
            java.lang.Object objS = cancellableContinuationImplB.s();
            if (objS == kotlin.coroutines.intrinsics.IntrinsicsKt.g()) {
                kotlin.coroutines.jvm.internal.DebugProbesKt.c(continuation);
            }
            return objS == kotlin.coroutines.intrinsics.IntrinsicsKt.g() ? objS : kotlin.Unit.a;
        } catch (java.lang.Throwable th) {
            cancellableContinuationImplB.O();
            throw th;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean k(kotlinx.coroutines.Waiter waiter) {
        java.lang.Object objC;
        kotlinx.coroutines.sync.SemaphoreSegment semaphoreSegment = (kotlinx.coroutines.sync.SemaphoreSegment) e.get(this);
        long andIncrement = f.getAndIncrement(this);
        kotlinx.coroutines.sync.SemaphoreAndMutexImpl$addAcquireToQueue$createNewSegment$1 semaphoreAndMutexImpl$addAcquireToQueue$createNewSegment$1 = kotlinx.coroutines.sync.SemaphoreAndMutexImpl$addAcquireToQueue$createNewSegment$1.a;
        java.util.concurrent.atomic.AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = e;
        long j = andIncrement / ((long) kotlinx.coroutines.sync.SemaphoreKt.f);
        loop0: while (true) {
            objC = kotlinx.coroutines.internal.ConcurrentLinkedListKt.c(semaphoreSegment, j, semaphoreAndMutexImpl$addAcquireToQueue$createNewSegment$1);
            if (!kotlinx.coroutines.internal.SegmentOrClosed.e(objC)) {
                kotlinx.coroutines.internal.Segment segmentC = kotlinx.coroutines.internal.SegmentOrClosed.c(objC);
                while (true) {
                    kotlinx.coroutines.internal.Segment segment = (kotlinx.coroutines.internal.Segment) atomicReferenceFieldUpdater.get(this);
                    if (segment.id >= segmentC.id) {
                        break loop0;
                    }
                    if (!segmentC.u()) {
                        break;
                    }
                    if (androidx.concurrent.futures.a.a(atomicReferenceFieldUpdater, this, segment, segmentC)) {
                        if (!segment.p()) {
                            break loop0;
                        }
                        segment.n();
                        break loop0;
                    }
                    if (segmentC.p()) {
                        segmentC.n();
                    }
                }
            } else {
                break;
            }
        }
        kotlinx.coroutines.sync.SemaphoreSegment semaphoreSegment2 = (kotlinx.coroutines.sync.SemaphoreSegment) kotlinx.coroutines.internal.SegmentOrClosed.c(objC);
        int i = (int) (andIncrement % ((long) kotlinx.coroutines.sync.SemaphoreKt.f));
        if (com.google.common.util.concurrent.f0.a(semaphoreSegment2.getE(), i, (java.lang.Object) null, waiter)) {
            waiter.b(semaphoreSegment2, i);
            return true;
        }
        if (!com.google.common.util.concurrent.f0.a(semaphoreSegment2.getE(), i, kotlinx.coroutines.sync.SemaphoreKt.b, kotlinx.coroutines.sync.SemaphoreKt.c)) {
            return false;
        }
        if (waiter instanceof kotlinx.coroutines.CancellableContinuation) {
            kotlin.jvm.internal.Intrinsics.j(waiter, "null cannot be cast to non-null type kotlinx.coroutines.CancellableContinuation<kotlin.Unit>");
            ((kotlinx.coroutines.CancellableContinuation) waiter).I(kotlin.Unit.a, this.onCancellationRelease);
        } else {
            if (!(waiter instanceof kotlinx.coroutines.selects.SelectInstance)) {
                throw new java.lang.IllegalStateException(("unexpected: " + waiter).toString());
            }
            ((kotlinx.coroutines.selects.SelectInstance) waiter).d(kotlin.Unit.a);
        }
        return true;
    }

    private final void l() {
        int i;
        do {
            i = g.get(this);
            if (i <= this.permits) {
                return;
            }
        } while (!g.compareAndSet(this, i, this.permits));
    }

    private final int m() {
        int andDecrement;
        do {
            andDecrement = g.getAndDecrement(this);
        } while (andDecrement > this.permits);
        return andDecrement;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final kotlin.Unit t(kotlinx.coroutines.sync.SemaphoreAndMutexImpl semaphoreAndMutexImpl, java.lang.Throwable th, kotlin.Unit unit, kotlin.coroutines.CoroutineContext coroutineContext) {
        semaphoreAndMutexImpl.release();
        return kotlin.Unit.a;
    }

    private final boolean v(java.lang.Object obj) {
        if (!(obj instanceof kotlinx.coroutines.CancellableContinuation)) {
            if (obj instanceof kotlinx.coroutines.selects.SelectInstance) {
                return ((kotlinx.coroutines.selects.SelectInstance) obj).f(this, kotlin.Unit.a);
            }
            throw new java.lang.IllegalStateException(("unexpected: " + obj).toString());
        }
        kotlin.jvm.internal.Intrinsics.j(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancellableContinuation<kotlin.Unit>");
        kotlinx.coroutines.CancellableContinuation cancellableContinuation = (kotlinx.coroutines.CancellableContinuation) obj;
        java.lang.Object objU = cancellableContinuation.U(kotlin.Unit.a, (java.lang.Object) null, this.onCancellationRelease);
        if (objU == null) {
            return false;
        }
        cancellableContinuation.J(objU);
        return true;
    }

    private final boolean w() {
        java.lang.Object objC;
        kotlinx.coroutines.sync.SemaphoreSegment semaphoreSegment = (kotlinx.coroutines.sync.SemaphoreSegment) c.get(this);
        long andIncrement = d.getAndIncrement(this);
        long j = andIncrement / ((long) kotlinx.coroutines.sync.SemaphoreKt.f);
        kotlinx.coroutines.sync.SemaphoreAndMutexImpl$tryResumeNextFromQueue$createNewSegment$1 semaphoreAndMutexImpl$tryResumeNextFromQueue$createNewSegment$1 = kotlinx.coroutines.sync.SemaphoreAndMutexImpl$tryResumeNextFromQueue$createNewSegment$1.a;
        java.util.concurrent.atomic.AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = c;
        loop0: while (true) {
            objC = kotlinx.coroutines.internal.ConcurrentLinkedListKt.c(semaphoreSegment, j, semaphoreAndMutexImpl$tryResumeNextFromQueue$createNewSegment$1);
            if (kotlinx.coroutines.internal.SegmentOrClosed.e(objC)) {
                break;
            }
            kotlinx.coroutines.internal.Segment segmentC = kotlinx.coroutines.internal.SegmentOrClosed.c(objC);
            while (true) {
                kotlinx.coroutines.internal.Segment segment = (kotlinx.coroutines.internal.Segment) atomicReferenceFieldUpdater.get(this);
                if (segment.id >= segmentC.id) {
                    break loop0;
                }
                if (!segmentC.u()) {
                    break;
                }
                if (androidx.concurrent.futures.a.a(atomicReferenceFieldUpdater, this, segment, segmentC)) {
                    if (!segment.p()) {
                        break loop0;
                    }
                    segment.n();
                    break loop0;
                }
                if (segmentC.p()) {
                    segmentC.n();
                }
            }
        }
        kotlinx.coroutines.sync.SemaphoreSegment semaphoreSegment2 = (kotlinx.coroutines.sync.SemaphoreSegment) kotlinx.coroutines.internal.SegmentOrClosed.c(objC);
        semaphoreSegment2.b();
        if (semaphoreSegment2.id > j) {
            return false;
        }
        int i = (int) (andIncrement % ((long) kotlinx.coroutines.sync.SemaphoreKt.f));
        java.lang.Object andSet = semaphoreSegment2.getE().getAndSet(i, kotlinx.coroutines.sync.SemaphoreKt.b);
        if (andSet != null) {
            if (andSet == kotlinx.coroutines.sync.SemaphoreKt.e) {
                return false;
            }
            return v(andSet);
        }
        int i2 = kotlinx.coroutines.sync.SemaphoreKt.a;
        for (int i5 = 0; i5 < i2; i5++) {
            if (semaphoreSegment2.getE().get(i) == kotlinx.coroutines.sync.SemaphoreKt.c) {
                return true;
            }
        }
        return !com.google.common.util.concurrent.f0.a(semaphoreSegment2.getE(), i, kotlinx.coroutines.sync.SemaphoreKt.b, kotlinx.coroutines.sync.SemaphoreKt.d);
    }

    public final int b() {
        return java.lang.Math.max(g.get(this), 0);
    }

    public final java.lang.Object c(kotlin.coroutines.Continuation continuation) {
        if (m() > 0) {
            return kotlin.Unit.a;
        }
        java.lang.Object objJ = j(continuation);
        return objJ == kotlin.coroutines.intrinsics.IntrinsicsKt.g() ? objJ : kotlin.Unit.a;
    }

    protected final void h(kotlinx.coroutines.CancellableContinuation waiter) {
        while (m() <= 0) {
            kotlin.jvm.internal.Intrinsics.j(waiter, "null cannot be cast to non-null type kotlinx.coroutines.Waiter");
            if (k((kotlinx.coroutines.Waiter) waiter)) {
                return;
            }
        }
        waiter.I(kotlin.Unit.a, this.onCancellationRelease);
    }

    public final void release() {
        do {
            int andIncrement = g.getAndIncrement(this);
            if (andIncrement >= this.permits) {
                l();
                throw new java.lang.IllegalStateException(("The number of released permits cannot be greater than " + this.permits).toString());
            }
            if (andIncrement >= 0) {
                return;
            }
        } while (!w());
    }

    protected final void s(kotlinx.coroutines.selects.SelectInstance select, java.lang.Object ignoredParam) {
        while (m() <= 0) {
            kotlin.jvm.internal.Intrinsics.j(select, "null cannot be cast to non-null type kotlinx.coroutines.Waiter");
            if (k((kotlinx.coroutines.Waiter) select)) {
                return;
            }
        }
        select.d(kotlin.Unit.a);
    }

    public final boolean u() {
        while (true) {
            int i = g.get(this);
            if (i > this.permits) {
                l();
            } else {
                if (i <= 0) {
                    return false;
                }
                if (g.compareAndSet(this, i, i - 1)) {
                    return true;
                }
            }
        }
    }
}
