package h.a.l1;

import g.i0.c;
import h.a.k1.p;
import h.a.k1.r;
import h.a.u;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Objects;
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: compiled from: CoroutineScheduler.kt */
/* JADX INFO: loaded from: classes.dex */
@g.i
public final class a implements Executor, Closeable {
    private volatile /* synthetic */ int _isTerminated;
    public volatile /* synthetic */ long controlState;

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

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

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final d f4286l;
    public final d m;
    public final p<C0082a> n;
    private volatile /* synthetic */ long parkedWorkersStack;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final r f4281g = new r("NOT_IN_STACK");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f4278b = AtomicLongFieldUpdater.newUpdater(a.class, "parkedWorkersStack");

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f4279e = AtomicLongFieldUpdater.newUpdater(a.class, "controlState");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f4280f = AtomicIntegerFieldUpdater.newUpdater(a.class, "_isTerminated");

    /* JADX INFO: renamed from: h.a.l1.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: CoroutineScheduler.kt */
    @g.i
    public final class C0082a extends Thread {

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

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

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

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

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

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

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

        public C0082a(int i2) {
            a.this = a.this;
            setDaemon(true);
            this.f4288e = new n();
            this.f4289f = b.DORMANT;
            this.workerCtl = 0;
            this.nextParkedWorker = a.f4281g;
            c.a aVar = g.i0.c.f4114b;
            this.f4292i = g.i0.c.f4115e.a();
            f(i2);
        }

        public final h a(boolean z) {
            boolean z2;
            h hVarD;
            h hVarE;
            h hVarE2;
            boolean z3;
            if (this.f4289f == b.CPU_ACQUIRED) {
                z2 = true;
            } else {
                a aVar = a.this;
                while (true) {
                    long j2 = aVar.controlState;
                    if (((int) ((9223367638808264704L & j2) >> 42)) == 0) {
                        z3 = false;
                        break;
                    }
                    if (a.f4279e.compareAndSet(aVar, j2, j2 - 4398046511104L)) {
                        z3 = true;
                        break;
                    }
                }
                if (z3) {
                    this.f4289f = b.CPU_ACQUIRED;
                    z2 = true;
                } else {
                    z2 = false;
                }
            }
            if (!z2) {
                if (!z || (hVarD = this.f4288e.e()) == null) {
                    hVarD = a.this.m.d();
                }
                return hVarD == null ? i(true) : hVarD;
            }
            if (z) {
                boolean z4 = d(a.this.f4282h * 2) == 0;
                if (z4 && (hVarE2 = e()) != null) {
                    return hVarE2;
                }
                h hVarE3 = this.f4288e.e();
                if (hVarE3 != null) {
                    return hVarE3;
                }
                if (!z4 && (hVarE = e()) != null) {
                    return hVarE;
                }
            } else {
                h hVarE4 = e();
                if (hVarE4 != null) {
                    return hVarE4;
                }
            }
            return i(false);
        }

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

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

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

        public final h e() {
            if (d(2) == 0) {
                h hVarD = a.this.f4286l.d();
                return hVarD == null ? a.this.m.d() : hVarD;
            }
            h hVarD2 = a.this.m.d();
            return hVarD2 == null ? a.this.f4286l.d() : hVarD2;
        }

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

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

        public final boolean h(b bVar) {
            b bVar2 = this.f4289f;
            boolean z = bVar2 == b.CPU_ACQUIRED;
            if (z) {
                a.f4279e.addAndGet(a.this, 4398046511104L);
            }
            if (bVar2 != bVar) {
                this.f4289f = bVar;
            }
            return z;
        }

        public final h i(boolean z) {
            long jH;
            int i2 = (int) (a.this.controlState & 2097151);
            if (i2 < 2) {
                return null;
            }
            int iD = d(i2);
            a aVar = a.this;
            int i3 = 0;
            long jMin = Long.MAX_VALUE;
            while (i3 < i2) {
                i3++;
                iD++;
                if (iD > i2) {
                    iD = 1;
                }
                C0082a c0082aB = aVar.n.b(iD);
                if (c0082aB != null && c0082aB != this) {
                    if (z) {
                        jH = this.f4288e.g(c0082aB.f4288e);
                    } else {
                        n nVar = this.f4288e;
                        n nVar2 = c0082aB.f4288e;
                        Objects.requireNonNull(nVar);
                        h hVarF = nVar2.f();
                        if (hVarF != null) {
                            nVar.a(hVarF, false);
                            jH = -1;
                        } else {
                            jH = nVar.h(nVar2, false);
                        }
                    }
                    if (jH == -1) {
                        return this.f4288e.e();
                    }
                    if (jH > 0) {
                        jMin = Math.min(jMin, jH);
                    }
                }
            }
            if (jMin == Long.MAX_VALUE) {
                jMin = 0;
            }
            this.f4291h = jMin;
            return null;
        }

