package kotlinx.coroutines.q2;

import h.r;
import h.z.n;
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.atomic.AtomicReferenceArray;
import java.util.concurrent.locks.LockSupport;
import kotlinx.coroutines.o2.q;
import kotlinx.coroutines.r0;
import kotlinx.coroutines.s0;
import org.snmp4j.mp.MPv3;

/* JADX INFO: compiled from: CoroutineScheduler.kt */
/* JADX INFO: loaded from: classes.dex */
public final class a implements Executor, Closeable {
    private volatile /* synthetic */ int _isTerminated;
    public final int b;
    volatile /* synthetic */ long controlState;

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final d f1131h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final d f1132i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final AtomicReferenceArray<b> f1133j;
    private volatile /* synthetic */ long parkedWorkersStack;
    public static final q n = new q("NOT_IN_STACK");

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private static final /* synthetic */ AtomicLongFieldUpdater f1127k = AtomicLongFieldUpdater.newUpdater(a.class, "parkedWorkersStack");
    static final /* synthetic */ AtomicLongFieldUpdater l = AtomicLongFieldUpdater.newUpdater(a.class, "controlState");
    private static final /* synthetic */ AtomicIntegerFieldUpdater m = AtomicIntegerFieldUpdater.newUpdater(a.class, "_isTerminated");

