package h.a.l1;

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

/* JADX INFO: compiled from: WorkQueue.kt */
/* JADX INFO: loaded from: classes.dex */
@g.i
public final class n {
    public static final /* synthetic */ AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater.newUpdater(n.class, Object.class, "lastScheduledTask");

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final AtomicReferenceArray<h> f4316e = 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;

    public final h a(h hVar, boolean z) {
        if (z) {
            return b(hVar);
        }
        h hVar2 = (h) a.getAndSet(this, hVar);
        if (hVar2 == null) {
            return null;
        }
        return b(hVar2);
    }

    public final h b(h hVar) {
        if (hVar.f4304e.b() == 1) {
            f4315d.incrementAndGet(this);
        }
        if (c() == 127) {
            return hVar;
        }
        int i2 = this.producerIndex & 127;
        while (this.f4316e.get(i2) != null) {
            Thread.yield();
        }
        this.f4316e.lazySet(i2, hVar);
        f4313b.incrementAndGet(this);
        return null;
    }

    public final int c() {
        return this.producerIndex - this.consumerIndex;
    }

    public final int d() {
        return this.lastScheduledTask != null ? c() + 1 : c();
    }

    public final h e() {
        h hVar = (h) a.getAndSet(this, null);
        return hVar == null ? f() : hVar;
    }

    public final h f() {
        h andSet;
        while (true) {
            int i2 = this.consumerIndex;
            if (i2 - this.producerIndex == 0) {
                return null;
            }
            int i3 = i2 & 127;
            if (f4314c.compareAndSet(this, i2, i2 + 1) && (andSet = this.f4316e.getAndSet(i3, null)) != null) {
                if (andSet.f4304e.b() == 1) {
                    f4315d.decrementAndGet(this);
                }
                return andSet;
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:21:0x003e, code lost:
    
        return h(r9, true);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final long g(h.a.l1.n r9) {
        /*
            r8 = this;
            int r0 = r9.consumerIndex
            int r1 = r9.producerIndex
            java.util.concurrent.atomic.AtomicReferenceArray<h.a.l1.h> r2 = r9.f4316e
        L6:
            r3 = 1
            if (r0 == r1) goto L3a
            r4 = r0 & 127(0x7f, float:1.78E-43)
            int r5 = r9.blockingTasksInBuffer
            if (r5 != 0) goto L10
            goto L3a
        L10:
            java.lang.Object r5 = r2.get(r4)
            h.a.l1.h r5 = (h.a.l1.h) r5
            if (r5 == 0) goto L37
            h.a.l1.i r6 = r5.f4304e
            int r6 = r6.b()
            r7 = 0
            if (r6 != r3) goto L22
            goto L23
        L22:
            r3 = r7
        L23:
            if (r3 == 0) goto L37
            r3 = 0
            boolean r3 = r2.compareAndSet(r4, r5, r3)
            if (r3 == 0) goto L37
            java.util.concurrent.atomic.AtomicIntegerFieldUpdater r0 = h.a.l1.n.f4315d
            r0.decrementAndGet(r9)
            r8.a(r5, r7)
            r0 = -1
            return r0
        L37:
            int r0 = r0 + 1
            goto L6
        L3a:
            long r0 = r8.h(r9, r3)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: h.a.l1.n.g(h.a.l1.n):long");
    }

    public final long h(n nVar, boolean z) {
        h hVar;
        do {
            hVar = (h) nVar.lastScheduledTask;
            if (hVar == null) {
                return -2L;
            }
            if (z) {
                if (!(hVar.f4304e.b() == 1)) {
                    return -2L;
                }
            }
            long jA = l.f4309e.a() - hVar.f4303b;
            long j2 = l.a;
            if (jA < j2) {
                return j2 - jA;
            }
        } while (!a.compareAndSet(nVar, hVar, null));
        a(hVar, false);
        return -1L;
    }
}
