package defpackage;

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.locks.LockSupport;

/* JADX INFO: renamed from: ˑᵳᵲ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class ExecutorC2138 implements Executor, Closeable {
    private volatile /* synthetic */ int _isTerminated$volatile;
    private volatile /* synthetic */ long controlState$volatile;
    private volatile /* synthetic */ long parkedWorkersStack$volatile;

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public final C3348 f8470;

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

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final long f8472;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final int f8473;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public final String f8474;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public final C4219 f8475;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final C4219 f8476;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicLongFieldUpdater f8467 = AtomicLongFieldUpdater.newUpdater(ExecutorC2138.class, "parkedWorkersStack$volatile");

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicLongFieldUpdater f8469 = AtomicLongFieldUpdater.newUpdater(ExecutorC2138.class, "controlState$volatile");

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f8468 = AtomicIntegerFieldUpdater.newUpdater(ExecutorC2138.class, "_isTerminated$volatile");

    /* JADX INFO: renamed from: ʿٴˇ, reason: contains not printable characters */
    public static final C1735 f8466 = new C1735(6, "NOT_IN_STACK");

    public ExecutorC2138(int i, int i2, long j, String str) {
        this.f8471 = i;
        this.f8473 = i2;
        this.f8472 = j;
        this.f8474 = str;
        if (i < 1) {
            ᵕᵩʽᵲ.ᵱᵽʡᵚ(AbstractC0793.m2876(i, "Core pool size ", " should be at least 1"));
            throw null;
        }
        if (i2 < i) {
            ᵕᵩʽᵲ.ᵱᵽʡᵚ(AbstractC3653.m8230(i2, i, "Max pool size ", " should be greater than or equals to core pool size "));
            throw null;
        }
        if (i2 > 2097150) {
            ᵕᵩʽᵲ.ᵱᵽʡᵚ(AbstractC0793.m2876(i2, "Max pool size ", " should not exceed maximal supported number of threads 2097150"));
            throw null;
        }
        if (j <= 0) {
            ᵕᵩʽᵲ.ᵱᵽʡᵚ(AbstractC3589.m8118(j, "Idle worker keep alive time ", " must be positive"));
            throw null;
        }
        this.f8475 = new C4219();
        this.f8476 = new C4219();
        this.f8470 = new C3348((i + 1) * 2);
        this.controlState$volatile = ((long) i) << 42;
    }

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public static /* synthetic */ void m5646(ExecutorC2138 executorC2138, Runnable runnable, int i) {
        executorC2138.m5648(runnable, false, (i & 4) == 0);
    }

    /* JADX WARN: Removed duplicated region for block: B:38:0x0089  */
    @Override // java.io.Closeable, java.lang.AutoCloseable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void close() throws InterruptedException {
        int i;
        AbstractRunnableC1361 abstractRunnableC1361M9756;
        if (f8468.compareAndSet(this, 0, 1)) {
            Thread threadCurrentThread = Thread.currentThread();
            C4621 c4621 = threadCurrentThread instanceof C4621 ? (C4621) threadCurrentThread : null;
            if (c4621 == null || c4621.f17573 != this) {
                c4621 = null;
            }
            synchronized (this.f8470) {
                i = (int) (f8469.get(this) & 2097151);
            }
            if (1 <= i) {
                int i2 = 1;
                while (true) {
                    C4621 c46212 = (C4621) this.f8470.m7692(i2);
                    if (c46212 != c4621) {
                        while (c46212.getState() != Thread.State.TERMINATED) {
                            LockSupport.unpark(c46212);
                            c46212.join(10000L);
                        }
                        C4232 c4232 = c46212.f17572;
                        C4219 c4219 = this.f8476;
                        c4232.getClass();
                        C4232.f16141.getClass();
                        AbstractRunnableC1361 abstractRunnableC1361 = (AbstractRunnableC1361) AbstractC1929.m5168(AbstractC1929.f7689, c4232, C4232.f16145, null);
                        if (abstractRunnableC1361 != null) {
                            c4219.m5673(abstractRunnableC1361);
                        }
                        while (true) {
                            AbstractRunnableC1361 abstractRunnableC1361M9205 = c4232.m9205();
                            if (abstractRunnableC1361M9205 == null) {
                                break;
                            } else {
                                c4219.m5673(abstractRunnableC1361M9205);
                            }
                        }
                    }
                    if (i2 == i) {
                        break;
                    } else {
                        i2++;
                    }
                }
            }
            this.f8476.m5671();
            this.f8475.m5671();
            while (true) {
                if (c4621 == null) {
                    abstractRunnableC1361M9756 = (AbstractRunnableC1361) this.f8475.m5674();
                    if (abstractRunnableC1361M9756 == null && (abstractRunnableC1361M9756 = (AbstractRunnableC1361) this.f8476.m5674()) == null) {
                        break;
                    }
                } else {
                    abstractRunnableC1361M9756 = c4621.m9756(true);
                    if (abstractRunnableC1361M9756 == null) {
                    }
                }
                try {
                    abstractRunnableC1361M9756.run();
                } catch (Throwable th) {
                    Thread threadCurrentThread2 = Thread.currentThread();
                    threadCurrentThread2.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread2, th);
                }
            }
            if (c4621 != null) {
                c4621.m9753(5);
            }
            f8467.set(this, 0L);
            f8469.set(this, 0L);
        }
    }

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

    public final String toString() {
        ArrayList arrayList = new ArrayList();
        C3348 c3348 = this.f8470;
        int iM7694 = c3348.m7694();
        int i = 0;
        int i2 = 0;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        for (int i6 = 1; i6 < iM7694; i6++) {
            C4621 c4621 = (C4621) c3348.m7692(i6);
            if (c4621 != null) {
                int iM9204 = c4621.f17572.m9204();
                int iM8218 = AbstractC3653.m8218(c4621.f17574);
                if (iM8218 == 0) {
                    i++;
                    StringBuilder sb = new StringBuilder();
                    sb.append(iM9204);
                    sb.append('c');
                    arrayList.add(sb.toString());
                } else if (iM8218 == 1) {
                    i2++;
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(iM9204);
                    sb2.append('b');
                    arrayList.add(sb2.toString());
                } else if (iM8218 == 2) {
                    i3++;
                } else if (iM8218 == 3) {
                    i4++;
                    if (iM9204 > 0) {
                        StringBuilder sb3 = new StringBuilder();
                        sb3.append(iM9204);
                        sb3.append('d');
                        arrayList.add(sb3.toString());
                    }
                } else {
                    if (iM8218 != 4) {
                        C1685.m4561();
                        return null;
                    }
                    i5++;
                }
            }
        }
        long j = f8469.get(this);
        StringBuilder sb4 = new StringBuilder();
        sb4.append(this.f8474);
        sb4.append('@');
        sb4.append(ᵹˊᵫˋ.ᵶᵥᵝ(this));
        sb4.append("[Pool Size {core = ");
        int i7 = this.f8471;
        sb4.append(i7);
        sb4.append(", max = ");
        sb4.append(this.f8473);
        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.f8475.m5672());
        sb4.append(", global blocking queue size = ");
        sb4.append(this.f8476.m5672());
        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(i7 - ((int) ((j & 9223367638808264704L) >> 42)));
        sb4.append("}]");
        return sb4.toString();
    }

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public final boolean m5647(long j) {
        int i = ((int) (2097151 & j)) - ((int) ((j & 4398044413952L) >> 21));
        if (i < 0) {
            i = 0;
        }
        int i2 = this.f8471;
        if (i < i2) {
            int iM5651 = m5651();
            if (iM5651 == 1 && i2 > 1) {
                m5651();
            }
            if (iM5651 > 0) {
                return true;
            }
        }
        return false;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final void m5648(Runnable runnable, boolean z, boolean z2) {
        AbstractRunnableC1361 c2122;
        int i;
        AbstractC5244.f19768.getClass();
        long jNanoTime = System.nanoTime();
        if (runnable instanceof AbstractRunnableC1361) {
            c2122 = (AbstractRunnableC1361) runnable;
            c2122.f5633 = jNanoTime;
            c2122.f5634 = z;
        } else {
            c2122 = new C2122(runnable, jNanoTime, z);
        }
        boolean z3 = c2122.f5634;
        AtomicLongFieldUpdater atomicLongFieldUpdater = f8469;
        long jAddAndGet = z3 ? atomicLongFieldUpdater.addAndGet(this, 2097152L) : 0L;
        Thread threadCurrentThread = Thread.currentThread();
        C4621 c4621 = threadCurrentThread instanceof C4621 ? (C4621) threadCurrentThread : null;
        if (c4621 == null || c4621.f17573 != this) {
            c4621 = null;
        }
        if (c4621 != null && (i = c4621.f17574) != 5 && (c2122.f5634 || i != 2)) {
            c4621.f17571 = true;
            C4232 c4232 = c4621.f17572;
            if (z2) {
                c2122 = c4232.m9207(c2122);
            } else {
                c4232.getClass();
                C4232.f16141.getClass();
                AbstractRunnableC1361 abstractRunnableC1361 = (AbstractRunnableC1361) AbstractC1929.m5168(AbstractC1929.f7689, c4232, C4232.f16145, c2122);
                c2122 = abstractRunnableC1361 == null ? null : c4232.m9207(abstractRunnableC1361);
            }
        }
        if (c2122 != null) {
            if (!(c2122.f5634 ? this.f8476.m5673(c2122) : this.f8475.m5673(c2122))) {
                throw new RejectedExecutionException(AbstractC3653.m8231(new StringBuilder(), this.f8474, " was terminated"));
            }
        }
        if (z3) {
            if (m5649() || m5647(jAddAndGet)) {
                return;
            }
            m5649();
            return;
        }
        if (m5649() || m5647(atomicLongFieldUpdater.get(this))) {
            return;
        }
        m5649();
    }

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final boolean m5649() {
        ExecutorC2138 executorC2138;
        C1735 c1735;
        int iM9752;
        while (true) {
            long j = f8467.get(this);
            C4621 c4621 = (C4621) this.f8470.m7692((int) (2097151 & j));
            if (c4621 == null) {
                c4621 = null;
                executorC2138 = this;
            } else {
                long j2 = (2097152 + j) & (-2097152);
                Object objM9754 = c4621.m9754();
                while (true) {
                    c1735 = f8466;
                    if (objM9754 == c1735) {
                        iM9752 = -1;
                        break;
                    }
                    if (objM9754 == null) {
                        iM9752 = 0;
                        break;
                    }
                    C4621 c46212 = (C4621) objM9754;
                    iM9752 = c46212.m9752();
                    if (iM9752 != 0) {
                        break;
                    }
                    objM9754 = c46212.m9754();
                    j = j;
                }
                if (iM9752 >= 0) {
                    ExecutorC2138 executorC21382 = this;
                    boolean zCompareAndSet = f8467.compareAndSet(executorC21382, j, ((long) iM9752) | j2);
                    executorC2138 = executorC21382;
                    if (zCompareAndSet) {
                        c4621.m9757(c1735);
                    }
                    this = executorC2138;
                } else {
                    continue;
                }
            }
            if (c4621 == null) {
                return false;
            }
            if (C4621.f17570.compareAndSet(c4621, -1, 0)) {
                LockSupport.unpark(c4621);
                return true;
            }
            this = executorC2138;
        }
    }

    /* JADX INFO: renamed from: ᵱʡˏ, reason: contains not printable characters */
    public final void m5650(C4621 c4621, int i, int i2) {
        while (true) {
            long j = f8467.get(this);
            int i3 = (int) (2097151 & j);
            long j2 = (2097152 + j) & (-2097152);
            if (i3 == i) {
                if (i2 == 0) {
                    Object objM9754 = c4621.m9754();
                    while (true) {
                        if (objM9754 == f8466) {
                            i3 = -1;
                            break;
                        }
                        if (objM9754 == null) {
                            i3 = 0;
                            break;
                        }
                        C4621 c46212 = (C4621) objM9754;
                        int iM9752 = c46212.m9752();
                        if (iM9752 != 0) {
                            i3 = iM9752;
                            break;
                        }
                        objM9754 = c46212.m9754();
                    }
                } else {
                    i3 = i2;
                }
            }
            if (i3 >= 0) {
                ExecutorC2138 executorC2138 = this;
                if (f8467.compareAndSet(executorC2138, j, ((long) i3) | j2)) {
                    return;
                } else {
                    this = executorC2138;
                }
            }
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final int m5651() {
        synchronized (this.f8470) {
            try {
                if (f8468.get(this) == 1) {
                    return -1;
                }
                AtomicLongFieldUpdater atomicLongFieldUpdater = f8469;
                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.f8471) {
                    return 0;
                }
                if (i >= this.f8473) {
                    return 0;
                }
                int i3 = ((int) (atomicLongFieldUpdater.get(this) & 2097151)) + 1;
                if (i3 <= 0 || this.f8470.m7692(i3) != null) {
                    throw new IllegalArgumentException("Failed requirement.");
                }
                C4621 c4621 = new C4621(this, i3);
                this.f8470.m7693(i3, c4621);
                if (i3 != ((int) (2097151 & atomicLongFieldUpdater.incrementAndGet(this)))) {
                    throw new IllegalArgumentException("Failed requirement.");
                }
                int i4 = i2 + 1;
                c4621.start();
                return i4;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
