package kotlinx.coroutines.scheduling;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import p060t1.C1679h;

/* renamed from: kotlinx.coroutines.scheduling.o */
/* loaded from: classes.dex */
public final class C1406o {

    /* renamed from: b */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f3116b = AtomicReferenceFieldUpdater.newUpdater(C1406o.class, Object.class, "lastScheduledTask");

    /* renamed from: c */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f3117c = AtomicIntegerFieldUpdater.newUpdater(C1406o.class, "producerIndex");

    /* renamed from: d */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f3118d = AtomicIntegerFieldUpdater.newUpdater(C1406o.class, "consumerIndex");

    /* renamed from: e */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f3119e = AtomicIntegerFieldUpdater.newUpdater(C1406o.class, "blockingTasksInBuffer");

    /* renamed from: a */
    public final AtomicReferenceArray<AbstractRunnableC1399h> f3120a = new AtomicReferenceArray<>(128);
    private volatile /* synthetic */ Object lastScheduledTask = null;
    private volatile /* synthetic */ int producerIndex = 0;
    private volatile /* synthetic */ int consumerIndex = 0;
    private volatile /* synthetic */ int blockingTasksInBuffer = 0;

    /* renamed from: b */
    public static /* synthetic */ AbstractRunnableC1399h m1420b(C1406o c1406o, AbstractRunnableC1399h abstractRunnableC1399h, boolean z, int i, Object obj) {
        if ((i & 2) != 0) {
            z = false;
        }
        return c1406o.m1421a(abstractRunnableC1399h, z);
    }

    /* renamed from: a */
    public final AbstractRunnableC1399h m1421a(AbstractRunnableC1399h abstractRunnableC1399h, boolean z) {
        if (z) {
            return m1419c(abstractRunnableC1399h);
        }
        AbstractRunnableC1399h abstractRunnableC1399h2 = (AbstractRunnableC1399h) f3116b.getAndSet(this, abstractRunnableC1399h);
        if (abstractRunnableC1399h2 == null) {
            return null;
        }
        return m1419c(abstractRunnableC1399h2);
    }

    /* renamed from: c */
    public final AbstractRunnableC1399h m1419c(AbstractRunnableC1399h abstractRunnableC1399h) {
        if (abstractRunnableC1399h.f3105e.mo1424a() == 1) {
            f3119e.incrementAndGet(this);
        }
        if (m1417e() == 127) {
            return abstractRunnableC1399h;
        }
        int i = this.producerIndex & 127;
        while (this.f3120a.get(i) != null) {
            Thread.yield();
        }
        this.f3120a.lazySet(i, abstractRunnableC1399h);
        f3117c.incrementAndGet(this);
        return null;
    }

    /* renamed from: d */
    public final void m1418d(AbstractRunnableC1399h abstractRunnableC1399h) {
        if (abstractRunnableC1399h != null) {
            if (abstractRunnableC1399h.f3105e.mo1424a() == 1) {
                f3119e.decrementAndGet(this);
            }
        }
    }

    /* renamed from: e */
    public final int m1417e() {
        return this.producerIndex - this.consumerIndex;
    }

    /* renamed from: f */
    public final int m1416f() {
        return this.lastScheduledTask != null ? m1417e() + 1 : m1417e();
    }

    /* renamed from: g */
    public final void m1415g(C1395d c1395d) {
        AbstractRunnableC1399h abstractRunnableC1399h = (AbstractRunnableC1399h) f3116b.getAndSet(this, null);
        if (abstractRunnableC1399h != null) {
            c1395d.m1514a(abstractRunnableC1399h);
        }
        do {
        } while (m1412j(c1395d));
    }

    /* renamed from: h */
    public final AbstractRunnableC1399h m1414h() {
        AbstractRunnableC1399h abstractRunnableC1399h = (AbstractRunnableC1399h) f3116b.getAndSet(this, null);
        return abstractRunnableC1399h == null ? m1413i() : abstractRunnableC1399h;
    }

    /* renamed from: i */
    public final AbstractRunnableC1399h m1413i() {
        AbstractRunnableC1399h andSet;
        while (true) {
            int i = this.consumerIndex;
            if (i - this.producerIndex == 0) {
                return null;
            }
            int i2 = i & 127;
            if (f3118d.compareAndSet(this, i, i + 1) && (andSet = this.f3120a.getAndSet(i2, null)) != null) {
                m1418d(andSet);
                return andSet;
            }
        }
    }

    /* renamed from: j */
    public final boolean m1412j(C1395d c1395d) {
        AbstractRunnableC1399h m1413i = m1413i();
        if (m1413i == null) {
            return false;
        }
        c1395d.m1514a(m1413i);
        return true;
    }

    /* renamed from: k */
    public final long m1411k(C1406o c1406o) {
        int i = c1406o.consumerIndex;
        int i2 = c1406o.producerIndex;
        AtomicReferenceArray<AbstractRunnableC1399h> atomicReferenceArray = c1406o.f3120a;
        while (true) {
            if (i == i2) {
                break;
            }
            int i3 = i & 127;
            if (c1406o.blockingTasksInBuffer == 0) {
                break;
            }
            AbstractRunnableC1399h abstractRunnableC1399h = atomicReferenceArray.get(i3);
            if (abstractRunnableC1399h != null) {
                if ((abstractRunnableC1399h.f3105e.mo1424a() == 1) && C1405n.m1422a(atomicReferenceArray, i3, abstractRunnableC1399h, null)) {
                    f3119e.decrementAndGet(c1406o);
                    m1420b(this, abstractRunnableC1399h, false, 2, null);
                    return -1L;
                }
            }
            i++;
        }
        return m1409m(c1406o, true);
    }

    /* renamed from: l */
    public final long m1410l(C1406o c1406o) {
        AbstractRunnableC1399h m1413i = c1406o.m1413i();
        if (m1413i != null) {
            m1420b(this, m1413i, false, 2, null);
            return -1L;
        }
        return m1409m(c1406o, false);
    }

    /* renamed from: m */
    public final long m1409m(C1406o c1406o, boolean z) {
        AbstractRunnableC1399h abstractRunnableC1399h;
        do {
            abstractRunnableC1399h = (AbstractRunnableC1399h) c1406o.lastScheduledTask;
            if (abstractRunnableC1399h == null) {
                return -2L;
            }
            if (z) {
                if (!(abstractRunnableC1399h.f3105e.mo1424a() == 1)) {
                    return -2L;
                }
            }
            long mo1425a = C1403l.f3112e.mo1425a() - abstractRunnableC1399h.f3104d;
            long j = C1403l.f3108a;
            if (mo1425a < j) {
                return j - mo1425a;
            }
        } while (!C1679h.m602a(f3116b, c1406o, abstractRunnableC1399h, null));
        m1420b(this, abstractRunnableC1399h, false, 2, null);
        return -1L;
    }
}