package defpackage;

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.DispatchException;
import sun.misc.Unsafe;

/* JADX INFO: renamed from: ˋˁ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public class C1461 {

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f5937;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public static final /* synthetic */ long f5938;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f5939 = AtomicReferenceFieldUpdater.newUpdater(C1461.class, Object.class, "head$volatile");

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicLongFieldUpdater f5940;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f5941;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicLongFieldUpdater f5942;

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public static final /* synthetic */ long f5943;
    private volatile /* synthetic */ int _availablePermits$volatile;
    private volatile /* synthetic */ long deqIdx$volatile;
    private volatile /* synthetic */ long enqIdx$volatile;
    private volatile /* synthetic */ Object head$volatile;
    private volatile /* synthetic */ Object tail$volatile;

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public final int f5944;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final ᵦˀˈᵨ f5945;

    static {
        Unsafe unsafe = AbstractC1929.f7689;
        f5938 = unsafe.objectFieldOffset(C1461.class.getDeclaredField("head$volatile"));
        f5940 = AtomicLongFieldUpdater.newUpdater(C1461.class, "deqIdx$volatile");
        f5941 = AtomicReferenceFieldUpdater.newUpdater(C1461.class, Object.class, "tail$volatile");
        f5943 = unsafe.objectFieldOffset(C1461.class.getDeclaredField("tail$volatile"));
        f5942 = AtomicLongFieldUpdater.newUpdater(C1461.class, "enqIdx$volatile");
        f5937 = AtomicIntegerFieldUpdater.newUpdater(C1461.class, "_availablePermits$volatile");
    }

    public C1461(int i) {
        this.f5944 = i;
        if (i <= 0) {
            ᵕᵩʽᵲ.ᵱᵽʡᵚ(AbstractC2704.m6517(i, "Semaphore should have at least 1 permit, but had "));
            throw null;
        }
        if (i < 0) {
            ᵕᵩʽᵲ.ᵱᵽʡᵚ(AbstractC2704.m6517(i, "The number of acquired permits should be in 0.."));
            throw null;
        }
        C1412 c1412 = new C1412(0L, null, 2);
        this.head$volatile = c1412;
        this.tail$volatile = c1412;
        this._availablePermits$volatile = i;
        this.f5945 = new ᵦˀˈᵨ(18, this);
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x0025, code lost:
    
        r5.mo2190(r3, r4.f5945);
     */
    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object m4183(AbstractC3500 abstractC3500) throws DispatchException {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int andDecrement;
        int i;
        do {
            atomicIntegerFieldUpdater = f5937;
            andDecrement = atomicIntegerFieldUpdater.getAndDecrement(this);
            i = this.f5944;
        } while (andDecrement > i);
        C5063 c5063 = C5063.f19112;
        if (andDecrement <= 0) {
            C3453 c3453 = ᵹˊᵫˋ.ˋˈ(ˌᵨʽʙ.ᵔᵖᵹᵫ(abstractC3500));
            try {
                if (!m4185(c3453)) {
                    while (true) {
                        int andDecrement2 = atomicIntegerFieldUpdater.getAndDecrement(this);
                        if (andDecrement2 <= i) {
                            if (andDecrement2 > 0) {
                                break;
                            }
                            if (m4185(c3453)) {
                                break;
                            }
                        }
                    }
                }
                Object objM7875 = c3453.m7875();
                EnumC0111 enumC0111 = EnumC0111.f1384;
                if (objM7875 != enumC0111) {
                    objM7875 = c5063;
                }
                if (objM7875 == enumC0111) {
                    return objM7875;
                }
            } catch (Throwable th) {
                c3453.m7877();
                throw th;
            }
        }
        return c5063;
    }

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final void m4184() {
        int i;
        do {
            AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = f5937;
            int andIncrement = atomicIntegerFieldUpdater.getAndIncrement(this);
            int i2 = this.f5944;
            if (andIncrement >= i2) {
                do {
                    i = atomicIntegerFieldUpdater.get(this);
                    if (i <= i2) {
                        break;
                    }
                } while (!atomicIntegerFieldUpdater.compareAndSet(this, i, i2));
                ᵷᵝˎ.ᵵᵽ(i2, "The number of released permits cannot be greater than ");
                return;
            }
            if (andIncrement >= 0) {
                return;
            }
        } while (!m4186());
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final boolean m4185(InterfaceC2973 interfaceC2973) {
        Object objM9402;
        Unsafe unsafe;
        C1461 c1461 = this;
        f5941.getClass();
        Unsafe unsafe2 = AbstractC1929.f7689;
        long j = f5943;
        C1412 c1412 = (C1412) unsafe2.getObjectVolatile(c1461, j);
        long andIncrement = f5942.getAndIncrement(c1461);
        C3313 c3313 = C3313.f12742;
        long j2 = andIncrement / ((long) AbstractC3387.f13025);
        loop0: while (true) {
            objM9402 = AbstractC4390.m9402(c1412, j2, c3313);
            if (ᵻᵕᵭ.ᵯᵷʽ(objM9402)) {
                break;
            }
            AbstractC4709 abstractC4709 = ᵻᵕᵭ.ᵔᵖᵹᵫ(objM9402);
            while (true) {
                AbstractC4709 abstractC47092 = (AbstractC4709) AbstractC1929.f7689.getObjectVolatile(c1461, j);
                if (abstractC47092.f17947 >= abstractC4709.f17947) {
                    c1461 = this;
                    break loop0;
                }
                if (!abstractC4709.m9851()) {
                    break;
                }
                do {
                    unsafe = AbstractC1929.f7689;
                    c1461 = this;
                    if (unsafe.compareAndSwapObject(c1461, f5943, abstractC47092, abstractC4709)) {
                        if (abstractC47092.m9853()) {
                            abstractC47092.m10592();
                        }
                    }
                } while (unsafe.getObjectVolatile(c1461, j) == abstractC47092);
                if (abstractC4709.m9853()) {
                    abstractC4709.m10592();
                }
            }
            c1461 = this;
        }
        C1412 c14122 = (C1412) ᵻᵕᵭ.ᵔᵖᵹᵫ(objM9402);
        AtomicReferenceArray atomicReferenceArray = c14122.f5827;
        int i = (int) (andIncrement % ((long) AbstractC3387.f13025));
        while (!atomicReferenceArray.compareAndSet(i, null, interfaceC2973)) {
            if (atomicReferenceArray.get(i) != null) {
                C1735 c1735 = AbstractC3387.f13020;
                C1735 c17352 = AbstractC3387.f13021;
                while (!atomicReferenceArray.compareAndSet(i, c1735, c17352)) {
                    if (atomicReferenceArray.get(i) != c1735) {
                        return false;
                    }
                }
                ((InterfaceC0573) interfaceC2973).mo2190(C5063.f19112, c1461.f5945);
                return true;
            }
        }
        interfaceC2973.mo3792(c14122, i);
        return true;
    }

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final boolean m4186() {
        Object objM9402;
        Unsafe unsafe;
        f5939.getClass();
        Unsafe unsafe2 = AbstractC1929.f7689;
        long j = f5938;
        C1412 c1412 = (C1412) unsafe2.getObjectVolatile(this, j);
        long andIncrement = f5940.getAndIncrement(this);
        long j2 = andIncrement / ((long) AbstractC3387.f13025);
        C3253 c3253 = C3253.f12375;
        loop0: while (true) {
            objM9402 = AbstractC4390.m9402(c1412, j2, c3253);
            if (ᵻᵕᵭ.ᵯᵷʽ(objM9402)) {
                break;
            }
            AbstractC4709 abstractC4709 = ᵻᵕᵭ.ᵔᵖᵹᵫ(objM9402);
            while (true) {
                AbstractC4709 abstractC47092 = (AbstractC4709) AbstractC1929.f7689.getObjectVolatile(this, j);
                if (abstractC47092.f17947 >= abstractC4709.f17947) {
                    break loop0;
                }
                if (!abstractC4709.m9851()) {
                    break;
                }
                do {
                    unsafe = AbstractC1929.f7689;
                    if (unsafe.compareAndSwapObject(this, f5938, abstractC47092, abstractC4709)) {
                        if (abstractC47092.m9853()) {
                            abstractC47092.m10592();
                        }
                    }
                } while (unsafe.getObjectVolatile(this, j) == abstractC47092);
                if (abstractC4709.m9853()) {
                    abstractC4709.m10592();
                }
            }
        }
        C1412 c14122 = (C1412) ᵻᵕᵭ.ᵔᵖᵹᵫ(objM9402);
        c14122.m10590();
        AtomicReferenceArray atomicReferenceArray = c14122.f5827;
        boolean z = false;
        if (c14122.f17947 <= j2) {
            int i = (int) (andIncrement % ((long) AbstractC3387.f13025));
            Object andSet = atomicReferenceArray.getAndSet(i, AbstractC3387.f13020);
            if (andSet == null) {
                int i2 = AbstractC3387.f13023;
                for (int i3 = 0; i3 < i2; i3++) {
                    if (atomicReferenceArray.get(i) == AbstractC3387.f13021) {
                        return true;
                    }
                }
                C1735 c1735 = AbstractC3387.f13020;
                C1735 c17352 = AbstractC3387.f13024;
                while (true) {
                    if (atomicReferenceArray.compareAndSet(i, c1735, c17352)) {
                        z = true;
                        break;
                    }
                    if (atomicReferenceArray.get(i) != c1735) {
                        break;
                    }
                }
                return !z;
            }
            if (andSet != AbstractC3387.f13022) {
                boolean z2 = andSet instanceof InterfaceC0573;
                C5063 c5063 = C5063.f19112;
                if (!z2) {
                    if (andSet instanceof C4600) {
                        return ((C4600) andSet).m9695(this, c5063) == 0;
                    }
                    C3226.m7364(andSet, "unexpected: ");
                    return false;
                }
                InterfaceC0573 interfaceC0573 = (InterfaceC0573) andSet;
                C1735 c1735Mo2192 = interfaceC0573.mo2192(c5063, this.f5945);
                if (c1735Mo2192 != null) {
                    interfaceC0573.mo2191(c1735Mo2192);
                    return true;
                }
            }
        }
        return false;
    }
}