    /* JADX INFO: renamed from: kotlinx.coroutines.q2.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: CoroutineScheduler.kt */
    public /* synthetic */ class C0068a {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[c.values().length];
            iArr[c.PARKING.ordinal()] = 1;
            iArr[c.BLOCKING.ordinal()] = 2;
            iArr[c.CPU_ACQUIRED.ordinal()] = 3;
            iArr[c.DORMANT.ordinal()] = 4;
            iArr[c.TERMINATED.ordinal()] = 5;
            a = iArr;
        }
    }

    /* JADX INFO: compiled from: CoroutineScheduler.kt */
    public enum c {
        CPU_ACQUIRED,
        BLOCKING,
        PARKING,
        DORMANT,
        TERMINATED
    }

    public a(int i2, int i3, long j2, String str) {
        this.b = i2;
        this.f1128e = i3;
        this.f1129f = j2;
        this.f1130g = str;
        if (!(i2 >= 1)) {
            throw new IllegalArgumentException(("Core pool size " + this.b + " should be at least 1").toString());
        }
        if (!(this.f1128e >= this.b)) {
            throw new IllegalArgumentException(("Max pool size " + this.f1128e + " should be greater than or equals to core pool size " + this.b).toString());
        }
        if (!(this.f1128e <= 2097150)) {
            throw new IllegalArgumentException(("Max pool size " + this.f1128e + " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (!(this.f1129f > 0)) {
            throw new IllegalArgumentException(("Idle worker keep alive time " + this.f1129f + " must be positive").toString());
        }
        this.f1131h = new d();
        this.f1132i = new d();
        this.parkedWorkersStack = 0L;
        this.f1133j = new AtomicReferenceArray<>(this.f1128e + 1);
        this.controlState = ((long) this.b) << 42;
        this._isTerminated = 0;
    }

    private final boolean A() {
        b bVarN;
        do {
            bVarN = n();
            if (bVarN == null) {
                return false;
            }
        } while (!b.f1134k.compareAndSet(bVarN, -1, 0));
        LockSupport.unpark(bVarN);
        return true;
    }

    private final boolean a(i iVar) {
        return iVar.f1154e.E() == 1 ? this.f1132i.a(iVar) : this.f1131h.a(iVar);
    }

    private final int b() {
        synchronized (this.f1133j) {
            if (isTerminated()) {
                return -1;
            }
            long j2 = this.controlState;
            int i2 = (int) (j2 & 2097151);
            int iB = n.b(i2 - ((int) ((j2 & 4398044413952L) >> 21)), 0);
            if (iB >= this.b) {
                return 0;
            }
            if (i2 >= this.f1128e) {
                return 0;
            }
            int i3 = ((int) (this.controlState & 2097151)) + 1;
            if (!(i3 > 0 && this.f1133j.get(i3) == null)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            b bVar = new b(i3);
            this.f1133j.set(i3, bVar);
            if (!(i3 == ((int) (2097151 & l.incrementAndGet(this))))) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            bVar.start();
            return iB + 1;
        }
    }

    private final b i() {
        Thread threadCurrentThread = Thread.currentThread();
        b bVar = threadCurrentThread instanceof b ? (b) threadCurrentThread : null;
        if (bVar != null && h.x.d.k.a(a.this, this)) {
            return bVar;
        }
        return null;
    }

    public static /* synthetic */ void l(a aVar, Runnable runnable, j jVar, boolean z, int i2, Object obj) {
        if ((i2 & 2) != 0) {
            jVar = g.b;
        }
        if ((i2 & 4) != 0) {
            z = false;
        }
        aVar.j(runnable, jVar, z);
    }

    private final int m(b bVar) {
        Object objG = bVar.g();
        while (objG != n) {
            if (objG == null) {
                return 0;
            }
            b bVar2 = (b) objG;
            int iF = bVar2.f();
            if (iF != 0) {
                return iF;
            }
            objG = bVar2.g();
        }
        return -1;
    }

    private final b n() {
        while (true) {
            long j2 = this.parkedWorkersStack;
            b bVar = this.f1133j.get((int) (2097151 & j2));
            if (bVar == null) {
                return null;
            }
            long j3 = (2097152 + j2) & (-2097152);
            int iM = m(bVar);
            if (iM >= 0 && f1127k.compareAndSet(this, j2, ((long) iM) | j3)) {
                bVar.o(n);
                return bVar;
            }
        }
    }

    private final void t(boolean z) {
        long jAddAndGet = l.addAndGet(this, 2097152L);
        if (z || A() || y(jAddAndGet)) {
            return;
        }
        A();
    }

    private final i x(b bVar, i iVar, boolean z) {
        if (bVar == null || bVar.f1135e == c.TERMINATED) {
            return iVar;
        }
        if (iVar.f1154e.E() == 0 && bVar.f1135e == c.BLOCKING) {
            return iVar;
        }
        bVar.f1139i = true;
        return bVar.b.a(iVar, z);
    }

    private final boolean y(long j2) {
        if (n.b(((int) (2097151 & j2)) - ((int) ((j2 & 4398044413952L) >> 21)), 0) < this.b) {
            int iB = b();
            if (iB == 1 && this.b > 1) {
                b();
            }
            if (iB > 0) {
                return true;
            }
        }
        return false;
    }

    static /* synthetic */ boolean z(a aVar, long j2, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            j2 = aVar.controlState;
        }
        return aVar.y(j2);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws InterruptedException {
        s(10000L);
    }

    public final i d(Runnable runnable, j jVar) {
        long jA = l.f1158e.a();
        if (!(runnable instanceof i)) {
            return new k(runnable, jA, jVar);
        }
        i iVar = (i) runnable;
        iVar.b = jA;
        iVar.f1154e = jVar;
        return iVar;
    }

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

    /* JADX WARN: Type inference failed for: r0v0, types: [boolean, int] */
    public final boolean isTerminated() {
        return this._isTerminated;
    }

    public final void j(Runnable runnable, j jVar, boolean z) {
        kotlinx.coroutines.d dVarA = kotlinx.coroutines.e.a();
        if (dVarA != null) {
            dVarA.d();
        }
        i iVarD = d(runnable, jVar);
        b bVarI = i();
        i iVarX = x(bVarI, iVarD, z);
        if (iVarX != null && !a(iVarX)) {
            throw new RejectedExecutionException(h.x.d.k.i(this.f1130g, " was terminated"));
        }
        boolean z2 = z && bVarI != null;
        if (iVarD.f1154e.E() != 0) {
            t(z2);
        } else {
            if (z2) {
                return;
            }
            u();
        }
    }

    public final boolean p(b bVar) {
        long j2;
        long j3;
        int iF;
        if (bVar.g() != n) {
            return false;
        }
        do {
            j2 = this.parkedWorkersStack;
            int i2 = (int) (2097151 & j2);
            j3 = (2097152 + j2) & (-2097152);
            iF = bVar.f();
            if (r0.a()) {
                if (!(iF != 0)) {
                    throw new AssertionError();
                }
            }
            bVar.o(this.f1133j.get(i2));
        } while (!f1127k.compareAndSet(this, j2, ((long) iF) | j3));
        return true;
    }

    public final void q(b bVar, int i2, int i3) {
        while (true) {
            long j2 = this.parkedWorkersStack;
            int iM = (int) (2097151 & j2);
            long j3 = (2097152 + j2) & (-2097152);
            if (iM == i2) {
                iM = i3 == 0 ? m(bVar) : i3;
            }
            if (iM >= 0 && f1127k.compareAndSet(this, j2, j3 | ((long) iM))) {
                return;
            }
        }
    }

    public final void r(i iVar) {
        try {
            iVar.run();
        } catch (Throwable th) {
            try {
                Thread threadCurrentThread = Thread.currentThread();
                threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, th);
                kotlinx.coroutines.d dVarA = kotlinx.coroutines.e.a();
                if (dVarA == null) {
                }
            } finally {
                kotlinx.coroutines.d dVarA2 = kotlinx.coroutines.e.a();
                if (dVarA2 != null) {
                    dVarA2.e();
                }
            }
        }
    }

    public final void s(long j2) throws InterruptedException {
        int i2;
        if (m.compareAndSet(this, 0, 1)) {
            b bVarI = i();
            synchronized (this.f1133j) {
                i2 = (int) (this.controlState & 2097151);
            }
            if (1 <= i2) {
                int i3 = 1;
                while (true) {
                    int i4 = i3 + 1;
                    b bVar = this.f1133j.get(i3);
                    h.x.d.k.b(bVar);
                    b bVar2 = bVar;
                    if (bVar2 != bVarI) {
                        while (bVar2.isAlive()) {
                            LockSupport.unpark(bVar2);
                            bVar2.join(j2);
                        }
                        c cVar = bVar2.f1135e;
                        if (r0.a()) {
                            if (!(cVar == c.TERMINATED)) {
                                throw new AssertionError();
                            }
                        }
                        bVar2.b.g(this.f1132i);
                    }
                    if (i3 == i2) {
                        break;
                    } else {
                        i3 = i4;
                    }
                }
            }
            this.f1132i.b();
            this.f1131h.b();
            while (true) {
                i iVarE = bVarI == null ? null : bVarI.e(true);
                if (iVarE == null && (iVarE = this.f1131h.d()) == null && (iVarE = this.f1132i.d()) == null) {
                    break;
                } else {
                    r(iVarE);
                }
            }
            if (bVarI != null) {
                bVarI.r(c.TERMINATED);
            }
            if (r0.a()) {
                if (!(((int) ((this.controlState & 9223367638808264704L) >> 42)) == this.b)) {
                    throw new AssertionError();
                }
            }
            this.parkedWorkersStack = 0L;
            this.controlState = 0L;
        }
    }

    public String toString() {
        int i2;
        int i3;
        int i4;
        int i5;
        ArrayList arrayList = new ArrayList();
        int length = this.f1133j.length();
        int i6 = 0;
        if (1 < length) {
            i3 = 0;
            int i7 = 0;
            i4 = 0;
            i5 = 0;
            int i8 = 1;
            while (true) {
                int i9 = i8 + 1;
                b bVar = this.f1133j.get(i8);
                if (bVar != null) {
                    int iF = bVar.b.f();
                    int i10 = C0068a.a[bVar.f1135e.ordinal()];
                    if (i10 == 1) {
                        i6++;
                    } else if (i10 == 2) {
                        i3++;
                        StringBuilder sb = new StringBuilder();
                        sb.append(iF);
                        sb.append('b');
                        arrayList.add(sb.toString());
                    } else if (i10 == 3) {
                        i7++;
                        StringBuilder sb2 = new StringBuilder();
                        sb2.append(iF);
                        sb2.append('c');
                        arrayList.add(sb2.toString());
                    } else if (i10 == 4) {
                        i4++;
                        if (iF > 0) {
                            StringBuilder sb3 = new StringBuilder();
                            sb3.append(iF);
                            sb3.append('d');
                            arrayList.add(sb3.toString());
                        }
                    } else if (i10 == 5) {
                        i5++;
                    }
                }
                if (i9 >= length) {
                    break;
                }
                i8 = i9;
            }
            i2 = i6;
            i6 = i7;
        } else {
            i2 = 0;
            i3 = 0;
            i4 = 0;
            i5 = 0;
        }
        long j2 = this.controlState;
        return this.f1130g + '@' + s0.b(this) + "[Pool Size {core = " + this.b + ", max = " + this.f1128e + "}, Worker States {CPU = " + i6 + ", blocking = " + i3 + ", parked = " + i2 + ", dormant = " + i4 + ", terminated = " + i5 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.f1131h.c() + ", global blocking queue size = " + this.f1132i.c() + ", Control State {created workers= " + ((int) (2097151 & j2)) + ", blocking tasks = " + ((int) ((4398044413952L & j2) >> 21)) + ", CPUs acquired = " + (this.b - ((int) ((9223367638808264704L & j2) >> 42))) + "}]";
    }

    public final void u() {
        if (A() || z(this, 0L, 1, null)) {
            return;
        }
        A();
    }

    /* JADX INFO: compiled from: CoroutineScheduler.kt */
    public final class b extends Thread {

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        static final /* synthetic */ AtomicIntegerFieldUpdater f1134k = AtomicIntegerFieldUpdater.newUpdater(b.class, "workerCtl");
        public final m b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public c f1135e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private long f1136f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private long f1137g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private int f1138h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public boolean f1139i;
        private volatile int indexInArray;
        private volatile Object nextParkedWorker;
        volatile /* synthetic */ int workerCtl;

        private b() {
            setDaemon(true);
            this.b = new m();
            this.f1135e = c.DORMANT;
            this.workerCtl = 0;
            this.nextParkedWorker = a.n;
            this.f1138h = h.y.c.b.b();
        }

        private final void a(int i2) {
            if (i2 == 0) {
                return;
            }
            a.l.addAndGet(a.this, -2097152L);
            c cVar = this.f1135e;
            if (cVar != c.TERMINATED) {
                if (r0.a()) {
                    if (!(cVar == c.BLOCKING)) {
                        throw new AssertionError();
                    }
                }
                this.f1135e = c.DORMANT;
            }
        }

        private final void b(int i2) {
            if (i2 != 0 && r(c.BLOCKING)) {
                a.this.u();
            }
        }

        private final void c(i iVar) {
            int iE = iVar.f1154e.E();
            h(iE);
            b(iE);
            a.this.r(iVar);
            a(iE);
        }

        private final i d(boolean z) {
            i iVarL;
            i iVarL2;
            if (z) {
                boolean z2 = j(a.this.b * 2) == 0;
                if (z2 && (iVarL2 = l()) != null) {
                    return iVarL2;
                }
                i iVarH = this.b.h();
                if (iVarH != null) {
                    return iVarH;
                }
                if (!z2 && (iVarL = l()) != null) {
                    return iVarL;
                }
            } else {
                i iVarL3 = l();
                if (iVarL3 != null) {
                    return iVarL3;
                }
            }
            return s(false);
        }

        private final void h(int i2) {
            this.f1136f = 0L;
            if (this.f1135e == c.PARKING) {
                if (r0.a()) {
                    if (!(i2 == 1)) {
                        throw new AssertionError();
                    }
                }
                this.f1135e = c.BLOCKING;
            }
        }

        private final boolean i() {
            return this.nextParkedWorker != a.n;
        }

        private final void k() {
            if (this.f1136f == 0) {
                this.f1136f = System.nanoTime() + a.this.f1129f;
            }
            LockSupport.parkNanos(a.this.f1129f);
            if (System.nanoTime() - this.f1136f >= 0) {
                this.f1136f = 0L;
                t();
            }
        }

        private final i l() {
            if (j(2) == 0) {
                i iVarD = a.this.f1131h.d();
                return iVarD == null ? a.this.f1132i.d() : iVarD;
            }
            i iVarD2 = a.this.f1132i.d();
            return iVarD2 == null ? a.this.f1131h.d() : iVarD2;
        }

        private final void m() {
            loop0: while (true) {
                boolean z = false;
                while (!a.this.isTerminated() && this.f1135e != c.TERMINATED) {
                    i iVarE = e(this.f1139i);
                    if (iVarE != null) {
                        this.f1137g = 0L;
                        c(iVarE);
                    } else {
                        this.f1139i = false;
                        if (this.f1137g == 0) {
                            q();
                        } else if (z) {
                            r(c.PARKING);
                            Thread.interrupted();
                            LockSupport.parkNanos(this.f1137g);
                            this.f1137g = 0L;
                        } else {
                            z = true;
                        }
                    }
                }
                break loop0;
            }
            r(c.TERMINATED);
        }

        private final boolean p() {
            boolean z;
            if (this.f1135e != c.CPU_ACQUIRED) {
                a aVar = a.this;
                while (true) {
                    long j2 = aVar.controlState;
                    if (((int) ((9223367638808264704L & j2) >> 42)) == 0) {
                        z = false;
                        break;
                    }
                    if (a.l.compareAndSet(aVar, j2, j2 - 4398046511104L)) {
                        z = true;
                        break;
                    }
                }
                if (!z) {
                    return false;
                }
                this.f1135e = c.CPU_ACQUIRED;
            }
            return true;
        }

        private final void q() {
            if (!i()) {
                a.this.p(this);
                return;
            }
            if (r0.a()) {
                if (!(this.b.f() == 0)) {
                    throw new AssertionError();
                }
            }
            this.workerCtl = -1;
            while (i() && this.workerCtl == -1 && !a.this.isTerminated() && this.f1135e != c.TERMINATED) {
                r(c.PARKING);
                Thread.interrupted();
                k();
            }
        }

        private final i s(boolean z) {
            if (r0.a()) {
                if (!(this.b.f() == 0)) {
                    throw new AssertionError();
                }
            }
            int i2 = (int) (a.this.controlState & 2097151);
            if (i2 < 2) {
                return null;
            }
            int iJ = j(i2);
            a aVar = a.this;
            long jMin = Long.MAX_VALUE;
            for (int i3 = 0; i3 < i2; i3++) {
                iJ++;
                if (iJ > i2) {
                    iJ = 1;
                }
                b bVar = aVar.f1133j.get(iJ);
                if (bVar != null && bVar != this) {
                    if (r0.a()) {
                        if (!(this.b.f() == 0)) {
                            throw new AssertionError();
                        }
                    }
                    long jK = z ? this.b.k(bVar.b) : this.b.l(bVar.b);
                    if (jK == -1) {
                        return this.b.h();
                    }
                    if (jK > 0) {
                        jMin = Math.min(jMin, jK);
                    }
                }
            }
            if (jMin == Long.MAX_VALUE) {
                jMin = 0;
            }
            this.f1137g = jMin;
            return null;
        }

        private final void t() {
            a aVar = a.this;
            synchronized (aVar.f1133j) {
                if (aVar.isTerminated()) {
                    return;
                }
                if (((int) (aVar.controlState & 2097151)) <= aVar.b) {
                    return;
                }
                if (f1134k.compareAndSet(this, -1, 1)) {
                    int iF = f();
                    n(0);
                    aVar.q(this, iF, 0);
                    int andDecrement = (int) (2097151 & a.l.getAndDecrement(aVar));
                    if (andDecrement != iF) {
                        b bVar = aVar.f1133j.get(andDecrement);
                        h.x.d.k.b(bVar);
                        b bVar2 = bVar;
                        aVar.f1133j.set(iF, bVar2);
                        bVar2.n(iF);
                        aVar.q(bVar2, andDecrement, iF);
                    }
                    aVar.f1133j.set(andDecrement, null);
                    r rVar = r.a;
                    this.f1135e = c.TERMINATED;
                }
            }
        }

        public final i e(boolean z) {
            i iVarD;
            if (p()) {
                return d(z);
            }
            if (!z || (iVarD = this.b.h()) == null) {
                iVarD = a.this.f1132i.d();
            }
            return iVarD == null ? s(true) : iVarD;
        }

        public final int f() {
            return this.indexInArray;
        }

        public final Object g() {
            return this.nextParkedWorker;
        }

        public final int j(int i2) {
            int i3 = this.f1138h;
            int i4 = i3 ^ (i3 << 13);
            int i5 = i4 ^ (i4 >> 17);
            int i6 = i5 ^ (i5 << 5);
            this.f1138h = i6;
            int i7 = i2 - 1;
            return (i7 & i2) == 0 ? i6 & i7 : (i6 & MPv3.MAX_MESSAGE_ID) % i2;
        }

        public final void n(int i2) {
            StringBuilder sb = new StringBuilder();
            sb.append(a.this.f1130g);
            sb.append("-worker-");
            sb.append(i2 == 0 ? "TERMINATED" : String.valueOf(i2));
            setName(sb.toString());
            this.indexInArray = i2;
        }

        public final void o(Object obj) {
            this.nextParkedWorker = obj;
        }

        public final boolean r(c cVar) {
            c cVar2 = this.f1135e;
            boolean z = cVar2 == c.CPU_ACQUIRED;
            if (z) {
                a.l.addAndGet(a.this, 4398046511104L);
            }
            if (cVar2 != cVar) {
                this.f1135e = cVar;
            }
            return z;
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            m();
        }

        public b(int i2) {
            this();
            n(i2);
        }
    }
}
