package o;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.locks.LockSupport;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.Bn, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class ExecutorC0039Bn implements Executor, Closeable {
    public static final /* synthetic */ AtomicLongFieldUpdater s = AtomicLongFieldUpdater.newUpdater(ExecutorC0039Bn.class, "parkedWorkersStack$volatile");
    public static final /* synthetic */ AtomicLongFieldUpdater t = AtomicLongFieldUpdater.newUpdater(ExecutorC0039Bn.class, "controlState$volatile");
    public static final /* synthetic */ AtomicIntegerFieldUpdater u = AtomicIntegerFieldUpdater.newUpdater(ExecutorC0039Bn.class, "_isTerminated$volatile");
    public static final C2943xq v = new C2943xq("NOT_IN_STACK", 4);
    private volatile /* synthetic */ int _isTerminated$volatile;
    private volatile /* synthetic */ long controlState$volatile;
    public final int l;
    public final int m;
    public final long n;

    /* renamed from: o, reason: collision with root package name */
    public final String f58o;
    public final TD p;
    private volatile /* synthetic */ long parkedWorkersStack$volatile;
    public final TD q;
    public final C2206q20 r;

    public ExecutorC0039Bn(int i, int i2, long j, String str) {
        this.l = i;
        this.m = i2;
        this.n = j;
        this.f58o = str;
        if (i < 1) {
            throw new IllegalArgumentException(AbstractC0126Ew.k(i, "Core pool size ", " should be at least 1").toString());
        }
        if (i2 < i) {
            throw new IllegalArgumentException(AbstractC0126Ew.i(i2, i, "Max pool size ", " should be greater than or equals to core pool size ").toString());
        }
        if (i2 > 2097150) {
            throw new IllegalArgumentException(AbstractC0126Ew.k(i2, "Max pool size ", " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (j <= 0) {
            throw new IllegalArgumentException(("Idle worker keep alive time " + j + " must be positive").toString());
        }
        this.p = new TD();
        this.q = new TD();
        this.r = new C2206q20((i + 1) * 2);
        this.controlState$volatile = i << 42;
        this._isTerminated$volatile = 0;
    }

    public static /* synthetic */ void d(ExecutorC0039Bn executorC0039Bn, Runnable runnable, int i) {
        executorC0039Bn.c(runnable, false, (i & 4) == 0);
    }

    public final int b() {
        synchronized (this.r) {
            try {
                if (u.get(this) != 0) {
                    return -1;
                }
                AtomicLongFieldUpdater atomicLongFieldUpdater = t;
                long j = atomicLongFieldUpdater.get(this);
                int i = (int) (j & 2097151);
                int i2 = i - ((int) ((j & 4398044413952L) >> 21));
                if (i2 < 0) {
                    i2 = 0;
                }
                if (i2 >= this.l) {
                    return 0;
                }
                if (i >= this.m) {
                    return 0;
                }
                int i3 = ((int) (atomicLongFieldUpdater.get(this) & 2097151)) + 1;
                if (i3 <= 0 || this.r.b(i3) != null) {
                    throw new IllegalArgumentException("Failed requirement.");
                }
                C0013An c0013An = new C0013An(this, i3);
                this.r.c(i3, c0013An);
                if (i3 != ((int) (2097151 & atomicLongFieldUpdater.incrementAndGet(this)))) {
                    throw new IllegalArgumentException("Failed requirement.");
                }
                int i4 = i2 + 1;
                c0013An.start();
                return i4;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void c(Runnable runnable, boolean z, boolean z2) {
        Nc0 sc0;
        int i;
        Xc0.f.getClass();
        long jNanoTime = System.nanoTime();
        if (runnable instanceof Nc0) {
            sc0 = (Nc0) runnable;
            sc0.l = jNanoTime;
            sc0.m = z;
        } else {
            sc0 = new Sc0(runnable, jNanoTime, z);
        }
        boolean z3 = sc0.m;
        AtomicLongFieldUpdater atomicLongFieldUpdater = t;
        long jAddAndGet = z3 ? atomicLongFieldUpdater.addAndGet(this, 2097152L) : 0L;
        Thread threadCurrentThread = Thread.currentThread();
        C0013An c0013An = threadCurrentThread instanceof C0013An ? (C0013An) threadCurrentThread : null;
        if (c0013An == null || !F3.c(c0013An.s, this)) {
            c0013An = null;
        }
        if (c0013An != null && (i = c0013An.n) != 5 && (sc0.m || i != 2)) {
            c0013An.r = true;
            Bm0 bm0 = c0013An.l;
            if (z2) {
                sc0 = bm0.a(sc0);
            } else {
                bm0.getClass();
                Nc0 nc0 = (Nc0) Bm0.b.getAndSet(bm0, sc0);
                sc0 = nc0 == null ? null : bm0.a(nc0);
            }
        }
        if (sc0 != null) {
            if (!(sc0.m ? this.q.a(sc0) : this.p.a(sc0))) {
                throw new RejectedExecutionException(AbstractC2462sn.n(new StringBuilder(), this.f58o, " was terminated"));
            }
        }
        boolean z4 = z2 && c0013An != null;
        if (z3) {
            if (z4 || j() || g(jAddAndGet)) {
                return;
            }
            j();
            return;
        }
        if (z4 || j() || g(atomicLongFieldUpdater.get(this))) {
            return;
        }
        j();
    }

    /* JADX WARN: Removed duplicated region for block: B:39:0x0087  */
    @Override // java.io.Closeable, java.lang.AutoCloseable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void close() throws java.lang.InterruptedException {
        /*
            r8 = this;
            java.util.concurrent.atomic.AtomicIntegerFieldUpdater r0 = o.ExecutorC0039Bn.u
            r1 = 0
            r2 = 1
            boolean r0 = r0.compareAndSet(r8, r1, r2)
            if (r0 != 0) goto Lb
            return
        Lb:
            java.lang.Thread r0 = java.lang.Thread.currentThread()
            boolean r1 = r0 instanceof o.C0013An
            r3 = 0
            if (r1 == 0) goto L17
            o.An r0 = (o.C0013An) r0
            goto L18
        L17:
            r0 = r3
        L18:
            if (r0 == 0) goto L23
            o.Bn r1 = r0.s
            boolean r1 = o.F3.c(r1, r8)
            if (r1 == 0) goto L23
            goto L24
        L23:
            r0 = r3
        L24:
            o.q20 r1 = r8.r
            monitor-enter(r1)
            java.util.concurrent.atomic.AtomicLongFieldUpdater r4 = o.ExecutorC0039Bn.t     // Catch: java.lang.Throwable -> Lbf
            long r4 = r4.get(r8)     // Catch: java.lang.Throwable -> Lbf
            r6 = 2097151(0x1fffff, double:1.0361303E-317)
            long r4 = r4 & r6
            int r4 = (int) r4
            monitor-exit(r1)
            if (r2 > r4) goto L75
            r1 = r2
        L36:
            o.q20 r5 = r8.r
            java.lang.Object r5 = r5.b(r1)
            o.An r5 = (o.C0013An) r5
            if (r5 == r0) goto L70
        L40:
            java.lang.Thread$State r6 = r5.getState()
            java.lang.Thread$State r7 = java.lang.Thread.State.TERMINATED
            if (r6 == r7) goto L51
            java.util.concurrent.locks.LockSupport.unpark(r5)
            r6 = 10000(0x2710, double:4.9407E-320)
            r5.join(r6)
            goto L40
        L51:
            o.Bm0 r5 = r5.l
            o.TD r6 = r8.q
            r5.getClass()
            java.util.concurrent.atomic.AtomicReferenceFieldUpdater r7 = o.Bm0.b
            java.lang.Object r7 = r7.getAndSet(r5, r3)
            o.Nc0 r7 = (o.Nc0) r7
            if (r7 == 0) goto L65
            r6.a(r7)
        L65:
            o.Nc0 r7 = r5.b()
            if (r7 != 0) goto L6c
            goto L70
        L6c:
            r6.a(r7)
            goto L65
        L70:
            if (r1 == r4) goto L75
            int r1 = r1 + 1
            goto L36
        L75:
            o.TD r1 = r8.q
            r1.b()
            o.TD r1 = r8.p
            r1.b()
        L7f:
            if (r0 == 0) goto L87
            o.Nc0 r1 = r0.a(r2)
            if (r1 != 0) goto Lae
        L87:
            o.TD r1 = r8.p
            java.lang.Object r1 = r1.d()
            o.Nc0 r1 = (o.Nc0) r1
            if (r1 != 0) goto Lae
            o.TD r1 = r8.q
            java.lang.Object r1 = r1.d()
            o.Nc0 r1 = (o.Nc0) r1
            if (r1 != 0) goto Lae
            if (r0 == 0) goto La1
            r1 = 5
            r0.h(r1)
        La1:
            java.util.concurrent.atomic.AtomicLongFieldUpdater r0 = o.ExecutorC0039Bn.s
            r1 = 0
            r0.set(r8, r1)
            java.util.concurrent.atomic.AtomicLongFieldUpdater r0 = o.ExecutorC0039Bn.t
            r0.set(r8, r1)
            return
        Lae:
            r1.run()     // Catch: java.lang.Throwable -> Lb2
            goto L7f
        Lb2:
            r1 = move-exception
            java.lang.Thread r3 = java.lang.Thread.currentThread()
            java.lang.Thread$UncaughtExceptionHandler r4 = r3.getUncaughtExceptionHandler()
            r4.uncaughtException(r3, r1)
            goto L7f
        Lbf:
            r0 = move-exception
            monitor-exit(r1)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: o.ExecutorC0039Bn.close():void");
    }

    @Override // java.util.concurrent.Executor
    public final void execute(Runnable runnable) {
        d(this, runnable, 6);
    }

    public final void f(C0013An c0013An, int i, int i2) {
        while (true) {
            long j = s.get(this);
            int i3 = (int) (2097151 & j);
            long j2 = (2097152 + j) & (-2097152);
            if (i3 == i) {
                if (i2 == 0) {
                    Object objC = c0013An.c();
                    while (true) {
                        if (objC == v) {
                            i3 = -1;
                            break;
                        }
                        if (objC == null) {
                            i3 = 0;
                            break;
                        }
                        C0013An c0013An2 = (C0013An) objC;
                        int iB = c0013An2.b();
                        if (iB != 0) {
                            i3 = iB;
                            break;
                        }
                        objC = c0013An2.c();
                    }
                } else {
                    i3 = i2;
                }
            }
            if (i3 >= 0) {
                if (s.compareAndSet(this, j, i3 | j2)) {
                    return;
                }
            }
        }
    }

    public final boolean g(long j) {
        int i = ((int) (2097151 & j)) - ((int) ((j & 4398044413952L) >> 21));
        if (i < 0) {
            i = 0;
        }
        int i2 = this.l;
        if (i < i2) {
            int iB = b();
            if (iB == 1 && i2 > 1) {
                b();
            }
            if (iB > 0) {
                return true;
            }
        }
        return false;
    }

    public final boolean j() {
        C2943xq c2943xq;
        int iB;
        while (true) {
            long j = s.get(this);
            C0013An c0013An = (C0013An) this.r.b((int) (2097151 & j));
            if (c0013An == null) {
                c0013An = null;
            } else {
                long j2 = (2097152 + j) & (-2097152);
                Object objC = c0013An.c();
                while (true) {
                    c2943xq = v;
                    if (objC == c2943xq) {
                        iB = -1;
                        break;
                    }
                    if (objC == null) {
                        iB = 0;
                        break;
                    }
                    C0013An c0013An2 = (C0013An) objC;
                    iB = c0013An2.b();
                    if (iB != 0) {
                        break;
                    }
                    objC = c0013An2.c();
                }
                if (iB >= 0) {
                    if (s.compareAndSet(this, j, iB | j2)) {
                        c0013An.g(c2943xq);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            }
            if (c0013An == null) {
                return false;
            }
            if (C0013An.t.compareAndSet(c0013An, -1, 0)) {
                LockSupport.unpark(c0013An);
                return true;
            }
        }
    }

    public final String toString() {
        ArrayList arrayList = new ArrayList();
        C2206q20 c2206q20 = this.r;
        int iA = c2206q20.a();
        int i = 0;
        int i2 = 0;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        for (int i6 = 1; i6 < iA; i6++) {
            C0013An c0013An = (C0013An) c2206q20.b(i6);
            if (c0013An != null) {
                Bm0 bm0 = c0013An.l;
                bm0.getClass();
                int i7 = Bm0.b.get(bm0) != null ? (Bm0.c.get(bm0) - Bm0.d.get(bm0)) + 1 : Bm0.c.get(bm0) - Bm0.d.get(bm0);
                int iW = AbstractC2462sn.w(c0013An.n);
                if (iW == 0) {
                    i++;
                    StringBuilder sb = new StringBuilder();
                    sb.append(i7);
                    sb.append('c');
                    arrayList.add(sb.toString());
                } else if (iW == 1) {
                    i2++;
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(i7);
                    sb2.append('b');
                    arrayList.add(sb2.toString());
                } else if (iW == 2) {
                    i3++;
                } else if (iW == 3) {
                    i4++;
                    if (i7 > 0) {
                        StringBuilder sb3 = new StringBuilder();
                        sb3.append(i7);
                        sb3.append('d');
                        arrayList.add(sb3.toString());
                    }
                } else {
                    if (iW != 4) {
                        throw new C2170pj();
                    }
                    i5++;
                }
            }
        }
        long j = t.get(this);
        StringBuilder sb4 = new StringBuilder();
        sb4.append(this.f58o);
        sb4.append('@');
        sb4.append(AbstractC0249Jp.K(this));
        sb4.append("[Pool Size {core = ");
        int i8 = this.l;
        sb4.append(i8);
        sb4.append(", max = ");
        sb4.append(this.m);
        sb4.append("}, Worker States {CPU = ");
        sb4.append(i);
        sb4.append(", blocking = ");
        sb4.append(i2);
        sb4.append(", parked = ");
        sb4.append(i3);
        sb4.append(", dormant = ");
        sb4.append(i4);
        sb4.append(", terminated = ");
        sb4.append(i5);
        sb4.append("}, running workers queues = ");
        sb4.append(arrayList);
        sb4.append(", global CPU queue size = ");
        sb4.append(this.p.c());
        sb4.append(", global blocking queue size = ");
        sb4.append(this.q.c());
        sb4.append(", Control State {created workers= ");
        sb4.append((int) (2097151 & j));
        sb4.append(", blocking tasks = ");
        sb4.append((int) ((4398044413952L & j) >> 21));
        sb4.append(", CPUs acquired = ");
        sb4.append(i8 - ((int) ((j & 9223367638808264704L) >> 42)));
        sb4.append("}]");
        return sb4.toString();
    }
}
