package d2;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: EventLoop.common.kt */
/* JADX INFO: loaded from: classes.dex */
public abstract class p0 extends q0 implements g0 {

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f4519j = AtomicReferenceFieldUpdater.newUpdater(p0.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 */
    public static abstract class a implements Runnable, Comparable<a>, m0, kotlinx.coroutines.internal.h0 {

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

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

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private int f4522g;

        @Override // kotlinx.coroutines.internal.h0
        public void a(int i3) {
            this.f4522g = i3;
        }

        @Override // d2.m0
        public final synchronized void b() {
            Object obj = this.f4521f;
            if (obj == s0.f4527a) {
                return;
            }
            b bVar = obj instanceof b ? (b) obj : null;
            if (bVar != null) {
                bVar.g(this);
            }
            this.f4521f = s0.f4527a;
        }

        @Override // kotlinx.coroutines.internal.h0
        public void c(kotlinx.coroutines.internal.g0<?> g0Var) {
            if (!(this.f4521f != s0.f4527a)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            this.f4521f = g0Var;
        }

        @Override // kotlinx.coroutines.internal.h0
        public kotlinx.coroutines.internal.g0<?> e() {
            Object obj = this.f4521f;
            if (obj instanceof kotlinx.coroutines.internal.g0) {
                return (kotlinx.coroutines.internal.g0) obj;
            }
            return null;
        }

        @Override // kotlinx.coroutines.internal.h0
        public int f() {
            return this.f4522g;
        }

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

        public final synchronized int h(long j3, b bVar, p0 p0Var) {
            if (this.f4521f == s0.f4527a) {
                return 2;
            }
            synchronized (bVar) {
                a aVarB = bVar.b();
                if (p0Var.F()) {
                    return 1;
                }
                if (aVarB == null) {
                    bVar.f4523b = j3;
                } else {
                    long j4 = aVarB.f4520e;
                    if (j4 - j3 < 0) {
                        j3 = j4;
                    }
                    if (j3 - bVar.f4523b > 0) {
                        bVar.f4523b = j3;
                    }
                }
                long j5 = this.f4520e;
                long j6 = bVar.f4523b;
                if (j5 - j6 < 0) {
                    this.f4520e = j6;
                }
                bVar.a(this);
                return 0;
            }
        }

        public final boolean i(long j3) {
            return j3 - this.f4520e >= 0;
        }

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

    /* JADX INFO: compiled from: EventLoop.common.kt */
    public static final class b extends kotlinx.coroutines.internal.g0<a> {

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

        public b(long j3) {
            this.f4523b = j3;
        }
    }

    private final void B() {
        if (c0.a() && !F()) {
            throw new AssertionError();
        }
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                if (f4518i.compareAndSet(this, null, s0.f4528b)) {
                    return;
                }
            } else if (obj instanceof kotlinx.coroutines.internal.q) {
                ((kotlinx.coroutines.internal.q) obj).d();
                return;
            } else {
                if (obj == s0.f4528b) {
                    return;
                }
                kotlinx.coroutines.internal.q qVar = new kotlinx.coroutines.internal.q(8, true);
                qVar.a((Runnable) obj);
                if (f4518i.compareAndSet(this, obj, qVar)) {
                    return;
                }
            }
        }
    }

    private final Runnable C() {
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                return null;
            }
            if (obj instanceof kotlinx.coroutines.internal.q) {
                kotlinx.coroutines.internal.q qVar = (kotlinx.coroutines.internal.q) obj;
                Object objJ = qVar.j();
                if (objJ != kotlinx.coroutines.internal.q.f5228h) {
                    return (Runnable) objJ;
                }
                f4518i.compareAndSet(this, obj, qVar.i());
            } else {
                if (obj == s0.f4528b) {
                    return null;
                }
                if (f4518i.compareAndSet(this, obj, null)) {
                    return (Runnable) obj;
                }
            }
        }
    }

    private final boolean E(Runnable runnable) {
        while (true) {
            Object obj = this._queue;
            if (F()) {
                return false;
            }
            if (obj == null) {
                if (f4518i.compareAndSet(this, null, runnable)) {
                    return true;
                }
            } else if (obj instanceof kotlinx.coroutines.internal.q) {
                kotlinx.coroutines.internal.q qVar = (kotlinx.coroutines.internal.q) obj;
                int iA = qVar.a(runnable);
                if (iA == 0) {
                    return true;
                }
                if (iA == 1) {
                    f4518i.compareAndSet(this, obj, qVar.i());
                } else if (iA == 2) {
                    return false;
                }
            } else {
                if (obj == s0.f4528b) {
                    return false;
                }
                kotlinx.coroutines.internal.q qVar2 = new kotlinx.coroutines.internal.q(8, true);
                qVar2.a((Runnable) obj);
                qVar2.a(runnable);
                if (f4518i.compareAndSet(this, obj, qVar2)) {
                    return true;
                }
            }
        }
    }

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

    private final void I() {
        c.a();
        long jNanoTime = System.nanoTime();
        while (true) {
            b bVar = (b) this._delayed;
            a aVarI = bVar == null ? null : bVar.i();
            if (aVarI == null) {
                return;
            } else {
                y(jNanoTime, aVarI);
            }
        }
    }

    private final int L(long j3, a aVar) {
        if (F()) {
            return 1;
        }
        b bVar = (b) this._delayed;
        if (bVar == null) {
            f4519j.compareAndSet(this, null, new b(j3));
            Object obj = this._delayed;
            w1.g.b(obj);
            bVar = (b) obj;
        }
        return aVar.h(j3, bVar, this);
    }

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

    private final boolean N(a aVar) {
        b bVar = (b) this._delayed;
        return (bVar == null ? null : bVar.e()) == aVar;
    }

    public void D(Runnable runnable) {
        if (E(runnable)) {
            z();
        } else {
            e0.f4471k.D(runnable);
        }
    }

    protected boolean G() {
        if (!u()) {
            return false;
        }
        b bVar = (b) this._delayed;
        if (bVar != null && !bVar.d()) {
            return false;
        }
        Object obj = this._queue;
        if (obj != null) {
            if (obj instanceof kotlinx.coroutines.internal.q) {
                return ((kotlinx.coroutines.internal.q) obj).g();
            }
            if (obj != s0.f4528b) {
                return false;
            }
        }
        return true;
    }

    public long H() {
        a aVarH;
        if (v()) {
            return 0L;
        }
        b bVar = (b) this._delayed;
        if (bVar != null && !bVar.d()) {
            c.a();
            long jNanoTime = System.nanoTime();
            do {
                synchronized (bVar) {
                    a aVarB = bVar.b();
                    if (aVarB != null) {
                        a aVar = aVarB;
                        aVarH = aVar.i(jNanoTime) ? E(aVar) : false ? bVar.h(0) : null;
                    }
                }
            } while (aVarH != null);
        }
        Runnable runnableC = C();
        if (runnableC == null) {
            return q();
        }
        runnableC.run();
        return 0L;
    }

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

    public final void K(long j3, a aVar) {
        int iL = L(j3, aVar);
        if (iL == 0) {
            if (N(aVar)) {
                z();
            }
        } else if (iL == 1) {
            y(j3, aVar);
        } else if (iL != 2) {
            throw new IllegalStateException("unexpected result".toString());
        }
    }

    @Override // d2.u
    public final void d(n1.f fVar, Runnable runnable) {
        D(runnable);
    }

    @Override // d2.o0
    protected long q() {
        if (super.q() == 0) {
            return 0L;
        }
        Object obj = this._queue;
        if (obj != null) {
            if (!(obj instanceof kotlinx.coroutines.internal.q)) {
                return obj == s0.f4528b ? Long.MAX_VALUE : 0L;
            }
            if (!((kotlinx.coroutines.internal.q) obj).g()) {
                return 0L;
            }
        }
        b bVar = (b) this._delayed;
        a aVarE = bVar == null ? null : bVar.e();
        if (aVarE == null) {
            return Long.MAX_VALUE;
        }
        long j3 = aVarE.f4520e;
        c.a();
        return z1.f.b(j3 - System.nanoTime(), 0L);
    }

    @Override // d2.o0
    public void w() {
        n1.f4510a.b();
        M(true);
        B();
        while (H() <= 0) {
        }
        I();
    }
}
