package dq;

import dn.l;
import dn.p;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.jvm.internal.q;
import kotlin.jvm.internal.t;
import kotlin.jvm.internal.v;
import rm.n0;
import tp.o;
import tp.y2;
import vp.i;
import zp.e0;
import zp.f0;
import zp.h0;

/* loaded from: classes3.dex */
public class d {

    /* renamed from: c, reason: collision with root package name */
    private static final AtomicReferenceFieldUpdater f16551c = AtomicReferenceFieldUpdater.newUpdater(d.class, Object.class, "head");

    /* renamed from: d, reason: collision with root package name */
    private static final AtomicLongFieldUpdater f16552d = AtomicLongFieldUpdater.newUpdater(d.class, "deqIdx");

    /* renamed from: e, reason: collision with root package name */
    private static final AtomicReferenceFieldUpdater f16553e = AtomicReferenceFieldUpdater.newUpdater(d.class, Object.class, "tail");

    /* renamed from: f, reason: collision with root package name */
    private static final AtomicLongFieldUpdater f16554f = AtomicLongFieldUpdater.newUpdater(d.class, "enqIdx");

    /* renamed from: g, reason: collision with root package name */
    private static final AtomicIntegerFieldUpdater f16555g = AtomicIntegerFieldUpdater.newUpdater(d.class, "_availablePermits");
    private volatile int _availablePermits;

    /* renamed from: a, reason: collision with root package name */
    private final int f16556a;

    /* renamed from: b, reason: collision with root package name */
    private final l f16557b;
    private volatile long deqIdx;
    private volatile long enqIdx;
    private volatile Object head;
    private volatile Object tail;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public /* synthetic */ class a extends q implements p {

        /* renamed from: p, reason: collision with root package name */
        public static final a f16558p = new a();

        a() {
            super(2, e.class, "createSegment", "createSegment(JLkotlinx/coroutines/sync/SemaphoreSegment;)Lkotlinx/coroutines/sync/SemaphoreSegment;", 1);
        }

        @Override // dn.p
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2) {
            return l(((Number) obj).longValue(), (f) obj2);
        }

