package kotlinx.coroutines.scheduling;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000t\n\u0002\u0018\u0002\n\u0002\u0018\u0002\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\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u0002\n\u0002\b\u0013\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0018\u0002\n\u0002\b\u0010\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\b\u0000\u0018\u0000 V2\u00020\u00012\u00020\u0002:\u0003WXYB+\u0012\u0006\u0010\u0004\u001a\u00020\u0003\u0012\u0006\u0010\u0005\u001a\u00020\u0003\u0012\b\b\u0002\u0010\u0007\u001a\u00020\u0006\u0012\b\b\u0002\u0010\t\u001a\u00020\b¢\u0006\u0004\b\n\u0010\u000bJ\u0017\u0010\u000f\u001a\u00020\u000e2\u0006\u0010\r\u001a\u00020\fH\u0002¢\u0006\u0004\b\u000f\u0010\u0010J\u0015\u0010\u0012\u001a\b\u0018\u00010\u0011R\u00020\u0000H\u0002¢\u0006\u0004\b\u0012\u0010\u0013J\u001b\u0010\u0015\u001a\u00020\u00032\n\u0010\u0014\u001a\u00060\u0011R\u00020\u0000H\u0002¢\u0006\u0004\b\u0015\u0010\u0016J\u0017\u0010\u0019\u001a\u00020\u00182\u0006\u0010\u0017\u001a\u00020\u0006H\u0002¢\u0006\u0004\b\u0019\u0010\u001aJ\u0019\u0010\u001c\u001a\u00020\u000e2\b\b\u0002\u0010\u001b\u001a\u00020\u0006H\u0002¢\u0006\u0004\b\u001c\u0010\u001dJ\u000f\u0010\u001e\u001a\u00020\u000eH\u0002¢\u0006\u0004\b\u001e\u0010\u001fJ\u000f\u0010 \u001a\u00020\u0003H\u0002¢\u0006\u0004\b \u0010!J+\u0010#\u001a\u0004\u0018\u00010\f*\b\u0018\u00010\u0011R\u00020\u00002\u0006\u0010\r\u001a\u00020\f2\u0006\u0010\"\u001a\u00020\u000eH\u0002¢\u0006\u0004\b#\u0010$J\u0015\u0010%\u001a\b\u0018\u00010\u0011R\u00020\u0000H\u0002¢\u0006\u0004\b%\u0010\u0013J)\u0010(\u001a\u00020\u00182\n\u0010\u0014\u001a\u00060\u0011R\u00020\u00002\u0006\u0010&\u001a\u00020\u00032\u0006\u0010'\u001a\u00020\u0003¢\u0006\u0004\b(\u0010)J\u0019\u0010*\u001a\u00020\u000e2\n\u0010\u0014\u001a\u00060\u0011R\u00020\u0000¢\u0006\u0004\b*\u0010+J\u001b\u0010/\u001a\u00020\u00182\n\u0010.\u001a\u00060,j\u0002`-H\u0016¢\u0006\u0004\b/\u00100J\u000f\u00101\u001a\u00020\u0018H\u0016¢\u0006\u0004\b1\u00102J\u0015\u00104\u001a\u00020\u00182\u0006\u00103\u001a\u00020\u0006¢\u0006\u0004\b4\u0010\u001aJ1\u00108\u001a\u00020\u00182\n\u00105\u001a\u00060,j\u0002`-2\f\b\u0002\u00107\u001a\u00060\u000ej\u0002`62\b\b\u0002\u0010\"\u001a\u00020\u000e¢\u0006\u0004\b8\u00109J%\u0010:\u001a\u00020\f2\n\u00105\u001a\u00060,j\u0002`-2\n\u00107\u001a\u00060\u000ej\u0002`6¢\u0006\u0004\b:\u0010;J\r\u0010<\u001a\u00020\u0018¢\u0006\u0004\b<\u00102J\u000f\u0010=\u001a\u00020\bH\u0016¢\u0006\u0004\b=\u0010>J\u0015\u0010?\u001a\u00020\u00182\u0006\u0010\r\u001a\u00020\f¢\u0006\u0004\b?\u0010@R\u0014\u0010\u0004\u001a\u00020\u00038\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bA\u0010BR\u0014\u0010\u0005\u001a\u00020\u00038\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bC\u0010BR\u0014\u0010\u0007\u001a\u00020\u00068\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bD\u0010ER\u0014\u0010\t\u001a\u00020\b8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b\u000f\u0010FR\u0014\u0010I\u001a\u00020G8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b \u0010HR\u0014\u0010K\u001a\u00020G8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bJ\u0010HR\u001e\u0010O\u001a\f\u0012\b\u0012\u00060\u0011R\u00020\u00000L8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\bM\u0010NR\u0011\u0010P\u001a\u00020\u000e8F¢\u0006\u0006\u001a\u0004\bP\u0010\u001fR\u000b\u0010R\u001a\u00020Q8\u0002X\u0082\u0004R\u000b\u0010S\u001a\u00020Q8\u0002X\u0082\u0004R\u000b\u0010U\u001a\u00020T8\u0002X\u0082\u0004¨\u0006Z"}, d2 = {"Lkotlinx/coroutines/scheduling/CoroutineScheduler;", "Ljava/util/concurrent/Executor;", "Ljava/io/Closeable;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "corePoolSize", "maxPoolSize", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "idleWorkerKeepAliveNs", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "schedulerName", "<init>", "(IIJLjava/lang/String;)V", "Lkotlinx/coroutines/scheduling/Task;", "task", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "d", "(Lkotlinx/coroutines/scheduling/Task;)Z", "Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;", "G", "()Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;", "worker", "E", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;)I", "stateSnapshot", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "c0", "(J)V", "state", "p0", "(J)Z", "z0", "()Z", "e", "()I", "fair", "n0", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;Lkotlinx/coroutines/scheduling/Task;Z)Lkotlinx/coroutines/scheduling/Task;", "k", "oldIndex", "newIndex", "R", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;II)V", "O", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;)Z", "Ljava/lang/Runnable;", "Lkotlinx/coroutines/Runnable;", "command", "execute", "(Ljava/lang/Runnable;)V", "close", "()V", "timeout", "Y", "block", "Lkotlinx/coroutines/scheduling/TaskContext;", "taskContext", "r", "(Ljava/lang/Runnable;ZZ)V", "i", "(Ljava/lang/Runnable;Z)Lkotlinx/coroutines/scheduling/Task;", "d0", "toString", "()Ljava/lang/String;", "X", "(Lkotlinx/coroutines/scheduling/Task;)V", "a", "I", "b", "c", "J", "Ljava/lang/String;", "Lkotlinx/coroutines/scheduling/GlobalQueue;", "Lkotlinx/coroutines/scheduling/GlobalQueue;", "globalCpuQueue", "f", "globalBlockingQueue", "Lkotlinx/coroutines/internal/ResizableAtomicArray;", "g", "Lkotlinx/coroutines/internal/ResizableAtomicArray;", "workers", "isTerminated", "Lkotlinx/atomicfu/AtomicLong;", "parkedWorkersStack", "controlState", "Lkotlinx/atomicfu/AtomicBoolean;", "_isTerminated", "h", "Companion", "Worker", "WorkerState", "kotlinx-coroutines-core"}, k = 1, mv = {2, 1, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class CoroutineScheduler implements java.util.concurrent.Executor, java.io.Closeable {
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicLongFieldUpdater i = java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.CoroutineScheduler.class, "parkedWorkersStack$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicLongFieldUpdater j = java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.CoroutineScheduler.class, "controlState$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater k = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.CoroutineScheduler.class, "_isTerminated$volatile");
    public static final kotlinx.coroutines.internal.Symbol l = new kotlinx.coroutines.internal.Symbol("NOT_IN_STACK");
    private volatile /* synthetic */ int _isTerminated$volatile;

    /* JADX INFO: renamed from: a, reason: from kotlin metadata */
    public final int corePoolSize;

    /* JADX INFO: renamed from: b, reason: from kotlin metadata */
    public final int maxPoolSize;

    /* JADX INFO: renamed from: c, reason: from kotlin metadata */
    public final long idleWorkerKeepAliveNs;
    private volatile /* synthetic */ long controlState$volatile;

    /* JADX INFO: renamed from: d, reason: from kotlin metadata */
    public final java.lang.String schedulerName;

    /* JADX INFO: renamed from: e, reason: from kotlin metadata */
    public final kotlinx.coroutines.scheduling.GlobalQueue globalCpuQueue;

    /* JADX INFO: renamed from: f, reason: from kotlin metadata */
    public final kotlinx.coroutines.scheduling.GlobalQueue globalBlockingQueue;

    /* JADX INFO: renamed from: g, reason: from kotlin metadata */
    public final kotlinx.coroutines.internal.ResizableAtomicArray workers;
    private volatile /* synthetic */ long parkedWorkersStack$volatile;

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(k = 3, mv = {2, 1, 0}, xi = 48)
    public /* synthetic */ class WhenMappings {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.values().length];
            try {
                iArr[kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.PARKING.ordinal()] = 1;
            } catch (java.lang.NoSuchFieldError unused) {
            }
            try {
                iArr[kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.BLOCKING.ordinal()] = 2;
            } catch (java.lang.NoSuchFieldError unused2) {
            }
            try {
                iArr[kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.CPU_ACQUIRED.ordinal()] = 3;
            } catch (java.lang.NoSuchFieldError unused3) {
            }
            try {
                iArr[kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.DORMANT.ordinal()] = 4;
            } catch (java.lang.NoSuchFieldError unused4) {
            }
            try {
                iArr[kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED.ordinal()] = 5;
            } catch (java.lang.NoSuchFieldError unused5) {
            }
            a = iArr;
        }
    }

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0010\n\u0002\b\b\b\u0086\u0081\u0002\u0018\u00002\b\u0012\u0004\u0012\u00020\u00000\u0001B\t\b\u0002¢\u0006\u0004\b\u0002\u0010\u0003j\u0002\b\u0004j\u0002\b\u0005j\u0002\b\u0006j\u0002\b\u0007j\u0002\b\b¨\u0006\t"}, d2 = {"Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "<init>", "(Ljava/lang/String;I)V", "CPU_ACQUIRED", "BLOCKING", "PARKING", "DORMANT", "TERMINATED", "kotlinx-coroutines-core"}, k = 1, mv = {2, 1, 0}, xi = 48)
    public enum WorkerState {
        CPU_ACQUIRED,
        BLOCKING,
        PARKING,
        DORMANT,
        TERMINATED;

        private static final /* synthetic */ kotlin.enums.EnumEntries $ENTRIES = kotlin.enums.EnumEntriesKt.a(values());

        @org.jetbrains.annotations.NotNull
        public static kotlin.enums.EnumEntries<kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState> getEntries() {
            return $ENTRIES;
        }
    }

    public CoroutineScheduler(int i2, int i5, long j2, java.lang.String str) {
        this.corePoolSize = i2;
        this.maxPoolSize = i5;
        this.idleWorkerKeepAliveNs = j2;
        this.schedulerName = str;
        if (i2 < 1) {
            throw new java.lang.IllegalArgumentException(("Core pool size " + i2 + " should be at least 1").toString());
        }
        if (i5 < i2) {
            throw new java.lang.IllegalArgumentException(("Max pool size " + i5 + " should be greater than or equals to core pool size " + i2).toString());
        }
        if (i5 > 2097150) {
            throw new java.lang.IllegalArgumentException(("Max pool size " + i5 + " should not exceed maximal supported number of threads 2097150").toString());
        }
        if (j2 > 0) {
            this.globalCpuQueue = new kotlinx.coroutines.scheduling.GlobalQueue();
            this.globalBlockingQueue = new kotlinx.coroutines.scheduling.GlobalQueue();
            this.workers = new kotlinx.coroutines.internal.ResizableAtomicArray((i2 + 1) * 2);
            this.controlState$volatile = ((long) i2) << 42;
            return;
        }
        throw new java.lang.IllegalArgumentException(("Idle worker keep alive time " + j2 + " must be positive").toString());
    }

    private final int E(kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker) {
        java.lang.Object nextParkedWorker = worker.getNextParkedWorker();
        while (nextParkedWorker != l) {
            if (nextParkedWorker == null) {
                return 0;
            }
            kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker2 = (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) nextParkedWorker;
            int indexInArray = worker2.getIndexInArray();
            if (indexInArray != 0) {
                return indexInArray;
            }
            nextParkedWorker = worker2.getNextParkedWorker();
        }
        return -1;
    }

    private final kotlinx.coroutines.scheduling.CoroutineScheduler.Worker G() {
        java.util.concurrent.atomic.AtomicLongFieldUpdater atomicLongFieldUpdater = i;
        while (true) {
            long j2 = atomicLongFieldUpdater.get(this);
            kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) this.workers.b((int) (2097151 & j2));
            if (worker == null) {
                return null;
            }
            long j5 = (2097152 + j2) & (-2097152);
            int iE = this.E(worker);
            if (iE >= 0) {
                kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler = this;
                if (i.compareAndSet(coroutineScheduler, j2, ((long) iE) | j5)) {
                    worker.o(l);
                    return worker;
                }
                this = coroutineScheduler;
            }
        }
    }

    private final void c0(long stateSnapshot) {
        if (z0() || p0(stateSnapshot)) {
            return;
        }
        z0();
    }

    private final boolean d(kotlinx.coroutines.scheduling.Task task) {
        return task.taskContext ? this.globalBlockingQueue.a(task) : this.globalCpuQueue.a(task);
    }

    private final int e() {
        synchronized (this.workers) {
            try {
                if (isTerminated()) {
                    return -1;
                }
                long j2 = j.get(this);
                int i2 = (int) (j2 & 2097151);
                int iF = kotlin.ranges.RangesKt.f(i2 - ((int) ((j2 & 4398044413952L) >> 21)), 0);
                if (iF >= this.corePoolSize) {
                    return 0;
                }
                if (i2 >= this.maxPoolSize) {
                    return 0;
                }
                int i5 = ((int) (v().get(this) & 2097151)) + 1;
                if (i5 <= 0 || this.workers.b(i5) != null) {
                    throw new java.lang.IllegalArgumentException("Failed requirement.");
                }
                kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = new kotlinx.coroutines.scheduling.CoroutineScheduler.Worker(this, i5);
                this.workers.c(i5, worker);
                if (i5 != ((int) (2097151 & j.incrementAndGet(this)))) {
                    throw new java.lang.IllegalArgumentException("Failed requirement.");
                }
                int i6 = iF + 1;
                worker.start();
                return i6;
            } catch (java.lang.Throwable th) {
                throw th;
            }
        }
    }

    private final kotlinx.coroutines.scheduling.CoroutineScheduler.Worker k() {
        java.lang.Thread threadCurrentThread = java.lang.Thread.currentThread();
        kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = threadCurrentThread instanceof kotlinx.coroutines.scheduling.CoroutineScheduler.Worker ? (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) threadCurrentThread : null;
        if (worker == null || !kotlin.jvm.internal.Intrinsics.g(kotlinx.coroutines.scheduling.CoroutineScheduler.this, this)) {
            return null;
        }
        return worker;
    }

    private final kotlinx.coroutines.scheduling.Task n0(kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker, kotlinx.coroutines.scheduling.Task task, boolean z) {
        kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState workerState;
        if (worker == null || (workerState = worker.state) == kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED) {
            return task;
        }
        if (!task.taskContext && workerState == kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.BLOCKING) {
            return task;
        }
        worker.mayHaveLocalTasks = true;
        return worker.localQueue.a(task, z);
    }

    private final boolean p0(long state) {
        if (kotlin.ranges.RangesKt.f(((int) (2097151 & state)) - ((int) ((state & 4398044413952L) >> 21)), 0) < this.corePoolSize) {
            int iE = e();
            if (iE == 1 && this.corePoolSize > 1) {
                e();
            }
            if (iE > 0) {
                return true;
            }
        }
        return false;
    }

    public static /* synthetic */ void u(kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler, java.lang.Runnable runnable, boolean z, boolean z4, int i2, java.lang.Object obj) {
        if ((i2 & 2) != 0) {
            z = false;
        }
        if ((i2 & 4) != 0) {
            z4 = false;
        }
        coroutineScheduler.r(runnable, z, z4);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final /* synthetic */ java.util.concurrent.atomic.AtomicLongFieldUpdater v() {
        return j;
    }

    static /* synthetic */ boolean w0(kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler, long j2, int i2, java.lang.Object obj) {
        if ((i2 & 1) != 0) {
            j2 = j.get(coroutineScheduler);
        }
        return coroutineScheduler.p0(j2);
    }

    private final boolean z0() {
        kotlinx.coroutines.scheduling.CoroutineScheduler.Worker workerG;
        do {
            workerG = G();
            if (workerG == null) {
                return false;
            }
        } while (!kotlinx.coroutines.scheduling.CoroutineScheduler.Worker.i.compareAndSet(workerG, -1, 0));
        java.util.concurrent.locks.LockSupport.unpark(workerG);
        return true;
    }

    public final boolean O(kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker) {
        if (worker.getNextParkedWorker() != l) {
            return false;
        }
        java.util.concurrent.atomic.AtomicLongFieldUpdater atomicLongFieldUpdater = i;
        while (true) {
            long j2 = atomicLongFieldUpdater.get(this);
            int indexInArray = worker.getIndexInArray();
            worker.o(this.workers.b((int) (2097151 & j2)));
            long j5 = ((2097152 + j2) & (-2097152)) | ((long) indexInArray);
            kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler = this;
            if (i.compareAndSet(coroutineScheduler, j2, j5)) {
                return true;
            }
            this = coroutineScheduler;
        }
    }

    public final void R(kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker, int oldIndex, int newIndex) {
        java.util.concurrent.atomic.AtomicLongFieldUpdater atomicLongFieldUpdater = i;
        while (true) {
            long j2 = atomicLongFieldUpdater.get(this);
            int iE = (int) (2097151 & j2);
            long j5 = (2097152 + j2) & (-2097152);
            if (iE == oldIndex) {
                iE = newIndex == 0 ? this.E(worker) : newIndex;
            }
            if (iE >= 0) {
                kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler = this;
                if (i.compareAndSet(coroutineScheduler, j2, j5 | ((long) iE))) {
                    return;
                } else {
                    this = coroutineScheduler;
                }
            }
        }
    }

    public final void X(kotlinx.coroutines.scheduling.Task task) {
        try {
            task.run();
            kotlinx.coroutines.AbstractTimeSource abstractTimeSourceA = kotlinx.coroutines.AbstractTimeSourceKt.a();
            if (abstractTimeSourceA != null) {
                abstractTimeSourceA.e();
            }
        } catch (java.lang.Throwable th) {
            try {
                java.lang.Thread threadCurrentThread = java.lang.Thread.currentThread();
                threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, th);
            } finally {
                kotlinx.coroutines.AbstractTimeSource abstractTimeSourceA2 = kotlinx.coroutines.AbstractTimeSourceKt.a();
                if (abstractTimeSourceA2 != null) {
                    abstractTimeSourceA2.e();
                }
            }
        }
    }

    /* JADX WARN: Code duplicated, block: B:24:0x005f  */
    public final void Y(long timeout) throws java.lang.InterruptedException {
        int i2;
        kotlinx.coroutines.scheduling.Task taskE;
        if (k.compareAndSet(this, 0, 1)) {
            kotlinx.coroutines.scheduling.CoroutineScheduler.Worker workerK = k();
            synchronized (this.workers) {
                i2 = (int) (v().get(this) & 2097151);
            }
            if (1 <= i2) {
                int i5 = 1;
                while (true) {
                    java.lang.Object objB = this.workers.b(i5);
                    kotlin.jvm.internal.Intrinsics.i(objB);
                    kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) objB;
                    if (worker != workerK) {
                        while (worker.getState() != java.lang.Thread.State.TERMINATED) {
                            java.util.concurrent.locks.LockSupport.unpark(worker);
                            worker.join(timeout);
                        }
                        worker.localQueue.j(this.globalBlockingQueue);
                    }
                    if (i5 == i2) {
                        break;
                    } else {
                        i5++;
                    }
                }
            }
            this.globalBlockingQueue.b();
            this.globalCpuQueue.b();
            while (true) {
                if (workerK == null) {
                    taskE = (kotlinx.coroutines.scheduling.Task) this.globalCpuQueue.e();
                    if (taskE == null && (taskE = (kotlinx.coroutines.scheduling.Task) this.globalBlockingQueue.e()) == null) {
                        break;
                    }
                } else {
                    taskE = workerK.e(true);
                    if (taskE == null) {
                        taskE = (kotlinx.coroutines.scheduling.Task) this.globalCpuQueue.e();
                        if (taskE == null) {
                            continue;
                        }
                    } else {
                        continue;
                    }
                }
                X(taskE);
            }
            if (workerK != null) {
                workerK.r(kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED);
            }
            i.set(this, 0L);
            j.set(this, 0L);
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws java.lang.InterruptedException {
        Y(10000L);
    }

    public final void d0() {
        if (z0() || w0(this, 0L, 1, null)) {
            return;
        }
        z0();
    }

    @Override // java.util.concurrent.Executor
    public void execute(java.lang.Runnable command) {
        u(this, command, false, false, 6, null);
    }

    public final kotlinx.coroutines.scheduling.Task i(java.lang.Runnable block, boolean taskContext) {
        long jA = kotlinx.coroutines.scheduling.TasksKt.f.a();
        if (!(block instanceof kotlinx.coroutines.scheduling.Task)) {
            return kotlinx.coroutines.scheduling.TasksKt.b(block, jA, taskContext);
        }
        kotlinx.coroutines.scheduling.Task task = (kotlinx.coroutines.scheduling.Task) block;
        task.submissionTime = jA;
        task.taskContext = taskContext;
        return task;
    }

    public final boolean isTerminated() {
        return k.get(this) == 1;
    }

    public final void r(java.lang.Runnable block, boolean taskContext, boolean fair) {
        kotlinx.coroutines.AbstractTimeSource abstractTimeSourceA = kotlinx.coroutines.AbstractTimeSourceKt.a();
        if (abstractTimeSourceA != null) {
            abstractTimeSourceA.d();
        }
        kotlinx.coroutines.scheduling.Task taskI = i(block, taskContext);
        boolean z = taskI.taskContext;
        long jAddAndGet = z ? j.addAndGet(this, 2097152L) : 0L;
        kotlinx.coroutines.scheduling.Task taskN0 = n0(k(), taskI, fair);
        if (taskN0 != null && !d(taskN0)) {
            throw new java.util.concurrent.RejectedExecutionException(this.schedulerName + " was terminated");
        }
        if (z) {
            c0(jAddAndGet);
        } else {
            d0();
        }
    }

    /* JADX INFO: Thrown type has an unknown type hierarchy: kotlin.NoWhenBranchMatchedException */
    public java.lang.String toString() throws kotlin.NoWhenBranchMatchedException {
        java.util.ArrayList arrayList = new java.util.ArrayList();
        int iA = this.workers.a();
        int i2 = 0;
        int i5 = 0;
        int i6 = 0;
        int i7 = 0;
        int i8 = 0;
        for (int i9 = 1; i9 < iA; i9++) {
            kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) this.workers.b(i9);
            if (worker != null) {
                int i10 = worker.localQueue.i();
                int i11 = kotlinx.coroutines.scheduling.CoroutineScheduler.WhenMappings.a[worker.state.ordinal()];
                if (i11 == 1) {
                    i6++;
                } else if (i11 == 2) {
                    i5++;
                    java.lang.StringBuilder sb = new java.lang.StringBuilder();
                    sb.append(i10);
                    sb.append('b');
                    arrayList.add(sb.toString());
                } else if (i11 == 3) {
                    i2++;
                    java.lang.StringBuilder sb2 = new java.lang.StringBuilder();
                    sb2.append(i10);
                    sb2.append('c');
                    arrayList.add(sb2.toString());
                } else if (i11 == 4) {
                    i7++;
                    if (i10 > 0) {
                        java.lang.StringBuilder sb3 = new java.lang.StringBuilder();
                        sb3.append(i10);
                        sb3.append('d');
                        arrayList.add(sb3.toString());
                    }
                } else {
                    if (i11 != 5) {
                        throw new kotlin.NoWhenBranchMatchedException();
                    }
                    i8++;
                }
            }
        }
        long j2 = j.get(this);
        return this.schedulerName + '@' + kotlinx.coroutines.DebugStringsKt.b(this) + "[Pool Size {core = " + this.corePoolSize + ", max = " + this.maxPoolSize + "}, Worker States {CPU = " + i2 + ", blocking = " + i5 + ", parked = " + i6 + ", dormant = " + i7 + ", terminated = " + i8 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.globalCpuQueue.c() + ", global blocking queue size = " + this.globalBlockingQueue.c() + ", Control State {created workers= " + ((int) (2097151 & j2)) + ", blocking tasks = " + ((int) ((4398044413952L & j2) >> 21)) + ", CPUs acquired = " + (this.corePoolSize - ((int) ((9223367638808264704L & j2) >> 42))) + "}]";
    }

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u0000d\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u000b\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u000f\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0010\u0000\n\u0002\b\t\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0080\u0004\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\u0002\u0010\u0003B\u0011\b\u0016\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0002\u0010\u0006J\u000f\u0010\b\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\b\u0010\tJ\u000f\u0010\u000b\u001a\u00020\nH\u0002¢\u0006\u0004\b\u000b\u0010\fJ\u000f\u0010\r\u001a\u00020\nH\u0002¢\u0006\u0004\b\r\u0010\fJ\u000f\u0010\u000e\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\u000e\u0010\tJ\u0017\u0010\u0011\u001a\u00020\n2\u0006\u0010\u0010\u001a\u00020\u000fH\u0002¢\u0006\u0004\b\u0011\u0010\u0012J\u000f\u0010\u0013\u001a\u00020\nH\u0002¢\u0006\u0004\b\u0013\u0010\fJ\u000f\u0010\u0014\u001a\u00020\nH\u0002¢\u0006\u0004\b\u0014\u0010\fJ\u0011\u0010\u0015\u001a\u0004\u0018\u00010\u000fH\u0002¢\u0006\u0004\b\u0015\u0010\u0016J\u0019\u0010\u0018\u001a\u0004\u0018\u00010\u000f2\u0006\u0010\u0017\u001a\u00020\u0007H\u0002¢\u0006\u0004\b\u0018\u0010\u0019J\u0011\u0010\u001a\u001a\u0004\u0018\u00010\u000fH\u0002¢\u0006\u0004\b\u001a\u0010\u0016J\u001d\u0010\u001d\u001a\u0004\u0018\u00010\u000f2\n\u0010\u001c\u001a\u00060\u0004j\u0002`\u001bH\u0002¢\u0006\u0004\b\u001d\u0010\u001eJ\u0015\u0010!\u001a\u00020\u00072\u0006\u0010 \u001a\u00020\u001f¢\u0006\u0004\b!\u0010\"J\u000f\u0010#\u001a\u00020\nH\u0016¢\u0006\u0004\b#\u0010\fJ\u0015\u0010%\u001a\u00020\u00042\u0006\u0010$\u001a\u00020\u0004¢\u0006\u0004\b%\u0010&J\u0017\u0010(\u001a\u0004\u0018\u00010\u000f2\u0006\u0010'\u001a\u00020\u0007¢\u0006\u0004\b(\u0010\u0019R*\u0010)\u001a\u00020\u00042\u0006\u0010\u0005\u001a\u00020\u00048\u0006@FX\u0086\u000e¢\u0006\u0012\n\u0004\b)\u0010*\u001a\u0004\b+\u0010,\"\u0004\b-\u0010.R\u0014\u00102\u001a\u00020/8\u0006X\u0087\u0004¢\u0006\u0006\n\u0004\b0\u00101R\u001c\u00105\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u000f038\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0011\u00104R\u0016\u00107\u001a\u00020\u001f8\u0006@\u0006X\u0087\u000e¢\u0006\u0006\n\u0004\b\u0018\u00106R\u0016\u0010:\u001a\u0002088\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0015\u00109R$\u0010<\u001a\u0004\u0018\u00010;8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b<\u0010=\u001a\u0004\b>\u0010?\"\u0004\b@\u0010AR\u0016\u0010B\u001a\u0002088\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b(\u00109R\u0016\u0010C\u001a\u00020\u00048\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b+\u0010*R\u0016\u0010'\u001a\u00020\u00078\u0006@\u0006X\u0087\u000e¢\u0006\u0006\n\u0004\b>\u0010DR\b\u0010F\u001a\u00020E8\u0006¨\u0006G"}, d2 = {"Lkotlinx/coroutines/scheduling/CoroutineScheduler$Worker;", "Ljava/lang/Thread;", "<init>", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler;)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "index", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler;I)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "p", "()Z", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "m", "()V", "q", "i", "Lkotlinx/coroutines/scheduling/Task;", "task", "b", "(Lkotlinx/coroutines/scheduling/Task;)V", "k", "t", "d", "()Lkotlinx/coroutines/scheduling/Task;", "scanLocalQueue", "c", "(Z)Lkotlinx/coroutines/scheduling/Task;", "l", "Lkotlinx/coroutines/scheduling/StealingMode;", "stealingMode", "s", "(I)Lkotlinx/coroutines/scheduling/Task;", "Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;", "newState", "r", "(Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;)Z", "run", "upperBound", "j", "(I)I", "mayHaveLocalTasks", "e", "indexInArray", "I", "f", "()I", "n", "(I)V", "Lkotlinx/coroutines/scheduling/WorkQueue;", "a", "Lkotlinx/coroutines/scheduling/WorkQueue;", "localQueue", "Lkotlin/jvm/internal/Ref$ObjectRef;", "Lkotlin/jvm/internal/Ref$ObjectRef;", "stolenTask", "Lkotlinx/coroutines/scheduling/CoroutineScheduler$WorkerState;", "state", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "J", "terminationDeadline", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "nextParkedWorker", "Ljava/lang/Object;", "g", "()Ljava/lang/Object;", "o", "(Ljava/lang/Object;)V", "minDelayUntilStealableTaskNs", "rngState", "Z", "Lkotlinx/atomicfu/AtomicInt;", "workerCtl", "kotlinx-coroutines-core"}, k = 1, mv = {2, 1, 0}, xi = 48)
    @kotlin.jvm.internal.SourceDebugExtension
    public final class Worker extends java.lang.Thread {
        private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater i = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.CoroutineScheduler.Worker.class, "workerCtl$volatile");

        /* JADX INFO: renamed from: a, reason: from kotlin metadata */
        public final kotlinx.coroutines.scheduling.WorkQueue localQueue;

        /* JADX INFO: renamed from: b, reason: from kotlin metadata */
        private final kotlin.jvm.internal.Ref.ObjectRef stolenTask;

        /* JADX INFO: renamed from: c, reason: from kotlin metadata */
        public kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState state;

        /* JADX INFO: renamed from: d, reason: from kotlin metadata */
        private long terminationDeadline;

        /* JADX INFO: renamed from: e, reason: from kotlin metadata */
        private long minDelayUntilStealableTaskNs;

        /* JADX INFO: renamed from: f, reason: from kotlin metadata */
        private int rngState;

        /* JADX INFO: renamed from: g, reason: from kotlin metadata */
        public boolean mayHaveLocalTasks;
        private volatile int indexInArray;

        @org.jetbrains.annotations.Nullable
        private volatile java.lang.Object nextParkedWorker;
        private volatile /* synthetic */ int workerCtl$volatile;

        private Worker() {
            setDaemon(true);
            setContextClassLoader(kotlinx.coroutines.scheduling.CoroutineScheduler.this.getClass().getClassLoader());
            this.localQueue = new kotlinx.coroutines.scheduling.WorkQueue();
            this.stolenTask = new kotlin.jvm.internal.Ref.ObjectRef();
            this.state = kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.DORMANT;
            this.nextParkedWorker = kotlinx.coroutines.scheduling.CoroutineScheduler.l;
            int iNanoTime = (int) java.lang.System.nanoTime();
            this.rngState = iNanoTime == 0 ? 42 : iNanoTime;
        }

        private final void b(kotlinx.coroutines.scheduling.Task task) {
            this.terminationDeadline = 0L;
            if (this.state == kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.PARKING) {
                this.state = kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.BLOCKING;
            }
            if (!task.taskContext) {
                kotlinx.coroutines.scheduling.CoroutineScheduler.this.X(task);
                return;
            }
            if (r(kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.BLOCKING)) {
                kotlinx.coroutines.scheduling.CoroutineScheduler.this.d0();
            }
            kotlinx.coroutines.scheduling.CoroutineScheduler.this.X(task);
            kotlinx.coroutines.scheduling.CoroutineScheduler.v().addAndGet(kotlinx.coroutines.scheduling.CoroutineScheduler.this, -2097152L);
            if (this.state != kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED) {
                this.state = kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.DORMANT;
            }
        }

        private final kotlinx.coroutines.scheduling.Task c(boolean scanLocalQueue) {
            kotlinx.coroutines.scheduling.Task taskL;
            kotlinx.coroutines.scheduling.Task taskL2;
            if (scanLocalQueue) {
                boolean z = j(kotlinx.coroutines.scheduling.CoroutineScheduler.this.corePoolSize * 2) == 0;
                if (z && (taskL2 = l()) != null) {
                    return taskL2;
                }
                kotlinx.coroutines.scheduling.Task taskK = this.localQueue.k();
                if (taskK != null) {
                    return taskK;
                }
                if (!z && (taskL = l()) != null) {
                    return taskL;
                }
            } else {
                kotlinx.coroutines.scheduling.Task taskL3 = l();
                if (taskL3 != null) {
                    return taskL3;
                }
            }
            return s(3);
        }

        private final kotlinx.coroutines.scheduling.Task d() {
            kotlinx.coroutines.scheduling.Task taskL = this.localQueue.l();
            return (taskL == null && (taskL = (kotlinx.coroutines.scheduling.Task) kotlinx.coroutines.scheduling.CoroutineScheduler.this.globalBlockingQueue.e()) == null) ? s(1) : taskL;
        }

        private final boolean i() {
            return this.nextParkedWorker != kotlinx.coroutines.scheduling.CoroutineScheduler.l;
        }

        private final void k() {
            if (this.terminationDeadline == 0) {
                this.terminationDeadline = java.lang.System.nanoTime() + kotlinx.coroutines.scheduling.CoroutineScheduler.this.idleWorkerKeepAliveNs;
            }
            java.util.concurrent.locks.LockSupport.parkNanos(kotlinx.coroutines.scheduling.CoroutineScheduler.this.idleWorkerKeepAliveNs);
            if (java.lang.System.nanoTime() - this.terminationDeadline >= 0) {
                this.terminationDeadline = 0L;
                t();
            }
        }

        private final kotlinx.coroutines.scheduling.Task l() {
            if (j(2) == 0) {
                kotlinx.coroutines.scheduling.Task task = (kotlinx.coroutines.scheduling.Task) kotlinx.coroutines.scheduling.CoroutineScheduler.this.globalCpuQueue.e();
                return task != null ? task : (kotlinx.coroutines.scheduling.Task) kotlinx.coroutines.scheduling.CoroutineScheduler.this.globalBlockingQueue.e();
            }
            kotlinx.coroutines.scheduling.Task task2 = (kotlinx.coroutines.scheduling.Task) kotlinx.coroutines.scheduling.CoroutineScheduler.this.globalBlockingQueue.e();
            return task2 != null ? task2 : (kotlinx.coroutines.scheduling.Task) kotlinx.coroutines.scheduling.CoroutineScheduler.this.globalCpuQueue.e();
        }

        private final void m() {
            loop0: while (true) {
                boolean z = false;
                while (true) {
                    if (kotlinx.coroutines.scheduling.CoroutineScheduler.this.isTerminated() || this.state == kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED) {
                        break loop0;
                    }
                    kotlinx.coroutines.scheduling.Task taskE = e(this.mayHaveLocalTasks);
                    if (taskE != null) {
                        this.minDelayUntilStealableTaskNs = 0L;
                        b(taskE);
                        break;
                    }
                    this.mayHaveLocalTasks = false;
                    if (this.minDelayUntilStealableTaskNs == 0) {
                        q();
                    } else {
                        if (z) {
                            r(kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.PARKING);
                            java.lang.Thread.interrupted();
                            java.util.concurrent.locks.LockSupport.parkNanos(this.minDelayUntilStealableTaskNs);
                            this.minDelayUntilStealableTaskNs = 0L;
                            break;
                        }
                        z = true;
                    }
                }
            }
            r(kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED);
        }

        private final boolean p() {
            long j;
            if (this.state == kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.CPU_ACQUIRED) {
                return true;
            }
            kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler = kotlinx.coroutines.scheduling.CoroutineScheduler.this;
            java.util.concurrent.atomic.AtomicLongFieldUpdater atomicLongFieldUpdaterV = kotlinx.coroutines.scheduling.CoroutineScheduler.v();
            do {
                j = atomicLongFieldUpdaterV.get(coroutineScheduler);
                if (((int) ((9223367638808264704L & j) >> 42)) == 0) {
                    return false;
                }
            } while (!kotlinx.coroutines.scheduling.CoroutineScheduler.v().compareAndSet(coroutineScheduler, j, j - 4398046511104L));
            this.state = kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.CPU_ACQUIRED;
            return true;
        }

        private final void q() {
            if (!i()) {
                kotlinx.coroutines.scheduling.CoroutineScheduler.this.O(this);
                return;
            }
            i.set(this, -1);
            while (i() && i.get(this) == -1 && !kotlinx.coroutines.scheduling.CoroutineScheduler.this.isTerminated() && this.state != kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED) {
                r(kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.PARKING);
                java.lang.Thread.interrupted();
                k();
            }
        }

        private final kotlinx.coroutines.scheduling.Task s(int stealingMode) {
            int i2 = (int) (kotlinx.coroutines.scheduling.CoroutineScheduler.v().get(kotlinx.coroutines.scheduling.CoroutineScheduler.this) & 2097151);
            if (i2 < 2) {
                return null;
            }
            int iJ = j(i2);
            kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler = kotlinx.coroutines.scheduling.CoroutineScheduler.this;
            long jMin = Long.MAX_VALUE;
            for (int i5 = 0; i5 < i2; i5++) {
                iJ++;
                if (iJ > i2) {
                    iJ = 1;
                }
                kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) coroutineScheduler.workers.b(iJ);
                if (worker != null && worker != this) {
                    long jR = worker.localQueue.r(stealingMode, this.stolenTask);
                    if (jR == -1) {
                        kotlin.jvm.internal.Ref.ObjectRef objectRef = this.stolenTask;
                        kotlinx.coroutines.scheduling.Task task = (kotlinx.coroutines.scheduling.Task) objectRef.a;
                        objectRef.a = null;
                        return task;
                    }
                    if (jR > 0) {
                        jMin = java.lang.Math.min(jMin, jR);
                    }
                }
            }
            if (jMin == Long.MAX_VALUE) {
                jMin = 0;
            }
            this.minDelayUntilStealableTaskNs = jMin;
            return null;
        }

        private final void t() {
            kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler = kotlinx.coroutines.scheduling.CoroutineScheduler.this;
            synchronized (coroutineScheduler.workers) {
                try {
                    if (coroutineScheduler.isTerminated()) {
                        return;
                    }
                    if (((int) (kotlinx.coroutines.scheduling.CoroutineScheduler.v().get(coroutineScheduler) & 2097151)) <= coroutineScheduler.corePoolSize) {
                        return;
                    }
                    if (i.compareAndSet(this, -1, 1)) {
                        int i2 = this.indexInArray;
                        n(0);
                        coroutineScheduler.R(this, i2, 0);
                        int andDecrement = (int) (kotlinx.coroutines.scheduling.CoroutineScheduler.v().getAndDecrement(coroutineScheduler) & 2097151);
                        if (andDecrement != i2) {
                            java.lang.Object objB = coroutineScheduler.workers.b(andDecrement);
                            kotlin.jvm.internal.Intrinsics.i(objB);
                            kotlinx.coroutines.scheduling.CoroutineScheduler.Worker worker = (kotlinx.coroutines.scheduling.CoroutineScheduler.Worker) objB;
                            coroutineScheduler.workers.c(i2, worker);
                            worker.n(i2);
                            coroutineScheduler.R(worker, andDecrement, i2);
                        }
                        coroutineScheduler.workers.c(andDecrement, null);
                        kotlin.Unit unit = kotlin.Unit.a;
                        this.state = kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.TERMINATED;
                    }
                } catch (java.lang.Throwable th) {
                    throw th;
                }
            }
        }

        public final kotlinx.coroutines.scheduling.Task e(boolean mayHaveLocalTasks) {
            return p() ? c(mayHaveLocalTasks) : d();
        }

        /* JADX INFO: renamed from: f, reason: from getter */
        public final int getIndexInArray() {
            return this.indexInArray;
        }

        /* JADX INFO: renamed from: g, reason: from getter */
        public final java.lang.Object getNextParkedWorker() {
            return this.nextParkedWorker;
        }

        public final int j(int upperBound) {
            int i2 = this.rngState;
            int i5 = i2 ^ (i2 << 13);
            int i6 = i5 ^ (i5 >> 17);
            int i7 = i6 ^ (i6 << 5);
            this.rngState = i7;
            int i8 = upperBound - 1;
            return (i8 & upperBound) == 0 ? i8 & i7 : (Integer.MAX_VALUE & i7) % upperBound;
        }

        public final void n(int i2) {
            java.lang.StringBuilder sb = new java.lang.StringBuilder();
            sb.append(kotlinx.coroutines.scheduling.CoroutineScheduler.this.schedulerName);
            sb.append("-worker-");
            sb.append(i2 == 0 ? "TERMINATED" : java.lang.String.valueOf(i2));
            setName(sb.toString());
            this.indexInArray = i2;
        }

        public final void o(java.lang.Object obj) {
            this.nextParkedWorker = obj;
        }

        public final boolean r(kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState newState) {
            kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState workerState = this.state;
            boolean z = workerState == kotlinx.coroutines.scheduling.CoroutineScheduler.WorkerState.CPU_ACQUIRED;
            if (z) {
                kotlinx.coroutines.scheduling.CoroutineScheduler.v().addAndGet(kotlinx.coroutines.scheduling.CoroutineScheduler.this, 4398046511104L);
            }
            if (workerState != newState) {
                this.state = newState;
            }
            return z;
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            m();
        }

        public Worker(kotlinx.coroutines.scheduling.CoroutineScheduler coroutineScheduler, int i2) {
            this();
            n(i2);
        }
    }
}