        /* JADX WARN: Code restructure failed: missing block: B:99:0x0002, code lost:
        
            continue;
         */
        @Override // java.lang.Thread, java.lang.Runnable
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void run() {
            /*
                Method dump skipped, instruction units count: 312
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: h.a.l1.a.C0082a.run():void");
        }
    }

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

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

    public static /* synthetic */ void e(a aVar, Runnable runnable, i iVar, boolean z, int i2) {
        i iVar2 = (i2 & 2) != 0 ? l.f4310f : null;
        if ((i2 & 4) != 0) {
            z = false;
        }
        aVar.d(runnable, iVar2, z);
    }

    public final int a() {
        synchronized (this.n) {
            if (this._isTerminated != 0) {
                return -1;
            }
            long j2 = this.controlState;
            int i2 = (int) (j2 & 2097151);
            int i3 = i2 - ((int) ((j2 & 4398044413952L) >> 21));
            if (i3 < 0) {
                i3 = 0;
            }
            if (i3 >= this.f4282h) {
                return 0;
            }
            if (i2 >= this.f4283i) {
                return 0;
            }
            int i4 = ((int) (this.controlState & 2097151)) + 1;
            if (!(i4 > 0 && this.n.b(i4) == null)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            C0082a c0082a = new C0082a(i4);
            this.n.c(i4, c0082a);
            if (!(i4 == ((int) (2097151 & f4279e.incrementAndGet(this))))) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            c0082a.start();
            return i3 + 1;
        }
    }

    public final C0082a b() {
        Thread threadCurrentThread = Thread.currentThread();
        C0082a c0082a = threadCurrentThread instanceof C0082a ? (C0082a) threadCurrentThread : null;
        if (c0082a != null && g.h0.c.l.a(a.this, this)) {
            return c0082a;
        }
        return null;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws InterruptedException {
        int i2;
        boolean z;
        if (f4280f.compareAndSet(this, 0, 1)) {
            C0082a c0082aB = b();
            synchronized (this.n) {
                i2 = (int) (this.controlState & 2097151);
            }
            if (1 <= i2) {
                int i3 = 1;
                while (true) {
                    int i4 = i3 + 1;
                    C0082a c0082aB2 = this.n.b(i3);
                    g.h0.c.l.c(c0082aB2);
                    C0082a c0082a = c0082aB2;
                    if (c0082a != c0082aB) {
                        while (c0082a.isAlive()) {
                            LockSupport.unpark(c0082a);
                            c0082a.join(10000L);
                        }
                        n nVar = c0082a.f4288e;
                        d dVar = this.m;
                        Objects.requireNonNull(nVar);
                        h hVar = (h) n.a.getAndSet(nVar, null);
                        if (hVar != null) {
                            dVar.a(hVar);
                        }
                        do {
                            h hVarF = nVar.f();
                            if (hVarF == null) {
                                z = false;
                            } else {
                                dVar.a(hVarF);
                                z = true;
                            }
                        } while (z);
                    }
                    if (i3 == i2) {
                        break;
                    } else {
                        i3 = i4;
                    }
                }
            }
            this.m.b();
            this.f4286l.b();
            while (true) {
                h hVarA = c0082aB == null ? null : c0082aB.a(true);
                if (hVarA == null && (hVarA = this.f4286l.d()) == null && (hVarA = this.m.d()) == null) {
                    break;
                } else {
                    m(hVarA);
                }
            }
            if (c0082aB != null) {
                c0082aB.h(b.TERMINATED);
            }
            this.parkedWorkersStack = 0L;
            this.controlState = 0L;
        }
    }

    public final void d(Runnable runnable, i iVar, boolean z) {
        h kVar;
        h hVarA;
        long jA = l.f4309e.a();
        if (runnable instanceof h) {
            kVar = (h) runnable;
            kVar.f4303b = jA;
            kVar.f4304e = iVar;
        } else {
            kVar = new k(runnable, jA, iVar);
        }
        C0082a c0082aB = b();
        if (c0082aB == null || c0082aB.f4289f == b.TERMINATED || (kVar.f4304e.b() == 0 && c0082aB.f4289f == b.BLOCKING)) {
            hVarA = kVar;
        } else {
            c0082aB.f4293j = true;
            hVarA = c0082aB.f4288e.a(kVar, z);
        }
        if (hVarA != null) {
            if (!(hVarA.f4304e.b() == 1 ? this.m.a(hVarA) : this.f4286l.a(hVarA))) {
                throw new RejectedExecutionException(g.h0.c.l.k(this.f4285k, " was terminated"));
            }
        }
        boolean z2 = z && c0082aB != null;
        if (kVar.f4304e.b() == 0) {
            if (z2) {
                return;
            }
            o();
        } else {
            long jAddAndGet = f4279e.addAndGet(this, 2097152L);
            if (z2 || q() || p(jAddAndGet)) {
                return;
            }
            q();
        }
    }

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

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

    public final int g(C0082a c0082a) {
        Object objC = c0082a.c();
        while (objC != f4281g) {
            if (objC == null) {
                return 0;
            }
            C0082a c0082a2 = (C0082a) objC;
            int iB = c0082a2.b();
            if (iB != 0) {
                return iB;
            }
            objC = c0082a2.c();
        }
        return -1;
    }

    public final boolean i(C0082a c0082a) {
        long j2;
        int iB;
        if (c0082a.c() != f4281g) {
            return false;
        }
        do {
            j2 = this.parkedWorkersStack;
            iB = c0082a.b();
            c0082a.g(this.n.b((int) (2097151 & j2)));
        } while (!f4278b.compareAndSet(this, j2, ((2097152 + j2) & (-2097152)) | ((long) iB)));
        return true;
    }

    public final void l(C0082a c0082a, int i2, int i3) {
        while (true) {
            long j2 = this.parkedWorkersStack;
            int iG = (int) (2097151 & j2);
            long j3 = (2097152 + j2) & (-2097152);
            if (iG == i2) {
                iG = i3 == 0 ? g(c0082a) : i3;
            }
            if (iG >= 0 && f4278b.compareAndSet(this, j2, j3 | ((long) iG))) {
                return;
            }
        }
    }

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

    public final void o() {
        if (q() || p(this.controlState)) {
            return;
        }
        q();
    }

    public final boolean p(long j2) {
        int i2 = ((int) (2097151 & j2)) - ((int) ((j2 & 4398044413952L) >> 21));
        if (i2 < 0) {
            i2 = 0;
        }
        if (i2 < this.f4282h) {
            int iA = a();
            if (iA == 1 && this.f4282h > 1) {
                a();
            }
            if (iA > 0) {
                return true;
            }
        }
        return false;
    }

    public final boolean q() {
        while (true) {
            long j2 = this.parkedWorkersStack;
            C0082a c0082aB = this.n.b((int) (2097151 & j2));
            if (c0082aB == null) {
                c0082aB = null;
            } else {
                long j3 = (2097152 + j2) & (-2097152);
                int iG = g(c0082aB);
                if (iG >= 0 && f4278b.compareAndSet(this, j2, ((long) iG) | j3)) {
                    c0082aB.g(f4281g);
                }
            }
            if (c0082aB == null) {
                return false;
            }
            if (C0082a.f4287b.compareAndSet(c0082aB, -1, 0)) {
                LockSupport.unpark(c0082aB);
                return true;
            }
        }
    }

    public String toString() {
        ArrayList arrayList = new ArrayList();
        int iA = this.n.a();
        int i2 = 0;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        int i6 = 0;
        int i7 = 1;
        while (i7 < iA) {
            int i8 = i7 + 1;
            C0082a c0082aB = this.n.b(i7);
            if (c0082aB != null) {
                int iD = c0082aB.f4288e.d();
                int iOrdinal = c0082aB.f4289f.ordinal();
                if (iOrdinal == 0) {
                    i2++;
                    StringBuilder sb = new StringBuilder();
                    sb.append(iD);
                    sb.append('c');
                    arrayList.add(sb.toString());
                } else if (iOrdinal == 1) {
                    i3++;
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(iD);
                    sb2.append('b');
                    arrayList.add(sb2.toString());
                } else if (iOrdinal == 2) {
                    i4++;
                } else if (iOrdinal == 3) {
                    i5++;
                    if (iD > 0) {
                        StringBuilder sb3 = new StringBuilder();
                        sb3.append(iD);
                        sb3.append('d');
                        arrayList.add(sb3.toString());
                    }
                } else if (iOrdinal == 4) {
                    i6++;
                }
            }
            i7 = i8;
        }
        long j2 = this.controlState;
        return this.f4285k + '@' + u.b(this) + "[Pool Size {core = " + this.f4282h + ", max = " + this.f4283i + "}, Worker States {CPU = " + i2 + ", blocking = " + i3 + ", parked = " + i4 + ", dormant = " + i5 + ", terminated = " + i6 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.f4286l.c() + ", global blocking queue size = " + this.m.c() + ", Control State {created workers= " + ((int) (2097151 & j2)) + ", blocking tasks = " + ((int) ((4398044413952L & j2) >> 21)) + ", CPUs acquired = " + (this.f4282h - ((int) ((9223367638808264704L & j2) >> 42))) + "}]";
    }
}
