package s.coroutines.scheduling;

import io.netty.handler.codec.rtsp.RtspHeaders;
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;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import kotlin.random.Random;
import s.coroutines.e0;
import s.coroutines.internal.ResizableAtomicArray;
import s.coroutines.internal.Symbol;

/* JADX INFO: renamed from: s.a.c2.a, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000b\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\t\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0006\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b-\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\u0018\u0002\b\u0000\u0018\u0000 X2\u00020\\2\u00020]:\u0003XYZB+\u0012\u0006\u0010\u0002\u001a\u00020\u0001\u0012\u0006\u0010\u0003\u001a\u00020\u0001\u0012\b\b\u0002\u0010\u0005\u001a\u00020\u0004\u0012\b\b\u0002\u0010\u0007\u001a\u00020\u0006¢\u0006\u0004\b\b\u0010\tJ\u0017\u0010\r\u001a\u00020\f2\u0006\u0010\u000b\u001a\u00020\nH\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u0018\u0010\u0010\u001a\u00020\u00012\u0006\u0010\u000f\u001a\u00020\u0004H\u0086\b¢\u0006\u0004\b\u0010\u0010\u0011J\u0018\u0010\u0012\u001a\u00020\u00012\u0006\u0010\u000f\u001a\u00020\u0004H\u0082\b¢\u0006\u0004\b\u0012\u0010\u0011J\u000f\u0010\u0014\u001a\u00020\u0013H\u0016¢\u0006\u0004\b\u0014\u0010\u0015J\u000f\u0010\u0016\u001a\u00020\u0001H\u0002¢\u0006\u0004\b\u0016\u0010\u0017J!\u0010\u001d\u001a\u00020\n2\n\u0010\u001a\u001a\u00060\u0018j\u0002`\u00192\u0006\u0010\u001c\u001a\u00020\u001b¢\u0006\u0004\b\u001d\u0010\u001eJ\u0018\u0010\u001f\u001a\u00020\u00012\u0006\u0010\u000f\u001a\u00020\u0004H\u0082\b¢\u0006\u0004\b\u001f\u0010\u0011J\u0015\u0010!\u001a\b\u0018\u00010 R\u00020\u0000H\u0002¢\u0006\u0004\b!\u0010\"J\u0010\u0010#\u001a\u00020\u0013H\u0082\b¢\u0006\u0004\b#\u0010\u0015J\u0010\u0010$\u001a\u00020\u0001H\u0082\b¢\u0006\u0004\b$\u0010\u0017J-\u0010&\u001a\u00020\u00132\n\u0010\u001a\u001a\u00060\u0018j\u0002`\u00192\b\b\u0002\u0010\u001c\u001a\u00020\u001b2\b\b\u0002\u0010%\u001a\u00020\f¢\u0006\u0004\b&\u0010'J\u001b\u0010)\u001a\u00020\u00132\n\u0010(\u001a\u00060\u0018j\u0002`\u0019H\u0016¢\u0006\u0004\b)\u0010*J\u0010\u0010+\u001a\u00020\u0004H\u0082\b¢\u0006\u0004\b+\u0010,J\u0010\u0010-\u001a\u00020\u0001H\u0082\b¢\u0006\u0004\b-\u0010\u0017J\u001b\u0010/\u001a\u00020\u00012\n\u0010.\u001a\u00060 R\u00020\u0000H\u0002¢\u0006\u0004\b/\u00100J\u0015\u00101\u001a\b\u0018\u00010 R\u00020\u0000H\u0002¢\u0006\u0004\b1\u0010\"J\u0019\u00102\u001a\u00020\f2\n\u0010.\u001a\u00060 R\u00020\u0000¢\u0006\u0004\b2\u00103J)\u00106\u001a\u00020\u00132\n\u0010.\u001a\u00060 R\u00020\u00002\u0006\u00104\u001a\u00020\u00012\u0006\u00105\u001a\u00020\u0001¢\u0006\u0004\b6\u00107J\u0010\u00108\u001a\u00020\u0004H\u0082\b¢\u0006\u0004\b8\u0010,J\u0015\u00109\u001a\u00020\u00132\u0006\u0010\u000b\u001a\u00020\n¢\u0006\u0004\b9\u0010:J\u0015\u0010<\u001a\u00020\u00132\u0006\u0010;\u001a\u00020\u0004¢\u0006\u0004\b<\u0010=J\u0017\u0010?\u001a\u00020\u00132\u0006\u0010>\u001a\u00020\fH\u0002¢\u0006\u0004\b?\u0010@J\r\u0010A\u001a\u00020\u0013¢\u0006\u0004\bA\u0010\u0015J\u000f\u0010B\u001a\u00020\u0006H\u0016¢\u0006\u0004\bB\u0010CJ\u0010\u0010D\u001a\u00020\fH\u0082\b¢\u0006\u0004\bD\u0010EJ\u0019\u0010F\u001a\u00020\f2\b\b\u0002\u0010\u000f\u001a\u00020\u0004H\u0002¢\u0006\u0004\bF\u0010GJ\u000f\u0010H\u001a\u00020\fH\u0002¢\u0006\u0004\bH\u0010EJ+\u0010I\u001a\u0004\u0018\u00010\n*\b\u0018\u00010 R\u00020\u00002\u0006\u0010\u000b\u001a\u00020\n2\u0006\u0010%\u001a\u00020\fH\u0002¢\u0006\u0004\bI\u0010JR\u0015\u0010\u0010\u001a\u00020\u00018Â\u0002X\u0082\u0004¢\u0006\u0006\u001a\u0004\bK\u0010\u0017R\u0014\u0010\u0002\u001a\u00020\u00018\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b\u0002\u0010LR\u0015\u0010\u001f\u001a\u00020\u00018Â\u0002X\u0082\u0004¢\u0006\u0006\u001a\u0004\bM\u0010\u0017R\u0014\u0010O\u001a\u00020N8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bO\u0010PR\u0014\u0010Q\u001a\u00020N8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bQ\u0010PR\u0014\u0010\u0005\u001a\u00020\u00048\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b\u0005\u0010RR\u0011\u0010S\u001a\u00020\f8F¢\u0006\u0006\u001a\u0004\bS\u0010ER\u0014\u0010\u0003\u001a\u00020\u00018\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010LR\u0014\u0010\u0007\u001a\u00020\u00068\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b\u0007\u0010TR\u001e\u0010V\u001a\f\u0012\b\u0012\u00060 R\u00020\u00000U8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bV\u0010W¨\u0006["}, d2 = {"Lkotlinx/coroutines/scheduling/CoroutineScheduler;", "", "corePoolSize", "maxPoolSize", "", "idleWorkerKeepAliveNs", "", "schedulerName", "<init>", "(IIJLjava/lang/String;)V", "Lkotlinx/coroutines/scheduling/Task;", "task", "", "addToGlobalQueue", "(Lkotlinx/coroutines/scheduling/Task;)Z", "state", "availableCpuPermits", "(J)I", "blockingTasks", "", "close", "()V", "createNewWorker", "()I", "Ljava/lang/Runnable;", "Lkotlinx/coroutines/Runnable;", "block", "Lkotlinx/coroutines/scheduling/TaskContext;", "taskContext", "createTask", "(Ljava/lang/Runnable;Lkotlinx/coroutines/scheduling/TaskContext;)Lkotlinx/coroutines/scheduling/Task;", "createdWorkers", "Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;", "currentWorker", "()Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;", "decrementBlockingTasks", "decrementCreatedWorkers", "tailDispatch", "dispatch", "(Ljava/lang/Runnable;Lkotlinx/coroutines/scheduling/TaskContext;Z)V", "command", "execute", "(Ljava/lang/Runnable;)V", "incrementBlockingTasks", "()J", "incrementCreatedWorkers", "worker", "parkedWorkersStackNextIndex", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;)I", "parkedWorkersStackPop", "parkedWorkersStackPush", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;)Z", "oldIndex", "newIndex", "parkedWorkersStackTopUpdate", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;II)V", "releaseCpuPermit", "runSafely", "(Lkotlinx/coroutines/scheduling/Task;)V", RtspHeaders.Values.TIMEOUT, "shutdown", "(J)V", "skipUnpark", "signalBlockingWork", "(Z)V", "signalCpuWork", "toString", "()Ljava/lang/String;", "tryAcquireCpuPermit", "()Z", "tryCreateWorker", "(J)Z", "tryUnpark", "submitToLocalQueue", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;Lkotlinx/coroutines/scheduling/Task;Z)Lkotlinx/coroutines/scheduling/Task;", "getAvailableCpuPermits", "I", "getCreatedWorkers", "Lkotlinx/coroutines/scheduling/GlobalQueue;", "globalBlockingQueue", "Lkotlinx/coroutines/scheduling/GlobalQueue;", "globalCpuQueue", "J", "isTerminated", "Ljava/lang/String;", "Lkotlinx/coroutines/internal/ResizableAtomicArray;", "workers", "Lkotlinx/coroutines/internal/ResizableAtomicArray;", "Companion", "Worker", "WorkerState", "kotlinx-coroutines-core", "Ljava/util/concurrent/Executor;", "Ljava/io/Closeable;"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class CoroutineScheduler implements Executor, Closeable {
    private volatile /* synthetic */ int _isTerminated;
    public volatile /* synthetic */ long controlState;

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

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

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

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final GlobalQueue f9382t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final GlobalQueue f9383u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final ResizableAtomicArray<a> f9384v;

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

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

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

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

    /* JADX INFO: renamed from: s.a.c2.a$a */
    @Metadata(d1 = {"\u0000P\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\b\u0005\n\u0002\u0010\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0013\n\u0002\u0018\u0002\n\u0002\b\u000b\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0010\u0000\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\b\u0080\u0004\u0018\u00002\u00020GB\u0011\b\u0016\u0012\u0006\u0010\u0002\u001a\u00020\u0001¢\u0006\u0004\b\u0003\u0010\u0004B\t\b\u0002¢\u0006\u0004\b\u0003\u0010\u0005J\u0017\u0010\b\u001a\u00020\u00072\u0006\u0010\u0006\u001a\u00020\u0001H\u0002¢\u0006\u0004\b\b\u0010\tJ\u0017\u0010\n\u001a\u00020\u00072\u0006\u0010\u0006\u001a\u00020\u0001H\u0002¢\u0006\u0004\b\n\u0010\tJ\u0017\u0010\r\u001a\u00020\u00072\u0006\u0010\f\u001a\u00020\u000bH\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u0019\u0010\u0011\u001a\u0004\u0018\u00010\u000b2\u0006\u0010\u0010\u001a\u00020\u000fH\u0002¢\u0006\u0004\b\u0011\u0010\u0012J\u0017\u0010\u0013\u001a\u0004\u0018\u00010\u000b2\u0006\u0010\u0010\u001a\u00020\u000f¢\u0006\u0004\b\u0013\u0010\u0012J\u0017\u0010\u0015\u001a\u00020\u00072\u0006\u0010\u0014\u001a\u00020\u0001H\u0002¢\u0006\u0004\b\u0015\u0010\tJ\u000f\u0010\u0016\u001a\u00020\u000fH\u0002¢\u0006\u0004\b\u0016\u0010\u0017J\u0015\u0010\u0019\u001a\u00020\u00012\u0006\u0010\u0018\u001a\u00020\u0001¢\u0006\u0004\b\u0019\u0010\u001aJ\u000f\u0010\u001b\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\u001b\u0010\u001cJ\u0011\u0010\u001d\u001a\u0004\u0018\u00010\u000bH\u0002¢\u0006\u0004\b\u001d\u0010\u001eJ\u000f\u0010\u001f\u001a\u00020\u0007H\u0016¢\u0006\u0004\b\u001f\u0010\u001cJ\u000f\u0010 \u001a\u00020\u0007H\u0002¢\u0006\u0004\b \u0010\u001cJ\u000f\u0010!\u001a\u00020\u000fH\u0002¢\u0006\u0004\b!\u0010\u0017J\u000f\u0010\"\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\"\u0010\u001cJ\u0015\u0010%\u001a\u00020\u000f2\u0006\u0010$\u001a\u00020#¢\u0006\u0004\b%\u0010&J\u0019\u0010(\u001a\u0004\u0018\u00010\u000b2\u0006\u0010'\u001a\u00020\u000fH\u0002¢\u0006\u0004\b(\u0010\u0012J\u000f\u0010)\u001a\u00020\u0007H\u0002¢\u0006\u0004\b)\u0010\u001cR*\u0010*\u001a\u00020\u00012\u0006\u0010\u0002\u001a\u00020\u00018\u0006@FX\u0086\u000e¢\u0006\u0012\n\u0004\b*\u0010+\u001a\u0004\b,\u0010-\"\u0004\b.\u0010\tR\u0014\u00100\u001a\u00020/8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b0\u00101R\u0016\u00102\u001a\u00020\u000f8\u0006@\u0006X\u0087\u000e¢\u0006\u0006\n\u0004\b2\u00103R\u0016\u00105\u001a\u0002048\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b5\u00106R$\u00108\u001a\u0004\u0018\u0001078\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b8\u00109\u001a\u0004\b:\u0010;\"\u0004\b<\u0010=R\u0016\u0010>\u001a\u00020\u00018\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b>\u0010+R\u0012\u0010B\u001a\u00020?8Æ\u0002¢\u0006\u0006\u001a\u0004\b@\u0010AR\u0016\u0010C\u001a\u00020#8\u0006@\u0006X\u0087\u000e¢\u0006\u0006\n\u0004\bC\u0010DR\u0016\u0010E\u001a\u0002048\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\bE\u00106¨\u0006F"}, d2 = {"Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;", "", "index", "<init>", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler;I)V", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler;)V", "taskMode", "", "afterTask", "(I)V", "beforeTask", "Lkotlinx/coroutines/scheduling/Task;", "task", "executeTask", "(Lkotlinx/coroutines/scheduling/Task;)V", "", "scanLocalQueue", "findAnyTask", "(Z)Lkotlinx/coroutines/scheduling/Task;", "findTask", RtspHeaders.Values.MODE, "idleReset", "inStack", "()Z", "upperBound", "nextInt", "(I)I", "park", "()V", "pollGlobalQueues", "()Lkotlinx/coroutines/scheduling/Task;", "run", "runWorker", "tryAcquireCpuPermit", "tryPark", "Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;", "newState", "tryReleaseCpu", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;)Z", "blockingOnly", "trySteal", "tryTerminateWorker", "indexInArray", "I", "getIndexInArray", "()I", "setIndexInArray", "Lkotlinx/coroutines/scheduling/WorkQueue;", "localQueue", "Lkotlinx/coroutines/scheduling/WorkQueue;", "mayHaveLocalTasks", "Z", "", "minDelayUntilStealableTaskNs", "J", "", "nextParkedWorker", "Ljava/lang/Object;", "getNextParkedWorker", "()Ljava/lang/Object;", "setNextParkedWorker", "(Ljava/lang/Object;)V", "rngState", "Lkotlinx/coroutines/scheduling/CoroutineScheduler;", "getScheduler", "()Lkotlinx/coroutines/scheduling/CoroutineScheduler;", "scheduler", "state", "Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;", "terminationDeadline", "kotlinx-coroutines-core", "Ljava/lang/Thread;"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class a extends Thread {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public static final /* synthetic */ AtomicIntegerFieldUpdater f9385f = AtomicIntegerFieldUpdater.newUpdater(a.class, "workerCtl");
        private volatile int indexInArray;

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final WorkQueue f9386m;

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        public b f9387n;
        private volatile Object nextParkedWorker;

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

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

        /* JADX INFO: renamed from: q, reason: collision with root package name */
        public int f9390q;

        /* JADX INFO: renamed from: r, reason: collision with root package name */
        public boolean f9391r;
        public volatile /* synthetic */ int workerCtl;

        public a(int i2) {
            setDaemon(true);
            this.f9386m = new WorkQueue();
            this.f9387n = b.DORMANT;
            this.workerCtl = 0;
            this.nextParkedWorker = CoroutineScheduler.f9377o;
            Random.a aVar = Random.f6186f;
            this.f9390q = Random.f6187m.a();
            f(i2);
        }

        public final Task a(boolean z2) {
            boolean z3;
            Task taskD;
            Task taskE;
            Task taskE2;
            boolean z4;
            b bVar = b.CPU_ACQUIRED;
            if (this.f9387n == bVar) {
                z3 = true;
            } else {
                CoroutineScheduler coroutineScheduler = CoroutineScheduler.this;
                while (true) {
                    long j2 = coroutineScheduler.controlState;
                    if (((int) ((9223367638808264704L & j2) >> 42)) == 0) {
                        z4 = false;
                        break;
                    }
                    if (CoroutineScheduler.f9375m.compareAndSet(coroutineScheduler, j2, j2 - 4398046511104L)) {
                        z4 = true;
                        break;
                    }
                }
                if (z4) {
                    this.f9387n = bVar;
                    z3 = true;
                } else {
                    z3 = false;
                }
            }
            if (!z3) {
                if (!z2 || (taskD = this.f9386m.e()) == null) {
                    taskD = CoroutineScheduler.this.f9383u.d();
                }
                return taskD == null ? i(true) : taskD;
            }
            if (z2) {
                boolean z5 = d(CoroutineScheduler.this.f9378p * 2) == 0;
                if (z5 && (taskE2 = e()) != null) {
                    return taskE2;
                }
                Task taskE3 = this.f9386m.e();
                if (taskE3 != null) {
                    return taskE3;
                }
                if (!z5 && (taskE = e()) != null) {
                    return taskE;
                }
            } else {
                Task taskE4 = e();
                if (taskE4 != null) {
                    return taskE4;
                }
            }
            return i(false);
        }

        /* JADX INFO: renamed from: b, reason: from getter */
        public final int getIndexInArray() {
            return this.indexInArray;
        }

        /* JADX INFO: renamed from: c, reason: from getter */
        public final Object getNextParkedWorker() {
            return this.nextParkedWorker;
        }

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

        public final Task e() {
            GlobalQueue globalQueue;
            if (d(2) == 0) {
                Task taskD = CoroutineScheduler.this.f9382t.d();
                if (taskD != null) {
                    return taskD;
                }
                globalQueue = CoroutineScheduler.this.f9383u;
            } else {
                Task taskD2 = CoroutineScheduler.this.f9383u.d();
                if (taskD2 != null) {
                    return taskD2;
                }
                globalQueue = CoroutineScheduler.this.f9382t;
            }
            return globalQueue.d();
        }

        public final void f(int i2) {
            StringBuilder sb = new StringBuilder();
            sb.append(CoroutineScheduler.this.f9381s);
            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.f9387n;
            boolean z2 = bVar2 == b.CPU_ACQUIRED;
            if (z2) {
                CoroutineScheduler.f9375m.addAndGet(CoroutineScheduler.this, 4398046511104L);
            }
            if (bVar2 != bVar) {
                this.f9387n = bVar;
            }
            return z2;
        }

        public final Task i(boolean z2) {
            long jH;
            int i2 = (int) (CoroutineScheduler.this.controlState & 2097151);
            if (i2 < 2) {
                return null;
            }
            int iD = d(i2);
            CoroutineScheduler coroutineScheduler = CoroutineScheduler.this;
            long jMin = Long.MAX_VALUE;
            for (int i3 = 0; i3 < i2; i3++) {
                iD++;
                if (iD > i2) {
                    iD = 1;
                }
                a aVarB = coroutineScheduler.f9384v.b(iD);
                if (aVarB != null && aVarB != this) {
                    WorkQueue workQueue = this.f9386m;
                    WorkQueue workQueue2 = aVarB.f9386m;
                    if (z2) {
                        jH = workQueue.g(workQueue2);
                    } else {
                        Objects.requireNonNull(workQueue);
                        Task taskF = workQueue2.f();
                        if (taskF != null) {
                            workQueue.a(taskF, false);
                            jH = -1;
                        } else {
                            jH = workQueue.h(workQueue2, false);
                        }
                    }
                    if (jH == -1) {
                        return this.f9386m.e();
                    }
                    if (jH > 0) {
                        jMin = Math.min(jMin, jH);
                    }
                }
            }
            if (jMin == Long.MAX_VALUE) {
                jMin = 0;
            }
            this.f9389p = jMin;
            return null;
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            b bVar = b.PARKING;
            b bVar2 = b.TERMINATED;
            loop0: while (true) {
                boolean z2 = false;
                while (!CoroutineScheduler.this.isTerminated() && this.f9387n != bVar2) {
                    Task taskA = a(this.f9391r);
                    if (taskA != null) {
                        this.f9389p = 0L;
                        b bVar3 = b.BLOCKING;
                        int a = taskA.f9408m.getA();
                        this.f9388o = 0L;
                        if (this.f9387n == bVar) {
                            this.f9387n = bVar3;
                        }
                        if (a != 0 && h(bVar3)) {
                            CoroutineScheduler.this.l();
                        }
                        CoroutineScheduler.this.j(taskA);
                        if (a != 0) {
                            CoroutineScheduler.f9375m.addAndGet(CoroutineScheduler.this, -2097152L);
                            if (this.f9387n != bVar2) {
                                this.f9387n = b.DORMANT;
                            }
                        }
                    } else {
                        this.f9391r = false;
                        if (this.f9389p == 0) {
                            if (this.nextParkedWorker != CoroutineScheduler.f9377o) {
                                this.workerCtl = -1;
                                while (true) {
                                    if (!(this.nextParkedWorker != CoroutineScheduler.f9377o) || this.workerCtl != -1 || CoroutineScheduler.this.isTerminated() || this.f9387n == bVar2) {
                                        break;
                                    }
                                    h(bVar);
                                    Thread.interrupted();
                                    if (this.f9388o == 0) {
                                        this.f9388o = System.nanoTime() + CoroutineScheduler.this.f9380r;
                                    }
                                    LockSupport.parkNanos(CoroutineScheduler.this.f9380r);
                                    if (System.nanoTime() - this.f9388o >= 0) {
                                        this.f9388o = 0L;
                                        CoroutineScheduler coroutineScheduler = CoroutineScheduler.this;
                                        synchronized (coroutineScheduler.f9384v) {
                                            if (!coroutineScheduler.isTerminated() && ((int) (coroutineScheduler.controlState & 2097151)) > coroutineScheduler.f9378p && f9385f.compareAndSet(this, -1, 1)) {
                                                int i2 = this.indexInArray;
                                                f(0);
                                                coroutineScheduler.h(this, i2, 0);
                                                int andDecrement = (int) (CoroutineScheduler.f9375m.getAndDecrement(coroutineScheduler) & 2097151);
                                                if (andDecrement != i2) {
                                                    a aVarB = coroutineScheduler.f9384v.b(andDecrement);
                                                    o.c(aVarB);
                                                    a aVar = aVarB;
                                                    coroutineScheduler.f9384v.c(i2, aVar);
                                                    aVar.f(i2);
                                                    coroutineScheduler.h(aVar, andDecrement, i2);
                                                }
                                                coroutineScheduler.f9384v.c(andDecrement, null);
                                                this.f9387n = bVar2;
                                            }
                                        }
                                    }
                                }
                            } else {
                                CoroutineScheduler.this.g(this);
                            }
                        } else if (z2) {
                            h(bVar);
                            Thread.interrupted();
                            LockSupport.parkNanos(this.f9389p);
                            this.f9389p = 0L;
                        } else {
                            z2 = true;
                        }
                    }
                }
                break loop0;
            }
            h(bVar2);
        }
    }

    /* JADX INFO: renamed from: s.a.c2.a$b */
    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0010\n\u0002\b\u0007\b\u0086\u0001\u0018\u00002\b\u0012\u0004\u0012\u00020\u00000\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002j\u0002\b\u0003j\u0002\b\u0004j\u0002\b\u0005j\u0002\b\u0006j\u0002\b\u0007¨\u0006\b"}, d2 = {"Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;", "", "(Ljava/lang/String;I)V", "CPU_ACQUIRED", "BLOCKING", "PARKING", "DORMANT", "TERMINATED", "kotlinx-coroutines-core"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public enum b {
        CPU_ACQUIRED,
        BLOCKING,
        PARKING,
        DORMANT,
        TERMINATED
    }

    public CoroutineScheduler(int i2, int i3, long j2, String str) {
        this.f9378p = i2;
        this.f9379q = i3;
        this.f9380r = j2;
        this.f9381s = str;
        if (!(i2 >= 1)) {
            throw new IllegalArgumentException(g.a.a.a.a.n("Core pool size ", i2, " should be at least 1").toString());
        }
        if (!(i3 >= i2)) {
            throw new IllegalArgumentException(g.a.a.a.a.o("Max pool size ", i3, " should be greater than or equals to core pool size ", i2).toString());
        }
        if (!(i3 <= 2097150)) {
            throw new IllegalArgumentException(g.a.a.a.a.n("Max pool size ", i3, " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (!(j2 > 0)) {
            throw new IllegalArgumentException(g.a.a.a.a.r("Idle worker keep alive time ", j2, " must be positive").toString());
        }
        this.f9382t = new GlobalQueue();
        this.f9383u = new GlobalQueue();
        this.parkedWorkersStack = 0L;
        this.f9384v = new ResizableAtomicArray<>(i2 + 1);
        this.controlState = ((long) i2) << 42;
        this._isTerminated = 0;
    }

    public final int a() {
        int i2;
        synchronized (this.f9384v) {
            if (this._isTerminated != 0) {
                i2 = -1;
            } else {
                long j2 = this.controlState;
                int i3 = (int) (j2 & 2097151);
                int i4 = i3 - ((int) ((j2 & 4398044413952L) >> 21));
                if (i4 < 0) {
                    i4 = 0;
                }
                if (i4 >= this.f9378p) {
                    return 0;
                }
                if (i3 >= this.f9379q) {
                    return 0;
                }
                int i5 = ((int) (this.controlState & 2097151)) + 1;
                if (!(i5 > 0 && this.f9384v.b(i5) == null)) {
                    throw new IllegalArgumentException("Failed requirement.".toString());
                }
                a aVar = new a(i5);
                this.f9384v.c(i5, aVar);
                if (!(i5 == ((int) (2097151 & f9375m.incrementAndGet(this))))) {
                    throw new IllegalArgumentException("Failed requirement.".toString());
                }
                aVar.start();
                i2 = i4 + 1;
            }
            return i2;
        }
    }

    public final a c() {
        Thread threadCurrentThread = Thread.currentThread();
        a aVar = threadCurrentThread instanceof a ? (a) threadCurrentThread : null;
        if (aVar == null || !o.a(CoroutineScheduler.this, this)) {
            return null;
        }
        return aVar;
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x0074  */
    @Override // java.io.Closeable, java.lang.AutoCloseable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void close() throws java.lang.InterruptedException {
        /*
            r9 = this;
            java.util.concurrent.atomic.AtomicIntegerFieldUpdater r0 = s.coroutines.scheduling.CoroutineScheduler.f9376n
            r1 = 0
            r2 = 1
            boolean r0 = r0.compareAndSet(r9, r1, r2)
            if (r0 != 0) goto Lc
            goto L95
        Lc:
            s.a.c2.a$a r0 = r9.c()
            s.a.b2.q<s.a.c2.a$a> r3 = r9.f9384v
            monitor-enter(r3)
            long r4 = r9.controlState     // Catch: java.lang.Throwable -> L9a
            r6 = 2097151(0x1fffff, double:1.0361303E-317)
            long r4 = r4 & r6
            int r4 = (int) r4
            monitor-exit(r3)
            if (r2 > r4) goto L62
            r3 = r2
        L1e:
            s.a.b2.q<s.a.c2.a$a> r5 = r9.f9384v
            java.lang.Object r5 = r5.b(r3)
            kotlin.jvm.internal.o.c(r5)
            s.a.c2.a$a r5 = (s.coroutines.scheduling.CoroutineScheduler.a) r5
            if (r5 == r0) goto L5d
        L2b:
            boolean r6 = r5.isAlive()
            if (r6 == 0) goto L3a
            java.util.concurrent.locks.LockSupport.unpark(r5)
            r6 = 10000(0x2710, double:4.9407E-320)
            r5.join(r6)
            goto L2b
        L3a:
            s.a.c2.n r5 = r5.f9386m
            s.a.c2.d r6 = r9.f9383u
            java.util.Objects.requireNonNull(r5)
            java.util.concurrent.atomic.AtomicReferenceFieldUpdater r7 = s.coroutines.scheduling.WorkQueue.a
            r8 = 0
            java.lang.Object r7 = r7.getAndSet(r5, r8)
            s.a.c2.h r7 = (s.coroutines.scheduling.Task) r7
            if (r7 == 0) goto L4f
            r6.a(r7)
        L4f:
            s.a.c2.h r7 = r5.f()
            if (r7 != 0) goto L57
            r7 = r1
            goto L5b
        L57:
            r6.a(r7)
            r7 = r2
        L5b:
            if (r7 != 0) goto L4f
        L5d:
            if (r3 == r4) goto L62
            int r3 = r3 + 1
            goto L1e
        L62:
            s.a.c2.d r1 = r9.f9383u
            r1.b()
            s.a.c2.d r1 = r9.f9382t
            r1.b()
        L6c:
            if (r0 == 0) goto L74
            s.a.c2.h r1 = r0.a(r2)
            if (r1 != 0) goto L96
        L74:
            s.a.c2.d r1 = r9.f9382t
            java.lang.Object r1 = r1.d()
            s.a.c2.h r1 = (s.coroutines.scheduling.Task) r1
            if (r1 != 0) goto L96
            s.a.c2.d r1 = r9.f9383u
            java.lang.Object r1 = r1.d()
            s.a.c2.h r1 = (s.coroutines.scheduling.Task) r1
            if (r1 != 0) goto L96
            if (r0 == 0) goto L8f
            s.a.c2.a$b r1 = s.coroutines.scheduling.CoroutineScheduler.b.TERMINATED
            r0.h(r1)
        L8f:
            r0 = 0
            r9.parkedWorkersStack = r0
            r9.controlState = r0
        L95:
            return
        L96:
            r9.j(r1)
            goto L6c
        L9a:
            r0 = move-exception
            monitor-exit(r3)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: s.coroutines.scheduling.CoroutineScheduler.close():void");
    }

    public final void d(Runnable runnable, TaskContext taskContext, boolean z2) {
        Task taskImpl;
        Task taskA;
        long jA = l.f9413e.a();
        if (runnable instanceof Task) {
            taskImpl = (Task) runnable;
            taskImpl.f9407f = jA;
            taskImpl.f9408m = taskContext;
        } else {
            taskImpl = new TaskImpl(runnable, jA, taskContext);
        }
        a aVarC = c();
        if (aVarC == null || aVarC.f9387n == b.TERMINATED || (taskImpl.f9408m.getA() == 0 && aVarC.f9387n == b.BLOCKING)) {
            taskA = taskImpl;
        } else {
            aVarC.f9391r = true;
            taskA = aVarC.f9386m.a(taskImpl, z2);
        }
        if (taskA != null) {
            if (!(taskA.f9408m.getA() == 1 ? this.f9383u : this.f9382t).a(taskA)) {
                throw new RejectedExecutionException(g.a.a.a.a.A(new StringBuilder(), this.f9381s, " was terminated"));
            }
        }
        boolean z3 = z2 && aVarC != null;
        if (taskImpl.f9408m.getA() == 0) {
            if (z3) {
                return;
            }
            l();
        } else {
            long jAddAndGet = f9375m.addAndGet(this, 2097152L);
            if (z3 || o() || m(jAddAndGet)) {
                return;
            }
            o();
        }
    }

    public final int e(a aVar) {
        int indexInArray;
        do {
            Object nextParkedWorker = aVar.getNextParkedWorker();
            if (nextParkedWorker == f9377o) {
                return -1;
            }
            if (nextParkedWorker == null) {
                return 0;
            }
            aVar = (a) nextParkedWorker;
            indexInArray = aVar.getIndexInArray();
        } while (indexInArray == 0);
        return indexInArray;
    }

    @Override // java.util.concurrent.Executor
    public void execute(Runnable command) {
        d(command, l.f9414f, false);
    }

    public final boolean g(a aVar) {
        long j2;
        int indexInArray;
        if (aVar.getNextParkedWorker() != f9377o) {
            return false;
        }
        do {
            j2 = this.parkedWorkersStack;
            indexInArray = aVar.getIndexInArray();
            aVar.g(this.f9384v.b((int) (2097151 & j2)));
        } while (!f9374f.compareAndSet(this, j2, ((2097152 + j2) & (-2097152)) | ((long) indexInArray)));
        return true;
    }

    public final void h(a aVar, int i2, int i3) {
        while (true) {
            long j2 = this.parkedWorkersStack;
            int iE = (int) (2097151 & j2);
            long j3 = (2097152 + j2) & (-2097152);
            if (iE == i2) {
                iE = i3 == 0 ? e(aVar) : i3;
            }
            if (iE >= 0 && f9374f.compareAndSet(this, j2, j3 | ((long) iE))) {
                return;
            }
        }
    }

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

    public final void j(Task task) {
        try {
            task.run();
        } finally {
        }
    }

    public final void l() {
        if (o() || m(this.controlState)) {
            return;
        }
        o();
    }

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

    public final boolean o() {
        while (true) {
            long j2 = this.parkedWorkersStack;
            a aVarB = this.f9384v.b((int) (2097151 & j2));
            if (aVarB == null) {
                aVarB = null;
            } else {
                long j3 = (2097152 + j2) & (-2097152);
                int iE = e(aVarB);
                if (iE >= 0 && f9374f.compareAndSet(this, j2, ((long) iE) | j3)) {
                    aVarB.g(f9377o);
                }
            }
            if (aVarB == null) {
                return false;
            }
            if (a.f9385f.compareAndSet(aVarB, -1, 0)) {
                LockSupport.unpark(aVarB);
                return true;
            }
        }
    }

    public String toString() {
        StringBuilder sb;
        char c2;
        ArrayList arrayList = new ArrayList();
        int iA = this.f9384v.a();
        int i2 = 0;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        int i6 = 0;
        for (int i7 = 1; i7 < iA; i7++) {
            a aVarB = this.f9384v.b(i7);
            if (aVarB != null) {
                int iD = aVarB.f9386m.d();
                int iOrdinal = aVarB.f9387n.ordinal();
                if (iOrdinal == 0) {
                    i2++;
                    sb = new StringBuilder();
                    sb.append(iD);
                    c2 = 'c';
                } else if (iOrdinal == 1) {
                    i3++;
                    sb = new StringBuilder();
                    sb.append(iD);
                    c2 = 'b';
                } else if (iOrdinal == 2) {
                    i4++;
                } else if (iOrdinal == 3) {
                    i5++;
                    if (iD > 0) {
                        sb = new StringBuilder();
                        sb.append(iD);
                        c2 = 'd';
                    }
                } else if (iOrdinal == 4) {
                    i6++;
                }
                sb.append(c2);
                arrayList.add(sb.toString());
            }
        }
        long j2 = this.controlState;
        return this.f9381s + '@' + e0.b(this) + "[Pool Size {core = " + this.f9378p + ", max = " + this.f9379q + "}, Worker States {CPU = " + i2 + ", blocking = " + i3 + ", parked = " + i4 + ", dormant = " + i5 + ", terminated = " + i6 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.f9382t.c() + ", global blocking queue size = " + this.f9383u.c() + ", Control State {created workers= " + ((int) (2097151 & j2)) + ", blocking tasks = " + ((int) ((4398044413952L & j2) >> 21)) + ", CPUs acquired = " + (this.f9378p - ((int) ((9223367638808264704L & j2) >> 42))) + "}]";
    }
}
