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 = {"\u0000\\\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u0002\n\u0002\b\u000b\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0000\u0018\u00002\u00020\u0001B\u0007¢\u0006\u0004\b\u0002\u0010\u0003J\u0019\u0010\u0006\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0006\u0010\u0007J\u001d\u0010\u000b\u001a\u0004\u0018\u00010\u00042\n\u0010\n\u001a\u00060\bj\u0002`\tH\u0002¢\u0006\u0004\b\u000b\u0010\fJ\u0019\u0010\u000f\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u000f\u0010\u0010J!\u0010\u0012\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u0011\u001a\u00020\b2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u0012\u0010\u0013J+\u0010\u0017\u001a\u00020\u00162\n\u0010\n\u001a\u00060\bj\u0002`\t2\u000e\u0010\u0015\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u00040\u0014H\u0002¢\u0006\u0004\b\u0017\u0010\u0018J\u0017\u0010\u001b\u001a\u00020\r2\u0006\u0010\u001a\u001a\u00020\u0019H\u0002¢\u0006\u0004\b\u001b\u0010\u001cJ\u0011\u0010\u001d\u001a\u0004\u0018\u00010\u0004H\u0002¢\u0006\u0004\b\u001d\u0010\u001eJ\u0015\u0010 \u001a\u00020\u001f*\u0004\u0018\u00010\u0004H\u0002¢\u0006\u0004\b \u0010!J\u000f\u0010\"\u001a\u0004\u0018\u00010\u0004¢\u0006\u0004\b\"\u0010\u001eJ!\u0010$\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u0005\u001a\u00020\u00042\b\b\u0002\u0010#\u001a\u00020\r¢\u0006\u0004\b$\u0010%J)\u0010&\u001a\u00020\u00162\n\u0010\n\u001a\u00060\bj\u0002`\t2\u000e\u0010\u0015\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u00040\u0014¢\u0006\u0004\b&\u0010\u0018J\u000f\u0010'\u001a\u0004\u0018\u00010\u0004¢\u0006\u0004\b'\u0010\u001eJ\u0015\u0010)\u001a\u00020\u001f2\u0006\u0010(\u001a\u00020\u0019¢\u0006\u0004\b)\u0010*R\u001c\u0010-\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u00040+8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b$\u0010,R\u0014\u00100\u001a\u00020\b8BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b.\u0010/R\u0014\u00102\u001a\u00020\b8@X\u0080\u0004¢\u0006\u0006\u001a\u0004\b1\u0010/R\u0013\u00104\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u0004038\u0002X\u0082\u0004R\u000b\u00106\u001a\u0002058\u0002X\u0082\u0004R\u000b\u00107\u001a\u0002058\u0002X\u0082\u0004R\u000b\u00108\u001a\u0002058\u0002X\u0082\u0004¨\u00069"}, d2 = {"Lkotlinx/coroutines/scheduling/WorkQueue;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "<init>", "()V", "Lkotlinx/coroutines/scheduling/Task;", "task", "b", "(Lkotlinx/coroutines/scheduling/Task;)Lkotlinx/coroutines/scheduling/Task;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lkotlinx/coroutines/scheduling/StealingMode;", "stealingMode", "p", "(I)Lkotlinx/coroutines/scheduling/Task;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "onlyBlocking", "o", "(Z)Lkotlinx/coroutines/scheduling/Task;", "index", "q", "(IZ)Lkotlinx/coroutines/scheduling/Task;", "Lkotlin/jvm/internal/Ref$ObjectRef;", "stolenTaskRef", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "s", "(ILkotlin/jvm/internal/Ref$ObjectRef;)J", "Lkotlinx/coroutines/scheduling/GlobalQueue;", "queue", "n", "(Lkotlinx/coroutines/scheduling/GlobalQueue;)Z", "m", "()Lkotlinx/coroutines/scheduling/Task;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "c", "(Lkotlinx/coroutines/scheduling/Task;)V", "k", "fair", "a", "(Lkotlinx/coroutines/scheduling/Task;Z)Lkotlinx/coroutines/scheduling/Task;", "r", "l", "globalQueue", "j", "(Lkotlinx/coroutines/scheduling/GlobalQueue;)V", "Ljava/util/concurrent/atomic/AtomicReferenceArray;", "Ljava/util/concurrent/atomic/AtomicReferenceArray;", "buffer", "e", "()I", "bufferSize", "i", "size", "Lkotlinx/atomicfu/AtomicRef;", "lastScheduledTask", "Lkotlinx/atomicfu/AtomicInt;", "producerIndex", "consumerIndex", "blockingTasksInBuffer", "kotlinx-coroutines-core"}, k = 1, mv = {2, 1, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class WorkQueue {
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicReferenceFieldUpdater b = java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.WorkQueue.class, java.lang.Object.class, "lastScheduledTask$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater c = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.WorkQueue.class, "producerIndex$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater d = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.WorkQueue.class, "consumerIndex$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater e = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.scheduling.WorkQueue.class, "blockingTasksInBuffer$volatile");

    /* JADX INFO: renamed from: a, reason: from kotlin metadata */
    private final java.util.concurrent.atomic.AtomicReferenceArray buffer = new java.util.concurrent.atomic.AtomicReferenceArray(128);
    private volatile /* synthetic */ int blockingTasksInBuffer$volatile;
    private volatile /* synthetic */ int consumerIndex$volatile;
    private volatile /* synthetic */ java.lang.Object lastScheduledTask$volatile;
    private volatile /* synthetic */ int producerIndex$volatile;

    private final kotlinx.coroutines.scheduling.Task b(kotlinx.coroutines.scheduling.Task task) {
        if (e() == 127) {
            return task;
        }
        if (task.taskContext) {
            e.incrementAndGet(this);
        }
        int i = c.get(this) & 127;
        while (this.buffer.get(i) != null) {
            java.lang.Thread.yield();
        }
        this.buffer.lazySet(i, task);
        c.incrementAndGet(this);
        return null;
    }

    private final void c(kotlinx.coroutines.scheduling.Task task) {
        if (task == null || !task.taskContext) {
            return;
        }
        e.decrementAndGet(this);
    }

    private final int e() {
        return c.get(this) - d.get(this);
    }

    private final kotlinx.coroutines.scheduling.Task m() {
        kotlinx.coroutines.scheduling.Task task;
        while (true) {
            int i = d.get(this);
            if (i - c.get(this) == 0) {
                return null;
            }
            int i2 = i & 127;
            if (d.compareAndSet(this, i, i + 1) && (task = (kotlinx.coroutines.scheduling.Task) this.buffer.getAndSet(i2, null)) != null) {
                c(task);
                return task;
            }
        }
    }

    private final boolean n(kotlinx.coroutines.scheduling.GlobalQueue queue) {
        kotlinx.coroutines.scheduling.Task taskM = m();
        if (taskM == null) {
            return false;
        }
        queue.a(taskM);
        return true;
    }

    private final kotlinx.coroutines.scheduling.Task o(boolean onlyBlocking) {
        kotlinx.coroutines.scheduling.Task task;
        do {
            task = (kotlinx.coroutines.scheduling.Task) b.get(this);
            if (task == null || task.taskContext != onlyBlocking) {
                int i = d.get(this);
                int i2 = c.get(this);
                while (i != i2) {
                    if (onlyBlocking && e.get(this) == 0) {
                        return null;
                    }
                    i2--;
                    kotlinx.coroutines.scheduling.Task taskQ = q(i2, onlyBlocking);
                    if (taskQ != null) {
                        return taskQ;
                    }
                }
                return null;
            }
        } while (!androidx.concurrent.futures.a.a(b, this, task, (java.lang.Object) null));
        return task;
    }

    private final kotlinx.coroutines.scheduling.Task p(int stealingMode) {
        int i = d.get(this);
        int i2 = c.get(this);
        boolean z = stealingMode == 1;
        while (i != i2) {
            if (z && e.get(this) == 0) {
                return null;
            }
            int i5 = i + 1;
            kotlinx.coroutines.scheduling.Task taskQ = q(i, z);
            if (taskQ != null) {
                return taskQ;
            }
            i = i5;
        }
        return null;
    }

    private final kotlinx.coroutines.scheduling.Task q(int index, boolean onlyBlocking) {
        int i = index & 127;
        kotlinx.coroutines.scheduling.Task task = (kotlinx.coroutines.scheduling.Task) this.buffer.get(i);
        if (task == null || task.taskContext != onlyBlocking || !com.google.common.util.concurrent.f0.a(this.buffer, i, task, (java.lang.Object) null)) {
            return null;
        }
        if (onlyBlocking) {
            e.decrementAndGet(this);
        }
        return task;
    }

    private final long s(int stealingMode, kotlin.jvm.internal.Ref.ObjectRef stolenTaskRef) {
        kotlinx.coroutines.scheduling.Task task;
        do {
            task = (kotlinx.coroutines.scheduling.Task) b.get(this);
            if (task == null) {
                return -2L;
            }
            if (((task.taskContext ? 1 : 2) & stealingMode) == 0) {
                return -2L;
            }
            long jA = kotlinx.coroutines.scheduling.TasksKt.f.a() - task.submissionTime;
            long j = kotlinx.coroutines.scheduling.TasksKt.b;
            if (jA < j) {
                return j - jA;
            }
        } while (!androidx.concurrent.futures.a.a(b, this, task, (java.lang.Object) null));
        stolenTaskRef.a = task;
        return -1L;
    }

    public final kotlinx.coroutines.scheduling.Task a(kotlinx.coroutines.scheduling.Task task, boolean fair) {
        if (fair) {
            return b(task);
        }
        kotlinx.coroutines.scheduling.Task task2 = (kotlinx.coroutines.scheduling.Task) b.getAndSet(this, task);
        if (task2 == null) {
            return null;
        }
        return b(task2);
    }

    public final int i() {
        java.lang.Object obj = b.get(this);
        int iE = e();
        return obj != null ? iE + 1 : iE;
    }

    public final void j(kotlinx.coroutines.scheduling.GlobalQueue globalQueue) {
        kotlinx.coroutines.scheduling.Task task = (kotlinx.coroutines.scheduling.Task) b.getAndSet(this, null);
        if (task != null) {
            globalQueue.a(task);
        }
        while (n(globalQueue)) {
        }
    }

    public final kotlinx.coroutines.scheduling.Task k() {
        kotlinx.coroutines.scheduling.Task task = (kotlinx.coroutines.scheduling.Task) b.getAndSet(this, null);
        return task == null ? m() : task;
    }

    public final kotlinx.coroutines.scheduling.Task l() {
        return o(true);
    }

    public final long r(int stealingMode, kotlin.jvm.internal.Ref.ObjectRef stolenTaskRef) {
        kotlinx.coroutines.scheduling.Task taskM = stealingMode == 3 ? m() : p(stealingMode);
        if (taskM == null) {
            return s(stealingMode, stolenTaskRef);
        }
        stolenTaskRef.a = taskM;
        return -1L;
    }
}
