package bq;

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 jn.o;
import kotlin.jvm.internal.o0;
import kotlin.jvm.internal.t;
import rm.n0;
import tp.q0;
import zp.c0;
import zp.h0;

/* loaded from: classes3.dex */
public final class a implements Executor, Closeable {
    private volatile int _isTerminated;
    private volatile long controlState;

    /* renamed from: p, reason: collision with root package name */
    public final int f7204p;
    private volatile long parkedWorkersStack;

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

    /* renamed from: r, reason: collision with root package name */
    public final long f7206r;

    /* renamed from: s, reason: collision with root package name */
    public final String f7207s;

    /* renamed from: t, reason: collision with root package name */
    public final bq.d f7208t;

    /* renamed from: u, reason: collision with root package name */
    public final bq.d f7209u;

    /* renamed from: v, reason: collision with root package name */
    public final c0 f7210v;

    /* renamed from: w, reason: collision with root package name */
    public static final C0186a f7200w = new C0186a(null);

    /* renamed from: x, reason: collision with root package name */
    private static final AtomicLongFieldUpdater f7201x = AtomicLongFieldUpdater.newUpdater(a.class, "parkedWorkersStack");

    /* renamed from: y, reason: collision with root package name */
    private static final AtomicLongFieldUpdater f7202y = AtomicLongFieldUpdater.newUpdater(a.class, "controlState");

    /* renamed from: z, reason: collision with root package name */
    private static final AtomicIntegerFieldUpdater f7203z = AtomicIntegerFieldUpdater.newUpdater(a.class, "_isTerminated");
    public static final h0 A = new h0("NOT_IN_STACK");

