package g7;

import Z6.AbstractC0385z;
import e7.p;
import java.io.Closeable;
import java.lang.Thread;
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.atomic.AtomicReferenceArray;
import java.util.concurrent.locks.LockSupport;

/* JADX INFO: renamed from: g7.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class ExecutorC0992c implements Executor, Closeable, AutoCloseable {
    private volatile /* synthetic */ int _isTerminated$volatile;
    private volatile /* synthetic */ long controlState$volatile;
    private volatile /* synthetic */ long parkedWorkersStack$volatile;

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

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final long f12980s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final String f12981t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final f f12982u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final f f12983v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final p f12984w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f12975x = AtomicLongFieldUpdater.newUpdater(ExecutorC0992c.class, "parkedWorkersStack$volatile");

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f12976y = AtomicLongFieldUpdater.newUpdater(ExecutorC0992c.class, "controlState$volatile");

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f12977z = AtomicIntegerFieldUpdater.newUpdater(ExecutorC0992c.class, "_isTerminated$volatile");

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final B3.f f12974A = new B3.f(5, "NOT_IN_STACK");

    public ExecutorC0992c(long j8, String str, int i, int i5) {
        this.f12978q = i;
        this.f12979r = i5;
        this.f12980s = j8;
        this.f12981t = str;
        if (i < 1) {
            throw new IllegalArgumentException(I1.a.k(i, "Core pool size ", " should be at least 1").toString());
        }
        if (i5 < i) {
            throw new IllegalArgumentException(I1.a.o("Max pool size ", i5, i, " should be greater than or equals to core pool size ").toString());
        }
        if (i5 > 2097150) {
            throw new IllegalArgumentException(I1.a.k(i5, "Max pool size ", " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (j8 <= 0) {
            throw new IllegalArgumentException(("Idle worker keep alive time " + j8 + " must be positive").toString());
        }
        this.f12982u = new f();
        this.f12983v = new f();
        this.f12984w = new p((i + 1) * 2);
        this.controlState$volatile = ((long) i) << 42;
    }

    public final int a() {
        synchronized (this.f12984w) {
            try {
                if (f12977z.get(this) == 1) {
                    return -1;
                }
                AtomicLongFieldUpdater atomicLongFieldUpdater = f12976y;
                long j8 = atomicLongFieldUpdater.get(this);
                int i = (int) (j8 & 2097151);
                int i5 = i - ((int) ((j8 & 4398044413952L) >> 21));
                if (i5 < 0) {
                    i5 = 0;
                }
                if (i5 >= this.f12978q) {
                    return 0;
                }
                if (i >= this.f12979r) {
                    return 0;
                }
                int i8 = ((int) (atomicLongFieldUpdater.get(this) & 2097151)) + 1;
                if (i8 <= 0 || this.f12984w.b(i8) != null) {
                    throw new IllegalArgumentException("Failed requirement.");
                }
                C0990a c0990a = new C0990a(this, i8);
                this.f12984w.c(i8, c0990a);
                if (i8 != ((int) (2097151 & atomicLongFieldUpdater.incrementAndGet(this)))) {
                    throw new IllegalArgumentException("Failed requirement.");
                }
                int i9 = i5 + 1;
                c0990a.start();
                return i9;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void b(Runnable runnable, boolean z8) {
        i jVar;
        EnumC0991b enumC0991b;
        AtomicReferenceArray atomicReferenceArray;
        k.f12998f.getClass();
        long jNanoTime = System.nanoTime();
        if (runnable instanceof i) {
            jVar = (i) runnable;
            jVar.f12990q = jNanoTime;
            jVar.f12991r = z8;
        } else {
            jVar = new j(runnable, jNanoTime, z8);
        }
        boolean z9 = jVar.f12991r;
        AtomicLongFieldUpdater atomicLongFieldUpdater = f12976y;
        long jAddAndGet = z9 ? atomicLongFieldUpdater.addAndGet(this, 2097152L) : 0L;
        Thread threadCurrentThread = Thread.currentThread();
        C0990a c0990a = threadCurrentThread instanceof C0990a ? (C0990a) threadCurrentThread : null;
        if (c0990a == null || !P6.g.a(c0990a.f12967x, this)) {
            c0990a = null;
        }
        if (c0990a != null && (enumC0991b = c0990a.f12962s) != EnumC0991b.f12972u && (jVar.f12991r || enumC0991b != EnumC0991b.f12969r)) {
            c0990a.f12966w = true;
            m mVar = c0990a.f12960q;
            mVar.getClass();
            jVar = (i) m.f13000b.getAndSet(mVar, jVar);
            if (jVar == null) {
                jVar = null;
            } else {
                mVar.getClass();
                AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = m.f13001c;
                if (atomicIntegerFieldUpdater.get(mVar) - m.f13002d.get(mVar) != 127) {
                    if (jVar.f12991r) {
                        m.f13003e.incrementAndGet(mVar);
                    }
                    int i = atomicIntegerFieldUpdater.get(mVar) & 127;
                    while (true) {
                        atomicReferenceArray = mVar.f13004a;
                        if (atomicReferenceArray.get(i) == null) {
                            break;
                        } else {
                            Thread.yield();
                        }
                    }
                    atomicReferenceArray.lazySet(i, jVar);
                    atomicIntegerFieldUpdater.incrementAndGet(mVar);
                    jVar = null;
                }
            }
        }
        if (jVar != null) {
            if (!(jVar.f12991r ? this.f12983v.a(jVar) : this.f12982u.a(jVar))) {
                throw new RejectedExecutionException(I1.a.r(new StringBuilder(), this.f12981t, " was terminated"));
            }
        }
        if (z9) {
            if (m() || j(jAddAndGet)) {
                return;
            }
            m();
            return;
        }
        if (m() || j(atomicLongFieldUpdater.get(this))) {
            return;
        }
        m();
    }

    /* JADX WARN: Removed duplicated region for block: B:39:0x008a  */
    @Override // java.io.Closeable, java.lang.AutoCloseable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void close() throws InterruptedException {
        int i;
        i iVarA;
        if (f12977z.compareAndSet(this, 0, 1)) {
            Thread threadCurrentThread = Thread.currentThread();
            C0990a c0990a = threadCurrentThread instanceof C0990a ? (C0990a) threadCurrentThread : null;
            if (c0990a == null || !P6.g.a(c0990a.f12967x, this)) {
                c0990a = null;
            }
            synchronized (this.f12984w) {
                i = (int) (f12976y.get(this) & 2097151);
            }
            if (1 <= i) {
                int i5 = 1;
                while (true) {
                    Object objB = this.f12984w.b(i5);
                    P6.g.b(objB);
                    C0990a c0990a2 = (C0990a) objB;
                    if (c0990a2 != c0990a) {
                        while (c0990a2.getState() != Thread.State.TERMINATED) {
                            LockSupport.unpark(c0990a2);
                            c0990a2.join(10000L);
                        }
                        m mVar = c0990a2.f12960q;
                        f fVar = this.f12983v;
                        mVar.getClass();
                        i iVar = (i) m.f13000b.getAndSet(mVar, null);
                        if (iVar != null) {
                            fVar.a(iVar);
                        }
                        while (true) {
                            i iVarA2 = mVar.a();
                            if (iVarA2 == null) {
                                break;
                            } else {
                                fVar.a(iVarA2);
                            }
                        }
                    }
                    if (i5 == i) {
                        break;
                    } else {
                        i5++;
                    }
                }
            }
            this.f12983v.b();
            this.f12982u.b();
            while (true) {
                if (c0990a == null) {
                    iVarA = (i) this.f12982u.d();
                    if (iVarA == null && (iVarA = (i) this.f12983v.d()) == null) {
                        break;
                    }
                } else {
                    iVarA = c0990a.a(true);
                    if (iVarA == null) {
                    }
                }
                try {
                    iVarA.run();
                } catch (Throwable th) {
                    Thread threadCurrentThread2 = Thread.currentThread();
                    threadCurrentThread2.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread2, th);
                }
            }
            if (c0990a != null) {
                c0990a.h(EnumC0991b.f12972u);
            }
            f12975x.set(this, 0L);
            f12976y.set(this, 0L);
        }
    }

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

    public final void g(C0990a c0990a, int i, int i5) {
        while (true) {
            long j8 = f12975x.get(this);
            int i8 = (int) (2097151 & j8);
            long j9 = (2097152 + j8) & (-2097152);
            if (i8 == i) {
                if (i5 == 0) {
                    Object objC = c0990a.c();
                    while (true) {
                        if (objC == f12974A) {
                            i8 = -1;
                            break;
                        }
                        if (objC == null) {
                            i8 = 0;
                            break;
                        }
                        C0990a c0990a2 = (C0990a) objC;
                        int iB = c0990a2.b();
                        if (iB != 0) {
                            i8 = iB;
                            break;
                        }
                        objC = c0990a2.c();
                    }
                } else {
                    i8 = i5;
                }
            }
            if (i8 >= 0) {
                if (f12975x.compareAndSet(this, j8, ((long) i8) | j9)) {
                    return;
                }
            }
        }
    }

    public final boolean j(long j8) {
        int i = ((int) (2097151 & j8)) - ((int) ((j8 & 4398044413952L) >> 21));
        if (i < 0) {
            i = 0;
        }
        int i5 = this.f12978q;
        if (i < i5) {
            int iA = a();
            if (iA == 1 && i5 > 1) {
                a();
            }
            if (iA > 0) {
                return true;
            }
        }
        return false;
    }

    public final boolean m() {
        B3.f fVar;
        int iB;
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = f12975x;
            long j8 = atomicLongFieldUpdater.get(this);
            C0990a c0990a = (C0990a) this.f12984w.b((int) (2097151 & j8));
            if (c0990a == null) {
                c0990a = null;
            } else {
                long j9 = (2097152 + j8) & (-2097152);
                Object objC = c0990a.c();
                while (true) {
                    fVar = f12974A;
                    if (objC == fVar) {
                        iB = -1;
                        break;
                    }
                    if (objC == null) {
                        iB = 0;
                        break;
                    }
                    C0990a c0990a2 = (C0990a) objC;
                    iB = c0990a2.b();
                    if (iB != 0) {
                        break;
                    }
                    objC = c0990a2.c();
                }
                if (iB >= 0 && atomicLongFieldUpdater.compareAndSet(this, j8, j9 | ((long) iB))) {
                    c0990a.g(fVar);
                }
            }
            if (c0990a == null) {
                return false;
            }
            if (C0990a.f12959y.compareAndSet(c0990a, -1, 0)) {
                LockSupport.unpark(c0990a);
                return true;
            }
        }
    }

    public final String toString() {
        ArrayList arrayList = new ArrayList();
        p pVar = this.f12984w;
        int iA = pVar.a();
        int i = 0;
        int i5 = 0;
        int i8 = 0;
        int i9 = 0;
        int i10 = 0;
        for (int i11 = 1; i11 < iA; i11++) {
            C0990a c0990a = (C0990a) pVar.b(i11);
            if (c0990a != null) {
                m mVar = c0990a.f12960q;
                mVar.getClass();
                int i12 = m.f13000b.get(mVar) != null ? (m.f13001c.get(mVar) - m.f13002d.get(mVar)) + 1 : m.f13001c.get(mVar) - m.f13002d.get(mVar);
                int iOrdinal = c0990a.f12962s.ordinal();
                if (iOrdinal == 0) {
                    i++;
                    StringBuilder sb = new StringBuilder();
                    sb.append(i12);
                    sb.append('c');
                    arrayList.add(sb.toString());
                } else if (iOrdinal == 1) {
                    i5++;
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(i12);
                    sb2.append('b');
                    arrayList.add(sb2.toString());
                } else if (iOrdinal == 2) {
                    i8++;
                } else if (iOrdinal == 3) {
                    i9++;
                    if (i12 > 0) {
                        StringBuilder sb3 = new StringBuilder();
                        sb3.append(i12);
                        sb3.append('d');
                        arrayList.add(sb3.toString());
                    }
                } else {
                    if (iOrdinal != 4) {
                        throw new A6.b();
                    }
                    i10++;
                }
            }
        }
        long j8 = f12976y.get(this);
        StringBuilder sb4 = new StringBuilder();
        sb4.append(this.f12981t);
        sb4.append('@');
        sb4.append(AbstractC0385z.m(this));
        sb4.append("[Pool Size {core = ");
        int i13 = this.f12978q;
        sb4.append(i13);
        sb4.append(", max = ");
        sb4.append(this.f12979r);
        sb4.append("}, Worker States {CPU = ");
        sb4.append(i);
        sb4.append(", blocking = ");
        sb4.append(i5);
        sb4.append(", parked = ");
        sb4.append(i8);
        sb4.append(", dormant = ");
        sb4.append(i9);
        sb4.append(", terminated = ");
        sb4.append(i10);
        sb4.append("}, running workers queues = ");
        sb4.append(arrayList);
        sb4.append(", global CPU queue size = ");
        sb4.append(this.f12982u.c());
        sb4.append(", global blocking queue size = ");
        sb4.append(this.f12983v.c());
        sb4.append(", Control State {created workers= ");
        sb4.append((int) (2097151 & j8));
        sb4.append(", blocking tasks = ");
        sb4.append((int) ((4398044413952L & j8) >> 21));
        sb4.append(", CPUs acquired = ");
        sb4.append(i13 - ((int) ((j8 & 9223367638808264704L) >> 42)));
        sb4.append("}]");
        return sb4.toString();
    }
}