        public final f l(long j10, f fVar) {
            f h10;
            h10 = e.h(j10, fVar);
            return h10;
        }
    }

    /* loaded from: classes3.dex */
    static final class b extends v implements l {
        b() {
            super(1);
        }

        public final void a(Throwable th2) {
            d.this.j();
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a((Throwable) obj);
            return n0.f34904a;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public /* synthetic */ class c extends q implements p {

        /* renamed from: p, reason: collision with root package name */
        public static final c f16560p = new c();

        c() {
            super(2, e.class, "createSegment", "createSegment(JLkotlinx/coroutines/sync/SemaphoreSegment;)Lkotlinx/coroutines/sync/SemaphoreSegment;", 1);
        }

        @Override // dn.p
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2) {
            return l(((Number) obj).longValue(), (f) obj2);
        }

        public final f l(long j10, f fVar) {
            f h10;
            h10 = e.h(j10, fVar);
            return h10;
        }
    }

    public d(int i10, int i11) {
        this.f16556a = i10;
        if (!(i10 > 0)) {
            throw new IllegalArgumentException(("Semaphore should have at least 1 permit, but had " + i10).toString());
        }
        if (!(i11 >= 0 && i11 <= i10)) {
            throw new IllegalArgumentException(("The number of acquired permits should be in 0.." + i10).toString());
        }
        f fVar = new f(0L, null, 2);
        this.head = fVar;
        this.tail = fVar;
        this._availablePermits = i10 - i11;
        this.f16557b = new b();
    }

    /* JADX WARN: Code restructure failed: missing block: B:21:0x0037, code lost:
    
        r10 = true;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final boolean f(y2 y2Var) {
        int i10;
        Object c10;
        int i11;
        h0 h0Var;
        h0 h0Var2;
        boolean z10;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f16553e;
        f fVar = (f) atomicReferenceFieldUpdater.get(this);
        long andIncrement = f16554f.getAndIncrement(this);
        a aVar = a.f16558p;
        i10 = e.f16566f;
        long j10 = andIncrement / i10;
        do {
            c10 = zp.d.c(fVar, j10, aVar);
            if (!f0.c(c10)) {
                e0 b10 = f0.b(c10);
                while (true) {
                    e0 e0Var = (e0) atomicReferenceFieldUpdater.get(this);
                    if (e0Var.f53544r >= b10.f53544r) {
                        break;
                    }
                    if (!b10.q()) {
                        z10 = false;
                        break;
                    }
                    if (androidx.concurrent.futures.b.a(atomicReferenceFieldUpdater, this, e0Var, b10)) {
                        if (e0Var.m()) {
                            e0Var.k();
                        }
                    } else if (b10.m()) {
                        b10.k();
                    }
                }
            } else {
                break;
            }
        } while (!z10);
        f fVar2 = (f) f0.b(c10);
        i11 = e.f16566f;
        int i12 = (int) (andIncrement % i11);
        if (i.a(fVar2.r(), i12, null, y2Var)) {
            y2Var.a(fVar2, i12);
            return true;
        }
        h0Var = e.f16562b;
        h0Var2 = e.f16563c;
        if (!i.a(fVar2.r(), i12, h0Var, h0Var2)) {
            return false;
        }
        if (y2Var instanceof o) {
            t.e(y2Var, "null cannot be cast to non-null type kotlinx.coroutines.CancellableContinuation<kotlin.Unit>");
            ((o) y2Var).z(n0.f34904a, this.f16557b);
            return true;
        }
        throw new IllegalStateException(("unexpected: " + y2Var).toString());
    }

    private final void g() {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i10;
        int i11;
        do {
            atomicIntegerFieldUpdater = f16555g;
            i10 = atomicIntegerFieldUpdater.get(this);
            i11 = this.f16556a;
            if (i10 <= i11) {
                return;
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i10, i11));
    }

    private final int h() {
        int andDecrement;
        do {
            andDecrement = f16555g.getAndDecrement(this);
        } while (andDecrement > this.f16556a);
        return andDecrement;
    }

    private final boolean l(Object obj) {
        if (!(obj instanceof o)) {
            throw new IllegalStateException(("unexpected: " + obj).toString());
        }
        t.e(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancellableContinuation<kotlin.Unit>");
        o oVar = (o) obj;
        Object M = oVar.M(n0.f34904a, null, this.f16557b);
        if (M == null) {
            return false;
        }
        oVar.W(M);
        return true;
    }

    /* JADX WARN: Code restructure failed: missing block: B:21:0x0034, code lost:
    
        r9 = true;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final boolean m() {
        int i10;
        Object c10;
        int i11;
        h0 h0Var;
        h0 h0Var2;
        int i12;
        h0 h0Var3;
        h0 h0Var4;
        h0 h0Var5;
        boolean z10;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f16551c;
        f fVar = (f) atomicReferenceFieldUpdater.get(this);
        long andIncrement = f16552d.getAndIncrement(this);
        i10 = e.f16566f;
        long j10 = andIncrement / i10;
        c cVar = c.f16560p;
        do {
            c10 = zp.d.c(fVar, j10, cVar);
            if (f0.c(c10)) {
                break;
            }
            e0 b10 = f0.b(c10);
            while (true) {
                e0 e0Var = (e0) atomicReferenceFieldUpdater.get(this);
                if (e0Var.f53544r >= b10.f53544r) {
                    break;
                }
                if (!b10.q()) {
                    z10 = false;
                    break;
                }
                if (androidx.concurrent.futures.b.a(atomicReferenceFieldUpdater, this, e0Var, b10)) {
                    if (e0Var.m()) {
                        e0Var.k();
                    }
                } else if (b10.m()) {
                    b10.k();
                }
            }
        } while (!z10);
        f fVar2 = (f) f0.b(c10);
        fVar2.b();
        if (fVar2.f53544r > j10) {
            return false;
        }
        i11 = e.f16566f;
        int i13 = (int) (andIncrement % i11);
        h0Var = e.f16562b;
        Object andSet = fVar2.r().getAndSet(i13, h0Var);
        if (andSet != null) {
            h0Var2 = e.f16565e;
            if (andSet == h0Var2) {
                return false;
            }
            return l(andSet);
        }
        i12 = e.f16561a;
        for (int i14 = 0; i14 < i12; i14++) {
            Object obj = fVar2.r().get(i13);
            h0Var5 = e.f16563c;
            if (obj == h0Var5) {
                return true;
            }
        }
        h0Var3 = e.f16562b;
        h0Var4 = e.f16564d;
        return !i.a(fVar2.r(), i13, h0Var3, h0Var4);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final void e(o oVar) {
        while (h() <= 0) {
            t.e(oVar, "null cannot be cast to non-null type kotlinx.coroutines.Waiter");
            if (f((y2) oVar)) {
                return;
            }
        }
        oVar.z(n0.f34904a, this.f16557b);
    }

    public int i() {
        return Math.max(f16555g.get(this), 0);
    }

    public void j() {
        do {
            int andIncrement = f16555g.getAndIncrement(this);
            if (andIncrement >= this.f16556a) {
                g();
                throw new IllegalStateException(("The number of released permits cannot be greater than " + this.f16556a).toString());
            }
            if (andIncrement >= 0) {
                return;
            }
        } while (!m());
    }

    public boolean k() {
        while (true) {
            AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = f16555g;
            int i10 = atomicIntegerFieldUpdater.get(this);
            if (i10 > this.f16556a) {
                g();
            } else {
                if (i10 <= 0) {
                    return false;
                }
                if (atomicIntegerFieldUpdater.compareAndSet(this, i10, i10 - 1)) {
                    return true;
                }
            }
        }
    }
}