    /* renamed from: bq.a$a, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    public static final class C0186a {
        private C0186a() {
        }

        public /* synthetic */ C0186a(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    /* loaded from: classes3.dex */
    public /* synthetic */ class b {

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

        static {
            int[] iArr = new int[d.values().length];
            try {
                iArr[d.PARKING.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[d.BLOCKING.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                iArr[d.CPU_ACQUIRED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                iArr[d.DORMANT.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                iArr[d.TERMINATED.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            f7211a = iArr;
        }
    }

    /* loaded from: classes3.dex */
    public enum d {
        CPU_ACQUIRED,
        BLOCKING,
        PARKING,
        DORMANT,
        TERMINATED
    }

    public a(int i10, int i11, long j10, String str) {
        this.f7204p = i10;
        this.f7205q = i11;
        this.f7206r = j10;
        this.f7207s = str;
        if (!(i10 >= 1)) {
            throw new IllegalArgumentException(("Core pool size " + i10 + " should be at least 1").toString());
        }
        if (!(i11 >= i10)) {
            throw new IllegalArgumentException(("Max pool size " + i11 + " should be greater than or equals to core pool size " + i10).toString());
        }
        if (!(i11 <= 2097150)) {
            throw new IllegalArgumentException(("Max pool size " + i11 + " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (!(j10 > 0)) {
            throw new IllegalArgumentException(("Idle worker keep alive time " + j10 + " must be positive").toString());
        }
        this.f7208t = new bq.d();
        this.f7209u = new bq.d();
        this.f7210v = new c0((i10 + 1) * 2);
        this.controlState = i10 << 42;
        this._isTerminated = 0;
    }

    private final boolean A(long j10) {
        int d10;
        d10 = o.d(((int) (2097151 & j10)) - ((int) ((j10 & 4398044413952L) >> 21)), 0);
        if (d10 < this.f7204p) {
            int c10 = c();
            if (c10 == 1 && this.f7204p > 1) {
                c();
            }
            if (c10 > 0) {
                return true;
            }
        }
        return false;
    }

    static /* synthetic */ boolean C(a aVar, long j10, int i10, Object obj) {
        if ((i10 & 1) != 0) {
            j10 = f7202y.get(aVar);
        }
        return aVar.A(j10);
    }

    private final boolean G() {
        c m10;
        do {
            m10 = m();
            if (m10 == null) {
                return false;
            }
        } while (!c.j().compareAndSet(m10, -1, 0));
        LockSupport.unpark(m10);
        return true;
    }

    private final boolean b(h hVar) {
        return hVar.f7231q.b() == 1 ? this.f7209u.a(hVar) : this.f7208t.a(hVar);
    }

    private final int c() {
        int d10;
        synchronized (this.f7210v) {
            if (isTerminated()) {
                return -1;
            }
            AtomicLongFieldUpdater atomicLongFieldUpdater = f7202y;
            long j10 = atomicLongFieldUpdater.get(this);
            int i10 = (int) (j10 & 2097151);
            d10 = o.d(i10 - ((int) ((j10 & 4398044413952L) >> 21)), 0);
            if (d10 >= this.f7204p) {
                return 0;
            }
            if (i10 >= this.f7205q) {
                return 0;
            }
            int i11 = ((int) (f7202y.get(this) & 2097151)) + 1;
            if (!(i11 > 0 && this.f7210v.b(i11) == null)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            c cVar = new c(this, i11);
            this.f7210v.c(i11, cVar);
            if (!(i11 == ((int) (2097151 & atomicLongFieldUpdater.incrementAndGet(this))))) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            int i12 = d10 + 1;
            cVar.start();
            return i12;
        }
    }

    private final c f() {
        Thread currentThread = Thread.currentThread();
        c cVar = currentThread instanceof c ? (c) currentThread : null;
        if (cVar == null || !t.b(a.this, this)) {
            return null;
        }
        return cVar;
    }

    public static /* synthetic */ void h(a aVar, Runnable runnable, i iVar, boolean z10, int i10, Object obj) {
        if ((i10 & 2) != 0) {
            iVar = l.f7240g;
        }
        if ((i10 & 4) != 0) {
            z10 = false;
        }
        aVar.g(runnable, iVar, z10);
    }

    private final int j(c cVar) {
        Object i10 = cVar.i();
        while (i10 != A) {
            if (i10 == null) {
                return 0;
            }
            c cVar2 = (c) i10;
            int h10 = cVar2.h();
            if (h10 != 0) {
                return h10;
            }
            i10 = cVar2.i();
        }
        return -1;
    }

    private final c m() {
        AtomicLongFieldUpdater atomicLongFieldUpdater = f7201x;
        while (true) {
            long j10 = atomicLongFieldUpdater.get(this);
            c cVar = (c) this.f7210v.b((int) (2097151 & j10));
            if (cVar == null) {
                return null;
            }
            long j11 = (2097152 + j10) & (-2097152);
            int j12 = j(cVar);
            if (j12 >= 0 && f7201x.compareAndSet(this, j10, j12 | j11)) {
                cVar.r(A);
                return cVar;
            }
        }
    }

    private final void v(long j10, boolean z10) {
        if (z10 || G() || A(j10)) {
            return;
        }
        G();
    }

    private final h y(c cVar, h hVar, boolean z10) {
        if (cVar == null || cVar.f7215r == d.TERMINATED) {
            return hVar;
        }
        if (hVar.f7231q.b() == 0 && cVar.f7215r == d.BLOCKING) {
            return hVar;
        }
        cVar.f7219v = true;
        return cVar.f7213p.a(hVar, z10);
    }

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

    public final h d(Runnable runnable, i iVar) {
        long a10 = l.f7239f.a();
        if (!(runnable instanceof h)) {
            return new k(runnable, a10, iVar);
        }
        h hVar = (h) runnable;
        hVar.f7230p = a10;
        hVar.f7231q = iVar;
        return hVar;
    }

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

    public final void g(Runnable runnable, i iVar, boolean z10) {
        tp.c.a();
        h d10 = d(runnable, iVar);
        boolean z11 = false;
        boolean z12 = d10.f7231q.b() == 1;
        long addAndGet = z12 ? f7202y.addAndGet(this, 2097152L) : 0L;
        c f10 = f();
        h y10 = y(f10, d10, z10);
        if (y10 != null && !b(y10)) {
            throw new RejectedExecutionException(this.f7207s + " was terminated");
        }
        if (z10 && f10 != null) {
            z11 = true;
        }
        if (z12) {
            v(addAndGet, z11);
        } else {
            if (z11) {
                return;
            }
            w();
        }
    }

    public final boolean isTerminated() {
        return f7203z.get(this) != 0;
    }

    public final boolean p(c cVar) {
        long j10;
        int h10;
        if (cVar.i() != A) {
            return false;
        }
        AtomicLongFieldUpdater atomicLongFieldUpdater = f7201x;
        do {
            j10 = atomicLongFieldUpdater.get(this);
            h10 = cVar.h();
            cVar.r(this.f7210v.b((int) (2097151 & j10)));
        } while (!f7201x.compareAndSet(this, j10, ((2097152 + j10) & (-2097152)) | h10));
        return true;
    }

    public final void r(c cVar, int i10, int i11) {
        AtomicLongFieldUpdater atomicLongFieldUpdater = f7201x;
        while (true) {
            long j10 = atomicLongFieldUpdater.get(this);
            int i12 = (int) (2097151 & j10);
            long j11 = (2097152 + j10) & (-2097152);
            if (i12 == i10) {
                i12 = i11 == 0 ? j(cVar) : i11;
            }
            if (i12 >= 0 && f7201x.compareAndSet(this, j10, j11 | i12)) {
                return;
            }
        }
    }

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

    public final void t(long j10) {
        int i10;
        h hVar;
        if (f7203z.compareAndSet(this, 0, 1)) {
            c f10 = f();
            synchronized (this.f7210v) {
                i10 = (int) (f7202y.get(this) & 2097151);
            }
            if (1 <= i10) {
                int i11 = 1;
                while (true) {
                    Object b10 = this.f7210v.b(i11);
                    t.d(b10);
                    c cVar = (c) b10;
                    if (cVar != f10) {
                        while (cVar.isAlive()) {
                            LockSupport.unpark(cVar);
                            cVar.join(j10);
                        }
                        cVar.f7213p.f(this.f7209u);
                    }
                    if (i11 == i10) {
                        break;
                    } else {
                        i11++;
                    }
                }
            }
            this.f7209u.b();
            this.f7208t.b();
            while (true) {
                if (f10 != null) {
                    hVar = f10.g(true);
                    if (hVar != null) {
                        continue;
                        s(hVar);
                    }
                }
                hVar = (h) this.f7208t.d();
                if (hVar == null && (hVar = (h) this.f7209u.d()) == null) {
                    break;
                }
                s(hVar);
            }
            if (f10 != null) {
                f10.u(d.TERMINATED);
            }
            f7201x.set(this, 0L);
            f7202y.set(this, 0L);
        }
    }

    public String toString() {
        ArrayList arrayList = new ArrayList();
        int a10 = this.f7210v.a();
        int i10 = 0;
        int i11 = 0;
        int i12 = 0;
        int i13 = 0;
        int i14 = 0;
        for (int i15 = 1; i15 < a10; i15++) {
            c cVar = (c) this.f7210v.b(i15);
            if (cVar != null) {
                int e10 = cVar.f7213p.e();
                int i16 = b.f7211a[cVar.f7215r.ordinal()];
                if (i16 == 1) {
                    i12++;
                } else if (i16 == 2) {
                    i11++;
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(e10);
                    sb2.append('b');
                    arrayList.add(sb2.toString());
                } else if (i16 == 3) {
                    i10++;
                    StringBuilder sb3 = new StringBuilder();
                    sb3.append(e10);
                    sb3.append('c');
                    arrayList.add(sb3.toString());
                } else if (i16 == 4) {
                    i13++;
                    if (e10 > 0) {
                        StringBuilder sb4 = new StringBuilder();
                        sb4.append(e10);
                        sb4.append('d');
                        arrayList.add(sb4.toString());
                    }
                } else if (i16 == 5) {
                    i14++;
                }
            }
        }
        long j10 = f7202y.get(this);
        return this.f7207s + '@' + q0.b(this) + "[Pool Size {core = " + this.f7204p + ", max = " + this.f7205q + "}, Worker States {CPU = " + i10 + ", blocking = " + i11 + ", parked = " + i12 + ", dormant = " + i13 + ", terminated = " + i14 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.f7208t.c() + ", global blocking queue size = " + this.f7209u.c() + ", Control State {created workers= " + ((int) (2097151 & j10)) + ", blocking tasks = " + ((int) ((4398044413952L & j10) >> 21)) + ", CPUs acquired = " + (this.f7204p - ((int) ((9223367638808264704L & j10) >> 42))) + "}]";
    }

    public final void w() {
        if (G() || C(this, 0L, 1, null)) {
            return;
        }
        G();
    }

    /* loaded from: classes3.dex */
    public final class c extends Thread {

        /* renamed from: x, reason: collision with root package name */
        private static final AtomicIntegerFieldUpdater f7212x = AtomicIntegerFieldUpdater.newUpdater(c.class, "workerCtl");
        private volatile int indexInArray;
        private volatile Object nextParkedWorker;

        /* renamed from: p, reason: collision with root package name */
        public final n f7213p;

        /* renamed from: q, reason: collision with root package name */
        private final o0 f7214q;

        /* renamed from: r, reason: collision with root package name */
        public d f7215r;

        /* renamed from: s, reason: collision with root package name */
        private long f7216s;

        /* renamed from: t, reason: collision with root package name */
        private long f7217t;

        /* renamed from: u, reason: collision with root package name */
        private int f7218u;

        /* renamed from: v, reason: collision with root package name */
        public boolean f7219v;
        private volatile int workerCtl;

        private c() {
            setDaemon(true);
            this.f7213p = new n();
            this.f7214q = new o0();
            this.f7215r = d.DORMANT;
            this.nextParkedWorker = a.A;
            this.f7218u = hn.c.f21873p.c();
        }

        private final void b(int i10) {
            if (i10 == 0) {
                return;
            }
            a.f7202y.addAndGet(a.this, -2097152L);
            if (this.f7215r != d.TERMINATED) {
                this.f7215r = d.DORMANT;
            }
        }

        private final void c(int i10) {
            if (i10 != 0 && u(d.BLOCKING)) {
                a.this.w();
            }
        }

        private final void d(h hVar) {
            int b10 = hVar.f7231q.b();
            k(b10);
            c(b10);
            a.this.s(hVar);
            b(b10);
        }

        private final h e(boolean z10) {
            h o10;
            h o11;
            if (z10) {
                boolean z11 = m(a.this.f7204p * 2) == 0;
                if (z11 && (o11 = o()) != null) {
                    return o11;
                }
                h g10 = this.f7213p.g();
                if (g10 != null) {
                    return g10;
                }
                if (!z11 && (o10 = o()) != null) {
                    return o10;
                }
            } else {
                h o12 = o();
                if (o12 != null) {
                    return o12;
                }
            }
            return v(3);
        }

        private final h f() {
            h h10 = this.f7213p.h();
            if (h10 != null) {
                return h10;
            }
            h hVar = (h) a.this.f7209u.d();
            return hVar == null ? v(1) : hVar;
        }

        public static final AtomicIntegerFieldUpdater j() {
            return f7212x;
        }

        private final void k(int i10) {
            this.f7216s = 0L;
            if (this.f7215r == d.PARKING) {
                this.f7215r = d.BLOCKING;
            }
        }

        private final boolean l() {
            return this.nextParkedWorker != a.A;
        }

        private final void n() {
            if (this.f7216s == 0) {
                this.f7216s = System.nanoTime() + a.this.f7206r;
            }
            LockSupport.parkNanos(a.this.f7206r);
            if (System.nanoTime() - this.f7216s >= 0) {
                this.f7216s = 0L;
                w();
            }
        }

        private final h o() {
            if (m(2) == 0) {
                h hVar = (h) a.this.f7208t.d();
                return hVar != null ? hVar : (h) a.this.f7209u.d();
            }
            h hVar2 = (h) a.this.f7209u.d();
            return hVar2 != null ? hVar2 : (h) a.this.f7208t.d();
        }

        private final void p() {
            loop0: while (true) {
                boolean z10 = false;
                while (!a.this.isTerminated() && this.f7215r != d.TERMINATED) {
                    h g10 = g(this.f7219v);
                    if (g10 != null) {
                        this.f7217t = 0L;
                        d(g10);
                    } else {
                        this.f7219v = false;
                        if (this.f7217t == 0) {
                            t();
                        } else if (z10) {
                            u(d.PARKING);
                            Thread.interrupted();
                            LockSupport.parkNanos(this.f7217t);
                            this.f7217t = 0L;
                        } else {
                            z10 = true;
                        }
                    }
                }
            }
            u(d.TERMINATED);
        }

        private final boolean s() {
            boolean z10;
            if (this.f7215r == d.CPU_ACQUIRED) {
                return true;
            }
            a aVar = a.this;
            AtomicLongFieldUpdater atomicLongFieldUpdater = a.f7202y;
            while (true) {
                long j10 = atomicLongFieldUpdater.get(aVar);
                if (((int) ((9223367638808264704L & j10) >> 42)) == 0) {
                    z10 = false;
                    break;
                }
                if (a.f7202y.compareAndSet(aVar, j10, j10 - 4398046511104L)) {
                    z10 = true;
                    break;
                }
            }
            if (!z10) {
                return false;
            }
            this.f7215r = d.CPU_ACQUIRED;
            return true;
        }

        private final void t() {
            if (!l()) {
                a.this.p(this);
                return;
            }
            f7212x.set(this, -1);
            while (l() && f7212x.get(this) == -1 && !a.this.isTerminated() && this.f7215r != d.TERMINATED) {
                u(d.PARKING);
                Thread.interrupted();
                n();
            }
        }

        private final h v(int i10) {
            int i11 = (int) (a.f7202y.get(a.this) & 2097151);
            if (i11 < 2) {
                return null;
            }
            int m10 = m(i11);
            a aVar = a.this;
            long j10 = Long.MAX_VALUE;
            for (int i12 = 0; i12 < i11; i12++) {
                m10++;
                if (m10 > i11) {
                    m10 = 1;
                }
                c cVar = (c) aVar.f7210v.b(m10);
                if (cVar != null && cVar != this) {
                    long n10 = cVar.f7213p.n(i10, this.f7214q);
                    if (n10 == -1) {
                        o0 o0Var = this.f7214q;
                        h hVar = (h) o0Var.f26199p;
                        o0Var.f26199p = null;
                        return hVar;
                    }
                    if (n10 > 0) {
                        j10 = Math.min(j10, n10);
                    }
                }
            }
            if (j10 == Long.MAX_VALUE) {
                j10 = 0;
            }
            this.f7217t = j10;
            return null;
        }

        private final void w() {
            a aVar = a.this;
            synchronized (aVar.f7210v) {
                if (aVar.isTerminated()) {
                    return;
                }
                if (((int) (a.f7202y.get(aVar) & 2097151)) <= aVar.f7204p) {
                    return;
                }
                if (f7212x.compareAndSet(this, -1, 1)) {
                    int i10 = this.indexInArray;
                    q(0);
                    aVar.r(this, i10, 0);
                    int andDecrement = (int) (a.f7202y.getAndDecrement(aVar) & 2097151);
                    if (andDecrement != i10) {
                        Object b10 = aVar.f7210v.b(andDecrement);
                        t.d(b10);
                        c cVar = (c) b10;
                        aVar.f7210v.c(i10, cVar);
                        cVar.q(i10);
                        aVar.r(cVar, andDecrement, i10);
                    }
                    aVar.f7210v.c(andDecrement, null);
                    n0 n0Var = n0.f34904a;
                    this.f7215r = d.TERMINATED;
                }
            }
        }

        public final h g(boolean z10) {
            return s() ? e(z10) : f();
        }

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

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

        public final int m(int i10) {
            int i11 = this.f7218u;
            int i12 = i11 ^ (i11 << 13);
            int i13 = i12 ^ (i12 >> 17);
            int i14 = i13 ^ (i13 << 5);
            this.f7218u = i14;
            int i15 = i10 - 1;
            return (i15 & i10) == 0 ? i15 & i14 : (Integer.MAX_VALUE & i14) % i10;
        }

        public final void q(int i10) {
            StringBuilder sb2 = new StringBuilder();
            sb2.append(a.this.f7207s);
            sb2.append("-worker-");
            sb2.append(i10 == 0 ? "TERMINATED" : String.valueOf(i10));
            setName(sb2.toString());
            this.indexInArray = i10;
        }

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

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

        public final boolean u(d dVar) {
            d dVar2 = this.f7215r;
            boolean z10 = dVar2 == d.CPU_ACQUIRED;
            if (z10) {
                a.f7202y.addAndGet(a.this, 4398046511104L);
            }
            if (dVar2 != dVar) {
                this.f7215r = dVar;
            }
            return z10;
        }

        public c(a aVar, int i10) {
            this();
            q(i10);
        }
    }
}
