package g.a;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: EventLoop.common.kt */
/* JADX INFO: loaded from: classes.dex */
public abstract class c1 extends d1 implements r0 {

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f4018j = AtomicReferenceFieldUpdater.newUpdater(c1.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 j<f.t> f4019g;

        /* JADX WARN: Multi-variable type inference failed */
        public a(long j2, j<? super f.t> jVar) {
            super(j2);
            this.f4019g = jVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            this.f4019g.b(c1.this, f.t.a);
        }

        @Override // g.a.c1.b
        public String toString() {
            return super.toString() + this.f4019g;
        }
    }

    /* JADX INFO: compiled from: EventLoop.common.kt */
    public static abstract class b implements Runnable, Comparable<b>, y0, g.a.n2.h0 {
        private volatile Object _heap;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public long f4021b;

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

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

        @Override // g.a.n2.h0
        public void a(int i2) {
            this.f4022f = i2;
        }

        @Override // g.a.n2.h0
        public void b(g.a.n2.g0<?> g0Var) {
            if (!(this._heap != f1.a)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            this._heap = g0Var;
        }

        @Override // g.a.n2.h0
        public int c() {
            return this.f4022f;
        }

        @Override // g.a.n2.h0
        public g.a.n2.g0<?> d() {
            Object obj = this._heap;
            if (obj instanceof g.a.n2.g0) {
                return (g.a.n2.g0) obj;
            }
            return null;
        }

        @Override // g.a.y0
        public final synchronized void dispose() {
            Object obj = this._heap;
            if (obj == f1.a) {
                return;
            }
            c cVar = obj instanceof c ? (c) obj : null;
            if (cVar != null) {
                cVar.g(this);
            }
            this._heap = f1.a;
        }

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

        public final synchronized int g(long j2, c cVar, c1 c1Var) {
            if (this._heap == f1.a) {
                return 2;
            }
            synchronized (cVar) {
                b bVarB = cVar.b();
                if (c1Var.b0()) {
                    return 1;
                }
                if (bVarB == null) {
                    cVar.f4023b = j2;
                } else {
                    long j3 = bVarB.f4021b;
                    if (j3 - j2 < 0) {
                        j2 = j3;
                    }
                    if (j2 - cVar.f4023b > 0) {
                        cVar.f4023b = j2;
                    }
                }
                if (this.f4021b - cVar.f4023b < 0) {
                    this.f4021b = cVar.f4023b;
                }
                cVar.a(this);
                return 0;
            }
        }

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

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

    /* JADX INFO: compiled from: EventLoop.common.kt */
    public static final class c extends g.a.n2.g0<b> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public long f4023b;

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

    private final void U() {
        if (l0.a() && !b0()) {
            throw new AssertionError();
        }
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                if (f4017i.compareAndSet(this, null, f1.f4029b)) {
                    return;
                }
            } else {
                if (obj instanceof g.a.n2.r) {
                    ((g.a.n2.r) obj).d();
                    return;
                }
                if (obj == f1.f4029b) {
                    return;
                }
                g.a.n2.r rVar = new g.a.n2.r(8, true);
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }");
                }
                rVar.a((Runnable) obj);
                if (f4017i.compareAndSet(this, obj, rVar)) {
                    return;
                }
            }
        }
    }

    private final Runnable V() {
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                return null;
            }
            if (obj instanceof g.a.n2.r) {
                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 }> }");
                }
                g.a.n2.r rVar = (g.a.n2.r) obj;
                Object objJ = rVar.j();
                if (objJ != g.a.n2.r.f4095h) {
                    return (Runnable) objJ;
                }
                f4017i.compareAndSet(this, obj, rVar.i());
            } else {
                if (obj == f1.f4029b) {
                    return null;
                }
                if (f4017i.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 Z(Runnable runnable) {
        while (true) {
            Object obj = this._queue;
            if (b0()) {
                return false;
            }
            if (obj == null) {
                if (f4017i.compareAndSet(this, null, runnable)) {
                    return true;
                }
            } else if (obj instanceof g.a.n2.r) {
                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 }> }");
                }
                g.a.n2.r rVar = (g.a.n2.r) obj;
                int iA = rVar.a(runnable);
                if (iA == 0) {
                    return true;
                }
                if (iA == 1) {
                    f4017i.compareAndSet(this, obj, rVar.i());
                } else if (iA == 2) {
                    return false;
                }
            } else {
                if (obj == f1.f4029b) {
                    return false;
                }
                g.a.n2.r rVar2 = new g.a.n2.r(8, true);
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type java.lang.Runnable{ kotlinx.coroutines.RunnableKt.Runnable }");
                }
                rVar2.a((Runnable) obj);
                rVar2.a(runnable);
                if (f4017i.compareAndSet(this, obj, rVar2)) {
                    return true;
                }
            }
        }
    }

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

    private final void i0() {
        b bVarI;
        g.a.b bVarA = g.a.c.a();
        long jA = bVarA != null ? bVarA.a() : System.nanoTime();
        while (true) {
            c cVar = (c) this._delayed;
            if (cVar == null || (bVarI = cVar.i()) == null) {
                return;
            } else {
                R(jA, bVarI);
            }
        }
    }

    private final int m0(long j2, b bVar) {
        if (b0()) {
            return 1;
        }
        c cVar = (c) this._delayed;
        if (cVar == null) {
            f4018j.compareAndSet(this, null, new c(j2));
            Object obj = this._delayed;
            f.z.d.k.c(obj);
            cVar = (c) obj;
        }
        return bVar.g(j2, cVar, this);
    }

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

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

    @Override // g.a.b1
    protected long F() {
        b bVarE;
        if (super.F() == 0) {
            return 0L;
        }
        Object obj = this._queue;
        if (obj != null) {
            if (!(obj instanceof g.a.n2.r)) {
                return obj == f1.f4029b ? Long.MAX_VALUE : 0L;
            }
            if (!((g.a.n2.r) obj).g()) {
                return 0L;
            }
        }
        c cVar = (c) this._delayed;
        if (cVar == null || (bVarE = cVar.e()) == null) {
            return Long.MAX_VALUE;
        }
        long j2 = bVarE.f4021b;
        g.a.b bVarA = g.a.c.a();
        return f.b0.n.b(j2 - (bVarA != null ? bVarA.a() : System.nanoTime()), 0L);
    }

    public void W(Runnable runnable) {
        if (Z(runnable)) {
            S();
        } else {
            n0.f4053k.W(runnable);
        }
    }

    protected boolean d0() {
        if (!N()) {
            return false;
        }
        c cVar = (c) this._delayed;
        if (cVar != null && !cVar.d()) {
            return false;
        }
        Object obj = this._queue;
        if (obj != null) {
            if (obj instanceof g.a.n2.r) {
                return ((g.a.n2.r) obj).g();
            }
            if (obj != f1.f4029b) {
                return false;
            }
        }
        return true;
    }

    public long g0() {
        b bVarH;
        if (P()) {
            return 0L;
        }
        c cVar = (c) this._delayed;
        if (cVar != null && !cVar.d()) {
            g.a.b bVarA = g.a.c.a();
            long jA = bVarA != null ? bVarA.a() : System.nanoTime();
            do {
                synchronized (cVar) {
                    b bVarB = cVar.b();
                    if (bVarB != null) {
                        b bVar = bVarB;
                        bVarH = bVar.h(jA) ? Z(bVar) : false ? cVar.h(0) : null;
                    }
                }
            } while (bVarH != null);
        }
        Runnable runnableV = V();
        if (runnableV == null) {
            return F();
        }
        runnableV.run();
        return 0L;
    }

    @Override // g.a.r0
    public void h(long j2, j<? super f.t> jVar) {
        long jC = f1.c(j2);
        if (jC < 4611686018427387903L) {
            g.a.b bVarA = g.a.c.a();
            long jA = bVarA != null ? bVarA.a() : System.nanoTime();
            a aVar = new a(jC + jA, jVar);
            l0(jA, aVar);
            l.a(jVar, aVar);
        }
    }

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

    public final void l0(long j2, b bVar) {
        int iM0 = m0(j2, bVar);
        if (iM0 == 0) {
            if (o0(bVar)) {
                S();
            }
        } else if (iM0 == 1) {
            R(j2, bVar);
        } else if (iM0 != 2) {
            throw new IllegalStateException("unexpected result".toString());
        }
    }

    @Override // g.a.b1
    public void shutdown() {
        i2.a.b();
        n0(true);
        U();
        while (g0() <= 0) {
        }
        i0();
    }

    @Override // g.a.b0
    public final void w(f.w.g gVar, Runnable runnable) {
        W(runnable);
    }
}
