package kotlinx.coroutines;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: EventLoop.common.kt */
/* JADX INFO: loaded from: classes.dex */
public abstract class g1 extends h1 implements w0 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f1080i = AtomicReferenceFieldUpdater.newUpdater(g1.class, Object.class, "_queue");

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f1081j = AtomicReferenceFieldUpdater.newUpdater(g1.class, Object.class, "_delayed");
    private volatile /* synthetic */ Object _queue = null;
    private volatile /* synthetic */ Object _delayed = null;
    private volatile /* synthetic */ int _isCompleted = 0;

    /* JADX INFO: compiled from: EventLoop.common.kt */
    private final class a extends b {

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private final n<h.r> f1082g;

        /* JADX WARN: Multi-variable type inference failed */
        public a(long j2, n<? super h.r> nVar) {
            super(j2);
            this.f1082g = nVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            this.f1082g.c(g1.this, h.r.a);
        }

        @Override // kotlinx.coroutines.g1.b
        public String toString() {
            return h.x.d.k.i(super.toString(), this.f1082g);
        }
    }

    /* JADX INFO: compiled from: EventLoop.common.kt */
    public static abstract class b implements Runnable, Comparable<b>, c1, kotlinx.coroutines.o2.w {
        public long b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private Object f1084e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private int f1085f = -1;

        public b(long j2) {
            this.b = j2;
        }

        @Override // kotlinx.coroutines.o2.w
        public void a(int i2) {
            this.f1085f = i2;
        }

        @Override // kotlinx.coroutines.o2.w
        public void b(kotlinx.coroutines.o2.v<?> vVar) {
            if (!(this.f1084e != j1.a)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            this.f1084e = vVar;
        }

        @Override // kotlinx.coroutines.o2.w
        public kotlinx.coroutines.o2.v<?> d() {
            Object obj = this.f1084e;
            if (obj instanceof kotlinx.coroutines.o2.v) {
                return (kotlinx.coroutines.o2.v) obj;
            }
            return null;
        }

        @Override // kotlinx.coroutines.c1
        public final synchronized void dispose() {
            Object obj = this.f1084e;
            if (obj == j1.a) {
                return;
            }
            c cVar = obj instanceof c ? (c) obj : null;
            if (cVar != null) {
                cVar.g(this);
            }
            this.f1084e = j1.a;
        }

        @Override // java.lang.Comparable
        /* JADX INFO: renamed from: e, reason: merged with bridge method [inline-methods] */
        public int compareTo(b bVar) {
            long j2 = this.b - bVar.b;
            if (j2 > 0) {
                return 1;
            }
            return j2 < 0 ? -1 : 0;
        }

        public final synchronized int f(long j2, c cVar, g1 g1Var) {
            if (this.f1084e == j1.a) {
                return 2;
            }
            synchronized (cVar) {
                b bVarB = cVar.b();
                if (g1Var.p0()) {
                    return 1;
                }
                if (bVarB == null) {
                    cVar.b = j2;
                } else {
                    long j3 = bVarB.b;
                    if (j3 - j2 < 0) {
                        j2 = j3;
                    }
                    if (j2 - cVar.b > 0) {
                        cVar.b = j2;
                    }
                }
                if (this.b - cVar.b < 0) {
                    this.b = cVar.b;
                }
                cVar.a(this);
                return 0;
            }
        }

        public final boolean g(long j2) {
            return j2 - this.b >= 0;
        }

        @Override // kotlinx.coroutines.o2.w
        public int getIndex() {
            return this.f1085f;
        }

        public String toString() {
            return "Delayed[nanos=" + this.b + ']';
        }
    }

    /* JADX INFO: compiled from: EventLoop.common.kt */
    public static final class c extends kotlinx.coroutines.o2.v<b> {
        public long b;

        public c(long j2) {
            this.b = j2;
        }
    }

    private final void j0() {
        if (r0.a() && !p0()) {
            throw new AssertionError();
        }
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                if (f1080i.compareAndSet(this, null, j1.b)) {
                    return;
                }
            } else {
                if (obj instanceof kotlinx.coroutines.o2.l) {
                    ((kotlinx.coroutines.o2.l) obj).d();
                    return;
                }
                if (obj == j1.b) {
                    return;
                }
                kotlinx.coroutines.o2.l lVar = new kotlinx.coroutines.o2.l(8, true);
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }");
                }
                lVar.a((Runnable) obj);
                if (f1080i.compareAndSet(this, obj, lVar)) {
                    return;
                }
            }
        }
    }

    private final Runnable l0() {
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                return null;
            }
            if (obj instanceof kotlinx.coroutines.o2.l) {
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeTaskQueueCore<java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }>{ kotlinx.coroutines.EventLoop_commonKt.Queue<java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }> }");
                }
                kotlinx.coroutines.o2.l lVar = (kotlinx.coroutines.o2.l) obj;
                Object objJ = lVar.j();
                if (objJ != kotlinx.coroutines.o2.l.f1116h) {
                    return (Runnable) objJ;
                }
                f1080i.compareAndSet(this, obj, lVar.i());
            } else {
                if (obj == j1.b) {
                    return null;
                }
                if (f1080i.compareAndSet(this, obj, null)) {
                    if (obj != null) {
                        return (Runnable) obj;
                    }
                    throw new NullPointerException("null cannot be cast to non-null type java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }");
                }
            }
        }
    }

    private final boolean o0(Runnable runnable) {
        while (true) {
            Object obj = this._queue;
            if (p0()) {
                return false;
            }
            if (obj == null) {
                if (f1080i.compareAndSet(this, null, runnable)) {
                    return true;
                }
            } else if (obj instanceof kotlinx.coroutines.o2.l) {
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeTaskQueueCore<java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }>{ kotlinx.coroutines.EventLoop_commonKt.Queue<java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }> }");
                }
                kotlinx.coroutines.o2.l lVar = (kotlinx.coroutines.o2.l) obj;
                int iA = lVar.a(runnable);
                if (iA == 0) {
                    return true;
                }
                if (iA == 1) {
                    f1080i.compareAndSet(this, obj, lVar.i());
                } else if (iA == 2) {
                    return false;
                }
            } else {
                if (obj == j1.b) {
                    return false;
                }
                kotlinx.coroutines.o2.l lVar2 = new kotlinx.coroutines.o2.l(8, true);
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }");
                }
                lVar2.a((Runnable) obj);
                lVar2.a(runnable);
                if (f1080i.compareAndSet(this, obj, lVar2)) {
                    return true;
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Type inference failed for: r0v0, types: [boolean, int] */
    public final boolean p0() {
        return this._isCompleted;
    }

    private final void r0() {
        d dVarA = e.a();
        long jNanoTime = dVarA == null ? System.nanoTime() : dVarA.a();
        while (true) {
            c cVar = (c) this._delayed;
            b bVarI = cVar == null ? null : cVar.i();
            if (bVarI == null) {
                return;
            } else {
                d0(jNanoTime, bVarI);
            }
        }
    }

    private final int u0(long j2, b bVar) {
        if (p0()) {
            return 1;
        }
        c cVar = (c) this._delayed;
        if (cVar == null) {
            f1081j.compareAndSet(this, null, new c(j2));
            Object obj = this._delayed;
            h.x.d.k.b(obj);
            cVar = (c) obj;
        }
        return bVar.f(j2, cVar, this);
    }

    private final void v0(boolean z) {
        this._isCompleted = z ? 1 : 0;
    }

    private final boolean w0(b bVar) {
        c cVar = (c) this._delayed;
        return (cVar == null ? null : cVar.e()) == bVar;
    }

    @Override // kotlinx.coroutines.i0
    public final void H(h.u.g gVar, Runnable runnable) {
        n0(runnable);
    }

    @Override // kotlinx.coroutines.f1
    protected long R() {
        if (super.R() == 0) {
            return 0L;
        }
        Object obj = this._queue;
        if (obj != null) {
            if (!(obj instanceof kotlinx.coroutines.o2.l)) {
                return obj == j1.b ? Long.MAX_VALUE : 0L;
            }
            if (!((kotlinx.coroutines.o2.l) obj).g()) {
                return 0L;
            }
        }
        c cVar = (c) this._delayed;
        b bVarE = cVar == null ? null : cVar.e();
        if (bVarE == null) {
            return Long.MAX_VALUE;
        }
        long j2 = bVarE.b;
        d dVarA = e.a();
        return h.z.n.c(j2 - (dVarA == null ? System.nanoTime() : dVarA.a()), 0L);
    }

    @Override // kotlinx.coroutines.f1
    public long W() {
        b bVarH;
        if (X()) {
            return 0L;
        }
        c cVar = (c) this._delayed;
        if (cVar != null && !cVar.d()) {
            d dVarA = e.a();
            long jNanoTime = dVarA == null ? System.nanoTime() : dVarA.a();
            do {
                synchronized (cVar) {
                    b bVarB = cVar.b();
                    if (bVarB != null) {
                        b bVar = bVarB;
                        bVarH = bVar.g(jNanoTime) ? o0(bVar) : false ? cVar.h(0) : null;
                    }
                }
            } while (bVarH != null);
        }
        Runnable runnableL0 = l0();
        if (runnableL0 == null) {
            return R();
        }
        runnableL0.run();
        return 0L;
    }

    @Override // kotlinx.coroutines.w0
    public void l(long j2, n<? super h.r> nVar) {
        long jC = j1.c(j2);
        if (jC < 4611686018427387903L) {
            d dVarA = e.a();
            long jNanoTime = dVarA == null ? System.nanoTime() : dVarA.a();
            a aVar = new a(jC + jNanoTime, nVar);
            p.a(nVar, aVar);
            t0(jNanoTime, aVar);
        }
    }

    public final void n0(Runnable runnable) {
        if (o0(runnable)) {
            g0();
        } else {
            t0.f1168k.n0(runnable);
        }
    }

    protected boolean q0() {
        if (!V()) {
            return false;
        }
        c cVar = (c) this._delayed;
        if (cVar != null && !cVar.d()) {
            return false;
        }
        Object obj = this._queue;
        if (obj != null) {
            if (obj instanceof kotlinx.coroutines.o2.l) {
                return ((kotlinx.coroutines.o2.l) obj).g();
            }
            if (obj != j1.b) {
                return false;
            }
        }
        return true;
    }

    protected final void s0() {
        this._queue = null;
        this._delayed = null;
    }

    @Override // kotlinx.coroutines.f1
    protected void shutdown() {
        j2.a.c();
        v0(true);
        j0();
        while (W() <= 0) {
        }
        r0();
    }

    public final void t0(long j2, b bVar) {
        int iU0 = u0(j2, bVar);
        if (iU0 == 0) {
            if (w0(bVar)) {
                g0();
            }
        } else if (iU0 == 1) {
            d0(j2, bVar);
        } else if (iU0 != 2) {
            throw new IllegalStateException("unexpected result".toString());
        }
    }
}
