package kotlinx.coroutines.scheduling;

import d2.c0;
import d2.d0;
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;
import k1.o;
import kotlinx.coroutines.internal.b0;
import kotlinx.coroutines.internal.y;

/* JADX INFO: compiled from: CoroutineScheduler.kt */
/* JADX INFO: loaded from: classes.dex */
public final class a implements Executor, Closeable {

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static final /* synthetic */ AtomicLongFieldUpdater f5242l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    static final /* synthetic */ AtomicLongFieldUpdater f5243m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private static final /* synthetic */ AtomicIntegerFieldUpdater f5244n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final b0 f5245o;
    private volatile /* synthetic */ int _isTerminated;
    volatile /* synthetic */ long controlState;

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

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final kotlinx.coroutines.scheduling.d f5250i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final kotlinx.coroutines.scheduling.d f5251j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final y<c> f5252k;
    private volatile /* synthetic */ long parkedWorkersStack;

    /* JADX INFO: renamed from: kotlinx.coroutines.scheduling.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: CoroutineScheduler.kt */
    public static final class C0073a {
        private C0073a() {
        }

        public /* synthetic */ C0073a(w1.e eVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: CoroutineScheduler.kt */
    public /* synthetic */ class b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public static final /* synthetic */ int[] f5253a;

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

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

    static {
        new C0073a(null);
        f5245o = new b0("NOT_IN_STACK");
        f5242l = AtomicLongFieldUpdater.newUpdater(a.class, "parkedWorkersStack");
        f5243m = AtomicLongFieldUpdater.newUpdater(a.class, "controlState");
        f5244n = AtomicIntegerFieldUpdater.newUpdater(a.class, "_isTerminated");
    }

    public a(int i3, int i4, long j3, String str) {
        this.f5246e = i3;
        this.f5247f = i4;
        this.f5248g = j3;
        this.f5249h = str;
        if (!(i3 >= 1)) {
            throw new IllegalArgumentException(("Core pool size " + i3 + " should be at least 1").toString());
        }
        if (!(i4 >= i3)) {
            throw new IllegalArgumentException(("Max pool size " + i4 + " should be greater than or equals to core pool size " + i3).toString());
        }
        if (!(i4 <= 2097150)) {
            throw new IllegalArgumentException(("Max pool size " + i4 + " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (!(j3 > 0)) {
            throw new IllegalArgumentException(("Idle worker keep alive time " + j3 + " must be positive").toString());
        }
        this.f5250i = new kotlinx.coroutines.scheduling.d();
        this.f5251j = new kotlinx.coroutines.scheduling.d();
        this.parkedWorkersStack = 0L;
        this.f5252k = new y<>(i3 + 1);
        this.controlState = ((long) i3) << 42;
        this._isTerminated = 0;
    }

    private final boolean a(h hVar) {
        return hVar.f5272f.a() == 1 ? this.f5251j.a(hVar) : this.f5250i.a(hVar);
    }

    private final int b() {
        synchronized (this.f5252k) {
            if (g()) {
                return -1;
            }
            long j3 = this.controlState;
            int i3 = (int) (j3 & 2097151);
            int iA = z1.f.a(i3 - ((int) ((j3 & 4398044413952L) >> 21)), 0);
            if (iA >= this.f5246e) {
                return 0;
            }
            if (i3 >= this.f5247f) {
                return 0;
            }
            int i4 = ((int) (this.controlState & 2097151)) + 1;
            if (!(i4 > 0 && this.f5252k.b(i4) == null)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            c cVar = new c(i4);
            this.f5252k.c(i4, cVar);
            if (!(i4 == ((int) (2097151 & f5243m.incrementAndGet(this))))) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            cVar.start();
            return iA + 1;
        }
    }

    private final c d() {
        Thread threadCurrentThread = Thread.currentThread();
        c cVar = threadCurrentThread instanceof c ? (c) threadCurrentThread : null;
        if (cVar != null && w1.g.a(a.this, this)) {
            return cVar;
        }
        return null;
    }

    public static /* synthetic */ void f(a aVar, Runnable runnable, i iVar, boolean z2, int i3, Object obj) {
        if ((i3 & 2) != 0) {
            iVar = l.f5280f;
        }
        if ((i3 & 4) != 0) {
            z2 = false;
        }
        aVar.e(runnable, iVar, z2);
    }

    private final int h(c cVar) {
        Object objH = cVar.h();
        while (objH != f5245o) {
            if (objH == null) {
                return 0;
            }
            c cVar2 = (c) objH;
            int iG = cVar2.g();
            if (iG != 0) {
                return iG;
            }
            objH = cVar2.h();
        }
        return -1;
    }

    private final c i() {
        while (true) {
            long j3 = this.parkedWorkersStack;
            c cVarB = this.f5252k.b((int) (2097151 & j3));
            if (cVarB == null) {
                return null;
            }
            long j4 = (2097152 + j3) & (-2097152);
            int iH = h(cVarB);
            if (iH >= 0 && f5242l.compareAndSet(this, j3, ((long) iH) | j4)) {
                cVarB.p(f5245o);
                return cVarB;
            }
        }
    }

    private final void n(boolean z2) {
        long jAddAndGet = f5243m.addAndGet(this, 2097152L);
        if (z2 || s() || q(jAddAndGet)) {
            return;
        }
        s();
    }

    private final h p(c cVar, h hVar, boolean z2) {
        if (cVar == null || cVar.f5256f == d.TERMINATED) {
            return hVar;
        }
        if (hVar.f5272f.a() == 0 && cVar.f5256f == d.BLOCKING) {
            return hVar;
        }
        cVar.f5260j = true;
        return cVar.f5255e.a(hVar, z2);
    }

    private final boolean q(long j3) {
        if (z1.f.a(((int) (2097151 & j3)) - ((int) ((j3 & 4398044413952L) >> 21)), 0) < this.f5246e) {
            int iB = b();
            if (iB == 1 && this.f5246e > 1) {
                b();
            }
            if (iB > 0) {
                return true;
            }
        }
        return false;
    }

    static /* synthetic */ boolean r(a aVar, long j3, int i3, Object obj) {
        if ((i3 & 1) != 0) {
            j3 = aVar.controlState;
        }
        return aVar.q(j3);
    }

    private final boolean s() {
        c cVarI;
        do {
            cVarI = i();
            if (cVarI == null) {
                return false;
            }
        } while (!c.f5254l.compareAndSet(cVarI, -1, 0));
        LockSupport.unpark(cVarI);
        return true;
    }

    public final h c(Runnable runnable, i iVar) {
        long jA = l.f5279e.a();
        if (!(runnable instanceof h)) {
            return new k(runnable, jA, iVar);
        }
        h hVar = (h) runnable;
        hVar.f5271e = jA;
        hVar.f5272f = iVar;
        return hVar;
    }

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

    public final void e(Runnable runnable, i iVar, boolean z2) {
        d2.c.a();
        h hVarC = c(runnable, iVar);
        c cVarD = d();
        h hVarP = p(cVarD, hVarC, z2);
        if (hVarP != null && !a(hVarP)) {
            throw new RejectedExecutionException(w1.g.j(this.f5249h, " was terminated"));
        }
        boolean z3 = z2 && cVarD != null;
        if (hVarC.f5272f.a() != 0) {
            n(z3);
        } else {
            if (z3) {
                return;
            }
            o();
        }
    }

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

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

    public final boolean j(c cVar) {
        long j3;
        long j4;
        int iG;
        if (cVar.h() != f5245o) {
            return false;
        }
        do {
            j3 = this.parkedWorkersStack;
            int i3 = (int) (2097151 & j3);
            j4 = (2097152 + j3) & (-2097152);
            iG = cVar.g();
            if (c0.a()) {
                if (!(iG != 0)) {
                    throw new AssertionError();
                }
            }
            cVar.p(this.f5252k.b(i3));
        } while (!f5242l.compareAndSet(this, j3, ((long) iG) | j4));
        return true;
    }

    public final void k(c cVar, int i3, int i4) {
        while (true) {
            long j3 = this.parkedWorkersStack;
            int iH = (int) (2097151 & j3);
            long j4 = (2097152 + j3) & (-2097152);
            if (iH == i3) {
                iH = i4 == 0 ? h(cVar) : i4;
            }
            if (iH >= 0 && f5242l.compareAndSet(this, j3, j4 | ((long) iH))) {
                return;
            }
        }
    }

    public final void l(h hVar) {
        try {
            hVar.run();
        } finally {
            try {
            } finally {
            }
        }
    }

    public final void m(long j3) throws InterruptedException {
        int i3;
        if (f5244n.compareAndSet(this, 0, 1)) {
            c cVarD = d();
            synchronized (this.f5252k) {
                i3 = (int) (this.controlState & 2097151);
            }
            if (1 <= i3) {
                int i4 = 1;
                while (true) {
                    int i5 = i4 + 1;
                    c cVarB = this.f5252k.b(i4);
                    w1.g.b(cVarB);
                    c cVar = cVarB;
                    if (cVar != cVarD) {
                        while (cVar.isAlive()) {
                            LockSupport.unpark(cVar);
                            cVar.join(j3);
                        }
                        d dVar = cVar.f5256f;
                        if (c0.a()) {
                            if (!(dVar == d.TERMINATED)) {
                                throw new AssertionError();
                            }
                        }
                        cVar.f5255e.g(this.f5251j);
                    }
                    if (i4 == i3) {
                        break;
                    } else {
                        i4 = i5;
                    }
                }
            }
            this.f5251j.b();
            this.f5250i.b();
            while (true) {
                h hVarF = cVarD == null ? null : cVarD.f(true);
                if (hVarF == null && (hVarF = this.f5250i.d()) == null && (hVarF = this.f5251j.d()) == null) {
                    break;
                } else {
                    l(hVarF);
                }
            }
            if (cVarD != null) {
                cVarD.s(d.TERMINATED);
            }
            if (c0.a()) {
                if (!(((int) ((this.controlState & 9223367638808264704L) >> 42)) == this.f5246e)) {
                    throw new AssertionError();
                }
            }
            this.parkedWorkersStack = 0L;
            this.controlState = 0L;
        }
    }

    public final void o() {
        if (s() || r(this, 0L, 1, null)) {
            return;
        }
        s();
    }

    public String toString() {
        ArrayList arrayList = new ArrayList();
        int iA = this.f5252k.a();
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        int i6 = 0;
        int i7 = 0;
        int i8 = 1;
        while (i8 < iA) {
            int i9 = i8 + 1;
            c cVarB = this.f5252k.b(i8);
            if (cVarB != null) {
                int iF = cVarB.f5255e.f();
                int i10 = b.f5253a[cVarB.f5256f.ordinal()];
                if (i10 == 1) {
                    i5++;
                } else if (i10 == 2) {
                    i4++;
                    StringBuilder sb = new StringBuilder();
                    sb.append(iF);
                    sb.append('b');
                    arrayList.add(sb.toString());
                } else if (i10 == 3) {
                    i3++;
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(iF);
                    sb2.append('c');
                    arrayList.add(sb2.toString());
                } else if (i10 == 4) {
                    i6++;
                    if (iF > 0) {
                        StringBuilder sb3 = new StringBuilder();
                        sb3.append(iF);
                        sb3.append('d');
                        arrayList.add(sb3.toString());
                    }
                } else if (i10 == 5) {
                    i7++;
                }
            }
            i8 = i9;
        }
        long j3 = this.controlState;
        return this.f5249h + '@' + d0.b(this) + "[Pool Size {core = " + this.f5246e + ", max = " + this.f5247f + "}, Worker States {CPU = " + i3 + ", blocking = " + i4 + ", parked = " + i5 + ", dormant = " + i6 + ", terminated = " + i7 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.f5250i.c() + ", global blocking queue size = " + this.f5251j.c() + ", Control State {created workers= " + ((int) (2097151 & j3)) + ", blocking tasks = " + ((int) ((4398044413952L & j3) >> 21)) + ", CPUs acquired = " + (this.f5246e - ((int) ((9223367638808264704L & j3) >> 42))) + "}]";
    }

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

        /* JADX INFO: renamed from: l, reason: collision with root package name */
        static final /* synthetic */ AtomicIntegerFieldUpdater f5254l = AtomicIntegerFieldUpdater.newUpdater(c.class, "workerCtl");

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public d f5256f;

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

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

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private int f5259i;
        private volatile int indexInArray;

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

        private c() {
            setDaemon(true);
            this.f5255e = new n();
            this.f5256f = d.DORMANT;
            this.workerCtl = 0;
            this.nextParkedWorker = a.f5245o;
            this.f5259i = x1.c.f5938e.b();
        }

        private final void b(int i3) {
            if (i3 == 0) {
                return;
            }
            a.f5243m.addAndGet(a.this, -2097152L);
            d dVar = this.f5256f;
            if (dVar != d.TERMINATED) {
                if (c0.a()) {
                    if (!(dVar == d.BLOCKING)) {
                        throw new AssertionError();
                    }
                }
                this.f5256f = d.DORMANT;
            }
        }

        private final void c(int i3) {
            if (i3 != 0 && s(d.BLOCKING)) {
                a.this.o();
            }
        }

        private final void d(h hVar) {
            int iA = hVar.f5272f.a();
            i(iA);
            c(iA);
            a.this.l(hVar);
            b(iA);
        }

        private final h e(boolean z2) {
            h hVarM;
            h hVarM2;
            if (z2) {
                boolean z3 = k(a.this.f5246e * 2) == 0;
                if (z3 && (hVarM2 = m()) != null) {
                    return hVarM2;
                }
                h hVarH = this.f5255e.h();
                if (hVarH != null) {
                    return hVarH;
                }
                if (!z3 && (hVarM = m()) != null) {
                    return hVarM;
                }
            } else {
                h hVarM3 = m();
                if (hVarM3 != null) {
                    return hVarM3;
                }
            }
            return t(false);
        }

        private final void i(int i3) {
            this.f5257g = 0L;
            if (this.f5256f == d.PARKING) {
                if (c0.a()) {
                    if (!(i3 == 1)) {
                        throw new AssertionError();
                    }
                }
                this.f5256f = d.BLOCKING;
            }
        }

        private final boolean j() {
            return this.nextParkedWorker != a.f5245o;
        }

        private final void l() {
            if (this.f5257g == 0) {
                this.f5257g = System.nanoTime() + a.this.f5248g;
            }
            LockSupport.parkNanos(a.this.f5248g);
            if (System.nanoTime() - this.f5257g >= 0) {
                this.f5257g = 0L;
                u();
            }
        }

        private final h m() {
            if (k(2) == 0) {
                h hVarD = a.this.f5250i.d();
                return hVarD == null ? a.this.f5251j.d() : hVarD;
            }
            h hVarD2 = a.this.f5251j.d();
            return hVarD2 == null ? a.this.f5250i.d() : hVarD2;
        }

        private final void n() {
            loop0: while (true) {
                boolean z2 = false;
                while (!a.this.g() && this.f5256f != d.TERMINATED) {
                    h hVarF = f(this.f5260j);
                    if (hVarF != null) {
                        this.f5258h = 0L;
                        d(hVarF);
                    } else {
                        this.f5260j = false;
                        if (this.f5258h == 0) {
                            r();
                        } else if (z2) {
                            s(d.PARKING);
                            Thread.interrupted();
                            LockSupport.parkNanos(this.f5258h);
                            this.f5258h = 0L;
                        } else {
                            z2 = true;
                        }
                    }
                }
                break loop0;
            }
            s(d.TERMINATED);
        }

        private final boolean q() {
            boolean z2;
            if (this.f5256f != d.CPU_ACQUIRED) {
                a aVar = a.this;
                while (true) {
                    long j3 = aVar.controlState;
                    if (((int) ((9223367638808264704L & j3) >> 42)) == 0) {
                        z2 = false;
                        break;
                    }
                    if (a.f5243m.compareAndSet(aVar, j3, j3 - 4398046511104L)) {
                        z2 = true;
                        break;
                    }
                }
                if (!z2) {
                    return false;
                }
                this.f5256f = d.CPU_ACQUIRED;
            }
            return true;
        }

        private final void r() {
            if (!j()) {
                a.this.j(this);
                return;
            }
            if (c0.a()) {
                if (!(this.f5255e.f() == 0)) {
                    throw new AssertionError();
                }
            }
            this.workerCtl = -1;
            while (j() && this.workerCtl == -1 && !a.this.g() && this.f5256f != d.TERMINATED) {
                s(d.PARKING);
                Thread.interrupted();
                l();
            }
        }

        private final h t(boolean z2) {
            if (c0.a()) {
                if (!(this.f5255e.f() == 0)) {
                    throw new AssertionError();
                }
            }
            int i3 = (int) (a.this.controlState & 2097151);
            if (i3 < 2) {
                return null;
            }
            int iK = k(i3);
            a aVar = a.this;
            int i4 = 0;
            long jMin = Long.MAX_VALUE;
            while (i4 < i3) {
                i4++;
                iK++;
                if (iK > i3) {
                    iK = 1;
                }
                c cVarB = aVar.f5252k.b(iK);
                if (cVarB != null && cVarB != this) {
                    if (c0.a()) {
                        if (!(this.f5255e.f() == 0)) {
                            throw new AssertionError();
                        }
                    }
                    long jK = z2 ? this.f5255e.k(cVarB.f5255e) : this.f5255e.l(cVarB.f5255e);
                    if (jK == -1) {
                        return this.f5255e.h();
                    }
                    if (jK > 0) {
                        jMin = Math.min(jMin, jK);
                    }
                }
            }
            if (jMin == Long.MAX_VALUE) {
                jMin = 0;
            }
            this.f5258h = jMin;
            return null;
        }

        private final void u() {
            a aVar = a.this;
            synchronized (aVar.f5252k) {
                if (aVar.g()) {
                    return;
                }
                if (((int) (aVar.controlState & 2097151)) <= aVar.f5246e) {
                    return;
                }
                if (f5254l.compareAndSet(this, -1, 1)) {
                    int iG = g();
                    o(0);
                    aVar.k(this, iG, 0);
                    int andDecrement = (int) (a.f5243m.getAndDecrement(aVar) & 2097151);
                    if (andDecrement != iG) {
                        c cVarB = aVar.f5252k.b(andDecrement);
                        w1.g.b(cVarB);
                        c cVar = cVarB;
                        aVar.f5252k.c(iG, cVar);
                        cVar.o(iG);
                        aVar.k(cVar, andDecrement, iG);
                    }
                    aVar.f5252k.c(andDecrement, null);
                    o oVar = o.f5171a;
                    this.f5256f = d.TERMINATED;
                }
            }
        }

        public final h f(boolean z2) {
            h hVarD;
            if (q()) {
                return e(z2);
            }
            if (!z2 || (hVarD = this.f5255e.h()) == null) {
                hVarD = a.this.f5251j.d();
            }
            return hVarD == null ? t(true) : hVarD;
        }

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

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

        public final int k(int i3) {
            int i4 = this.f5259i;
            int i5 = i4 ^ (i4 << 13);
            int i6 = i5 ^ (i5 >> 17);
            int i7 = i6 ^ (i6 << 5);
            this.f5259i = i7;
            int i8 = i3 - 1;
            return (i8 & i3) == 0 ? i8 & i7 : (Integer.MAX_VALUE & i7) % i3;
        }

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

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

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

        public final boolean s(d dVar) {
            d dVar2 = this.f5256f;
            boolean z2 = dVar2 == d.CPU_ACQUIRED;
            if (z2) {
                a.f5243m.addAndGet(a.this, 4398046511104L);
            }
            if (dVar2 != dVar) {
                this.f5256f = dVar;
            }
            return z2;
        }

        public c(int i3) {
            this();
            o(i3);
        }
    }
}
