package V4;

import I2.C0317a;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.LockSupport;
import x4.C3249o;

/* JADX INFO: loaded from: classes3.dex */
public abstract class X extends Y implements J {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f8272f = AtomicReferenceFieldUpdater.newUpdater(X.class, Object.class, "_queue$volatile");

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f8273j = AtomicReferenceFieldUpdater.newUpdater(X.class, Object.class, "_delayed$volatile");

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f8274k = AtomicIntegerFieldUpdater.newUpdater(X.class, "_isCompleted$volatile");
    private volatile /* synthetic */ Object _delayed$volatile;
    private volatile /* synthetic */ int _isCompleted$volatile = 0;
    private volatile /* synthetic */ Object _queue$volatile;

    /* JADX WARN: Code restructure failed: missing block: B:8:0x0018, code lost:
    
        r7 = null;
     */
    @Override // V4.Y
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long B() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        C0317a c0317a;
        Runnable runnable;
        V v3;
        if (!C()) {
            F();
            loop0: while (true) {
                atomicReferenceFieldUpdater = f8272f;
                Object obj = atomicReferenceFieldUpdater.get(this);
                c0317a = E.f8232c;
                if (obj == null) {
                    break;
                }
                if (!(obj instanceof a5.l)) {
                    if (obj != c0317a) {
                        while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, null)) {
                            if (atomicReferenceFieldUpdater.get(this) != obj) {
                                break;
                            }
                        }
                        runnable = (Runnable) obj;
                        break loop0;
                    }
                    break;
                }
                a5.l lVar = (a5.l) obj;
                Object objD = lVar.d();
                if (objD != a5.l.f10840g) {
                    runnable = (Runnable) objD;
                    break;
                }
                a5.l lVarC = lVar.c();
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVarC) && atomicReferenceFieldUpdater.get(this) == obj) {
                }
            }
            if (runnable != null) {
                runnable.run();
                return 0L;
            }
            C3249o c3249o = this.f8278d;
            if (((c3249o == null || c3249o.isEmpty()) ? Long.MAX_VALUE : 0L) != 0) {
                Object obj2 = atomicReferenceFieldUpdater.get(this);
                if (obj2 != null) {
                    if (obj2 instanceof a5.l) {
                        long j4 = a5.l.f10839f.get((a5.l) obj2);
                        if (((int) (1073741823 & j4)) != ((int) ((j4 & 1152921503533105152L) >> 30))) {
                            return 0L;
                        }
                    } else if (obj2 == c0317a) {
                        return Long.MAX_VALUE;
                    }
                }
                W w6 = (W) f8273j.get(this);
                if (w6 != null) {
                    synchronized (w6) {
                        V[] vArr = w6.f10854a;
                        v3 = vArr != null ? vArr[0] : null;
                    }
                    if (v3 != null) {
                        long jNanoTime = v3.f8269a - System.nanoTime();
                        if (jNanoTime >= 0) {
                            return jNanoTime;
                        }
                    }
                }
                return Long.MAX_VALUE;
            }
        }
        return 0L;
    }

    public void E(Runnable runnable) {
        F();
        if (!G(runnable)) {
            F.l.E(runnable);
            return;
        }
        Thread threadZ = z();
        if (Thread.currentThread() != threadZ) {
            LockSupport.unpark(threadZ);
        }
    }

    public final void F() {
        V vB;
        W w6 = (W) f8273j.get(this);
        if (w6 == null || a5.u.f10853b.get(w6) == 0) {
            return;
        }
        long jNanoTime = System.nanoTime();
        do {
            synchronized (w6) {
                try {
                    V[] vArr = w6.f10854a;
                    V v3 = vArr != null ? vArr[0] : null;
                    if (v3 != null) {
                        vB = ((jNanoTime - v3.f8269a) > 0L ? 1 : ((jNanoTime - v3.f8269a) == 0L ? 0 : -1)) >= 0 ? G(v3) : false ? w6.b(0) : null;
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        } while (vB != null);
    }

    /* JADX WARN: Code restructure failed: missing block: B:32:0x0050, code lost:
    
        return false;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean G(Runnable runnable) {
        loop0: while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8272f;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (!(f8274k.get(this) != 0)) {
                if (obj != null) {
                    if (!(obj instanceof a5.l)) {
                        if (obj != E.f8232c) {
                            a5.l lVar = new a5.l(8, true);
                            lVar.a((Runnable) obj);
                            lVar.a(runnable);
                            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVar)) {
                                if (atomicReferenceFieldUpdater.get(this) != obj) {
                                    break;
                                }
                            }
                            break loop0;
                        }
                        break;
                    }
                    a5.l lVar2 = (a5.l) obj;
                    int iA = lVar2.a(runnable);
                    if (iA == 0) {
                        break;
                    }
                    if (iA == 1) {
                        a5.l lVarC = lVar2.c();
                        while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVarC) && atomicReferenceFieldUpdater.get(this) == obj) {
                        }
                    } else if (iA == 2) {
                        break;
                    }
                } else {
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, null, runnable)) {
                        if (atomicReferenceFieldUpdater.get(this) != null) {
                            break;
                        }
                    }
                    break loop0;
                }
            } else {
                break;
            }
        }
        return true;
    }

    public final boolean H() {
        W w6;
        C3249o c3249o = this.f8278d;
        if (!(c3249o != null ? c3249o.isEmpty() : true) || ((w6 = (W) f8273j.get(this)) != null && a5.u.f10853b.get(w6) != 0)) {
            return false;
        }
        Object obj = f8272f.get(this);
        if (obj != null) {
            if (obj instanceof a5.l) {
                long j4 = a5.l.f10839f.get((a5.l) obj);
                return ((int) (1073741823 & j4)) == ((int) ((j4 & 1152921503533105152L) >> 30));
            }
            if (obj != E.f8232c) {
                return false;
            }
        }
        return true;
    }

    public final void I(long j4, V v3) {
        int iB;
        Thread threadZ;
        boolean z6 = f8274k.get(this) != 0;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8273j;
        if (z6) {
            iB = 1;
        } else {
            W w6 = (W) atomicReferenceFieldUpdater.get(this);
            if (w6 == null) {
                W w7 = new W();
                w7.f8271c = j4;
                while (!atomicReferenceFieldUpdater.compareAndSet(this, null, w7) && atomicReferenceFieldUpdater.get(this) == null) {
                }
                Object obj = atomicReferenceFieldUpdater.get(this);
                kotlin.jvm.internal.o.e(obj);
                w6 = (W) obj;
            }
            iB = v3.b(j4, w6, this);
        }
        if (iB != 0) {
            if (iB == 1) {
                D(j4, v3);
                return;
            } else {
                if (iB != 2) {
                    throw new IllegalStateException("unexpected result");
                }
                return;
            }
        }
        W w8 = (W) atomicReferenceFieldUpdater.get(this);
        if (w8 != null) {
            synchronized (w8) {
                V[] vArr = w8.f10854a;
                v = vArr != null ? vArr[0] : null;
            }
        }
        if (v != v3 || Thread.currentThread() == (threadZ = z())) {
            return;
        }
        LockSupport.unpark(threadZ);
    }

    @Override // V4.J
    public final void a(long j4, C0772m c0772m) {
        long j6 = j4 > 0 ? j4 >= 9223372036854L ? Long.MAX_VALUE : 1000000 * j4 : 0L;
        if (j6 < 4611686018427387903L) {
            long jNanoTime = System.nanoTime();
            T t6 = new T(this, j6 + jNanoTime, c0772m);
            I(jNanoTime, t6);
            c0772m.v(new C0766j(t6, 2));
        }
    }

    public P b(long j4, I0 i02, A4.j jVar) {
        return G.f8242a.b(j4, i02, jVar);
    }

    @Override // V4.AbstractC0786y
    public final void c(A4.j jVar, Runnable runnable) {
        E(runnable);
    }

    @Override // V4.Y
    public void shutdown() {
        V vB;
        F0.f8241a.set(null);
        f8274k.set(this, 1);
        loop0: while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8272f;
            Object obj = atomicReferenceFieldUpdater.get(this);
            C0317a c0317a = E.f8232c;
            if (obj != null) {
                if (!(obj instanceof a5.l)) {
                    if (obj != c0317a) {
                        a5.l lVar = new a5.l(8, true);
                        lVar.a((Runnable) obj);
                        while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVar)) {
                            if (atomicReferenceFieldUpdater.get(this) != obj) {
                                break;
                            }
                        }
                        break loop0;
                    }
                    break;
                }
                ((a5.l) obj).b();
                break;
            }
            while (!atomicReferenceFieldUpdater.compareAndSet(this, null, c0317a)) {
                if (atomicReferenceFieldUpdater.get(this) != null) {
                    break;
                }
            }
            break loop0;
        }
        while (B() <= 0) {
        }
        long jNanoTime = System.nanoTime();
        while (true) {
            W w6 = (W) f8273j.get(this);
            if (w6 == null) {
                return;
            }
            synchronized (w6) {
                vB = a5.u.f10853b.get(w6) > 0 ? w6.b(0) : null;
            }
            if (vB == null) {
                return;
            } else {
                D(jNanoTime, vB);
            }
        }
    }
}
