package h.a;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.LockSupport;

/* JADX INFO: compiled from: EventLoop.common.kt */
/* JADX INFO: loaded from: classes.dex */
@g.i
public abstract class g0 extends h0 implements x {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f4221h = AtomicReferenceFieldUpdater.newUpdater(g0.class, Object.class, "_queue");

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f4222i = AtomicReferenceFieldUpdater.newUpdater(g0.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 */
    @g.i
    public static abstract class a implements Runnable, Comparable<a>, d0, h.a.k1.v {

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

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public int f4225f;

        @Override // h.a.k1.v
        public void a(h.a.k1.u<?> uVar) {
            if (!(this.f4224e != i0.a)) {
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
            this.f4224e = uVar;
        }

        @Override // h.a.k1.v
        public h.a.k1.u<?> c() {
            Object obj = this.f4224e;
            if (obj instanceof h.a.k1.u) {
                return (h.a.k1.u) obj;
            }
            return null;
        }

        @Override // java.lang.Comparable
        public int compareTo(a aVar) {
            long j2 = this.f4223b - aVar.f4223b;
            if (j2 > 0) {
                return 1;
            }
            return j2 < 0 ? -1 : 0;
        }

        @Override // h.a.k1.v
        public void d(int i2) {
            this.f4225f = i2;
        }

        @Override // h.a.d0
        public final synchronized void dispose() {
            Object obj = this.f4224e;
            h.a.k1.r rVar = i0.a;
            if (obj == rVar) {
                return;
            }
            b bVar = obj instanceof b ? (b) obj : null;
            if (bVar != null) {
                synchronized (bVar) {
                    if (c() != null) {
                        bVar.d(e());
                    }
                }
            }
            this.f4224e = rVar;
        }

        @Override // h.a.k1.v
        public int e() {
            return this.f4225f;
        }

        public String toString() {
            StringBuilder sbB = d.b.a.a.a.B("Delayed[nanos=");
            sbB.append(this.f4223b);
            sbB.append(']');
            return sbB.toString();
        }
    }

    /* JADX INFO: compiled from: EventLoop.common.kt */
    @g.i
    public static final class b extends h.a.k1.u<a> {

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

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

    @Override // h.a.f0
    public void A() {
        d1 d1Var = d1.a;
        d1.f4209b.set(null);
        this._isCompleted = 1;
        while (true) {
            Object obj = this._queue;
            if (obj == null) {
                if (f4221h.compareAndSet(this, null, i0.f4231b)) {
                    break;
                }
            } else if (obj instanceof h.a.k1.k) {
                ((h.a.k1.k) obj).b();
                break;
            } else {
                if (obj == i0.f4231b) {
                    break;
                }
                h.a.k1.k kVar = new h.a.k1.k(8, true);
                kVar.a((Runnable) obj);
                if (f4221h.compareAndSet(this, obj, kVar)) {
                    break;
                }
            }
        }
        while (N() <= 0) {
        }
        long jNanoTime = System.nanoTime();
        while (true) {
            b bVar = (b) this._delayed;
            a aVarE = bVar == null ? null : bVar.e();
            if (aVarE == null) {
                return;
            } else {
                E(jNanoTime, aVarE);
            }
        }
    }

    public void G(Runnable runnable) {
        if (!J(runnable)) {
            v.f4330j.G(runnable);
            return;
        }
        Thread threadD = D();
        if (Thread.currentThread() != threadD) {
            LockSupport.unpark(threadD);
        }
    }

    public final boolean J(Runnable runnable) {
        while (true) {
            Object obj = this._queue;
            if (this._isCompleted != 0) {
                return false;
            }
            if (obj == null) {
                if (f4221h.compareAndSet(this, null, runnable)) {
                    return true;
                }
            } else if (obj instanceof h.a.k1.k) {
                h.a.k1.k kVar = (h.a.k1.k) obj;
                int iA = kVar.a(runnable);
                if (iA == 0) {
                    return true;
                }
                if (iA == 1) {
                    f4221h.compareAndSet(this, obj, kVar.e());
                } else if (iA == 2) {
                    return false;
                }
            } else {
                if (obj == i0.f4231b) {
                    return false;
                }
                h.a.k1.k kVar2 = new h.a.k1.k(8, true);
                kVar2.a((Runnable) obj);
                kVar2.a(runnable);
                if (f4221h.compareAndSet(this, obj, kVar2)) {
                    return true;
                }
            }
        }
    }

    public boolean L() {
        h.a.k1.a<a0<?>> aVar = this.f4219g;
        if (!(aVar == null || aVar.f4241b == aVar.f4242c)) {
            return false;
        }
        b bVar = (b) this._delayed;
        if (bVar != null && !bVar.c()) {
            return false;
        }
        Object obj = this._queue;
        if (obj == null) {
            return true;
        }
        return obj instanceof h.a.k1.k ? ((h.a.k1.k) obj).d() : obj == i0.f4231b;
    }

    /* JADX WARN: Code restructure failed: missing block: B:35:0x0050, code lost:
    
        r7 = null;
     */
    /* JADX WARN: Removed duplicated region for block: B:59:0x0098  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x00b9  */
    /* JADX WARN: Removed duplicated region for block: B:81:0x00c5  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public long N() {
        /*
            Method dump skipped, instruction units count: 220
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: h.a.g0.N():long");
    }

    public final void O() {
        this._queue = null;
        this._delayed = null;
    }

    public final void P(long j2, a aVar) {
        char c2;
        Thread threadD;
        a aVarB;
        a aVar2 = null;
        if (this._isCompleted == 0) {
            b bVar = (b) this._delayed;
            if (bVar == null) {
                f4222i.compareAndSet(this, null, new b(j2));
                Object obj = this._delayed;
                g.h0.c.l.c(obj);
                bVar = (b) obj;
            }
            synchronized (aVar) {
                if (aVar.f4224e == i0.a) {
                    c2 = 2;
                } else {
                    synchronized (bVar) {
                        a aVarB2 = bVar.b();
                        if (this._isCompleted != 0) {
                            c2 = 1;
                        } else {
                            if (aVarB2 == null) {
                                bVar.f4226b = j2;
                            } else {
                                long j3 = aVarB2.f4223b;
                                if (j3 - j2 >= 0) {
                                    j3 = j2;
                                }
                                if (j3 - bVar.f4226b > 0) {
                                    bVar.f4226b = j3;
                                }
                            }
                            long j4 = aVar.f4223b;
                            long j5 = bVar.f4226b;
                            if (j4 - j5 < 0) {
                                aVar.f4223b = j5;
                            }
                            bVar.a(aVar);
                            c2 = 0;
                        }
                    }
                }
            }
        } else {
            c2 = 1;
        }
        if (c2 != 0) {
            if (c2 == 1) {
                E(j2, aVar);
                return;
            } else {
                if (c2 != 2) {
                    throw new IllegalStateException("unexpected result".toString());
                }
                return;
            }
        }
        b bVar2 = (b) this._delayed;
        if (bVar2 != null) {
            synchronized (bVar2) {
                aVarB = bVar2.b();
            }
            aVar2 = aVarB;
        }
        if (!(aVar2 == aVar) || Thread.currentThread() == (threadD = D())) {
            return;
        }
        LockSupport.unpark(threadD);
    }

    @Override // h.a.q
    public final void e(g.e0.f fVar, Runnable runnable) {
        G(runnable);
    }
}
