package kotlinx.coroutines;

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

/* JADX INFO: loaded from: classes.dex */
public abstract class p0 extends q0 implements d0 {

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

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

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

    @Override // kotlinx.coroutines.t
    public final void L(kotlin.coroutines.h hVar, Runnable runnable) {
        U(runnable);
    }

    /* JADX WARN: Code restructure failed: missing block: B:36:0x0054, code lost:
    
        r7 = null;
     */
    /* JADX WARN: Removed duplicated region for block: B:80:0x00dc  */
    @Override // kotlinx.coroutines.q0
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long R() {
        Runnable runnable;
        o0 o0Var;
        n0 n0Var;
        n0 n0VarB;
        if (S()) {
            return 0L;
        }
        o0 o0Var2 = (o0) f29520h.get(this);
        if (o0Var2 != null && kotlinx.coroutines.internal.y.f29498b.get(o0Var2) != 0) {
            long jNanoTime = System.nanoTime();
            do {
                synchronized (o0Var2) {
                    n0[] n0VarArr = o0Var2.f29499a;
                    n0 n0Var2 = n0VarArr != null ? n0VarArr[0] : null;
                    n0VarB = n0Var2 == null ? null : (jNanoTime - n0Var2.f29511a < 0 || !V(n0Var2)) ? null : o0Var2.b(0);
                }
            } while (n0VarB != null);
        }
        loop1: while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29519g;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj == null) {
                break;
            }
            if (!(obj instanceof kotlinx.coroutines.internal.l)) {
                if (obj != y.f29584k) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, null)) {
                        if (atomicReferenceFieldUpdater.get(this) != obj) {
                            break;
                        }
                    }
                    runnable = (Runnable) obj;
                    break loop1;
                }
                break;
            }
            kotlinx.coroutines.internal.l lVar = (kotlinx.coroutines.internal.l) obj;
            Object objD = lVar.d();
            if (objD != kotlinx.coroutines.internal.l.f29478g) {
                runnable = (Runnable) objD;
                break;
            }
            kotlinx.coroutines.internal.l lVarC = lVar.c();
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVarC) && atomicReferenceFieldUpdater.get(this) == obj) {
            }
        }
        if (runnable != null) {
            runnable.run();
            return 0L;
        }
        kotlin.collections.h hVar = this.f29527e;
        if (((hVar == null || hVar.isEmpty()) ? Long.MAX_VALUE : 0L) == 0) {
            return 0L;
        }
        Object obj2 = f29519g.get(this);
        if (obj2 == null) {
            o0Var = (o0) f29520h.get(this);
            if (o0Var != null) {
                synchronized (o0Var) {
                    n0[] n0VarArr2 = o0Var.f29499a;
                    n0Var = n0VarArr2 != null ? n0VarArr2[0] : null;
                }
                if (n0Var != null) {
                    long jNanoTime2 = n0Var.f29511a - System.nanoTime();
                    if (jNanoTime2 < 0) {
                        return 0L;
                    }
                    return jNanoTime2;
                }
            }
        } else if (obj2 instanceof kotlinx.coroutines.internal.l) {
            long j10 = kotlinx.coroutines.internal.l.f29477f.get((kotlinx.coroutines.internal.l) obj2);
            if (((int) (1073741823 & j10)) != ((int) ((j10 & 1152921503533105152L) >> 30))) {
                return 0L;
            }
            o0Var = (o0) f29520h.get(this);
            if (o0Var != null) {
            }
        } else if (obj2 != y.f29584k) {
            return 0L;
        }
        return Long.MAX_VALUE;
    }

    public void U(Runnable runnable) {
        if (!V(runnable)) {
            z.f29593j.U(runnable);
            return;
        }
        Thread threadP = P();
        if (Thread.currentThread() != threadP) {
            LockSupport.unpark(threadP);
        }
    }

    public final boolean V(Runnable runnable) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29519g;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (f29521i.get(this) != 0) {
                return false;
            }
            if (obj == null) {
                while (!atomicReferenceFieldUpdater.compareAndSet(this, null, runnable)) {
                    if (atomicReferenceFieldUpdater.get(this) != null) {
                        break;
                    }
                }
                return true;
            }
            if (!(obj instanceof kotlinx.coroutines.internal.l)) {
                if (obj == y.f29584k) {
                    return false;
                }
                kotlinx.coroutines.internal.l lVar = new kotlinx.coroutines.internal.l(8, true);
                lVar.a((Runnable) obj);
                lVar.a(runnable);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVar)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return true;
            }
            kotlinx.coroutines.internal.l lVar2 = (kotlinx.coroutines.internal.l) obj;
            int iA = lVar2.a(runnable);
            if (iA == 0) {
                return true;
            }
            if (iA == 1) {
                kotlinx.coroutines.internal.l lVarC = lVar2.c();
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVarC) && atomicReferenceFieldUpdater.get(this) == obj) {
                }
            } else if (iA == 2) {
                return false;
            }
        }
    }

    public final boolean W() {
        kotlin.collections.h hVar = this.f29527e;
        if (hVar != null && !hVar.isEmpty()) {
            return false;
        }
        o0 o0Var = (o0) f29520h.get(this);
        if (o0Var != null && kotlinx.coroutines.internal.y.f29498b.get(o0Var) != 0) {
            return false;
        }
        Object obj = f29519g.get(this);
        if (obj != null) {
            if (obj instanceof kotlinx.coroutines.internal.l) {
                long j10 = kotlinx.coroutines.internal.l.f29477f.get((kotlinx.coroutines.internal.l) obj);
                if (((int) (1073741823 & j10)) != ((int) ((j10 & 1152921503533105152L) >> 30))) {
                    return false;
                }
            } else if (obj != y.f29584k) {
                return false;
            }
        }
        return true;
    }

    public final void X(long j10, n0 n0Var) {
        int iB;
        Thread threadP;
        boolean z10 = f29521i.get(this) != 0;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29520h;
        if (z10) {
            iB = 1;
        } else {
            o0 o0Var = (o0) atomicReferenceFieldUpdater.get(this);
            if (o0Var == null) {
                o0 o0Var2 = new o0();
                o0Var2.f29513c = j10;
                while (!atomicReferenceFieldUpdater.compareAndSet(this, null, o0Var2) && atomicReferenceFieldUpdater.get(this) == null) {
                }
                Object obj = atomicReferenceFieldUpdater.get(this);
                nb.g.f(obj);
                o0Var = (o0) obj;
            }
            iB = n0Var.b(j10, o0Var, this);
        }
        if (iB != 0) {
            if (iB == 1) {
                T(j10, n0Var);
                return;
            } else {
                if (iB != 2) {
                    throw new IllegalStateException("unexpected result".toString());
                }
                return;
            }
        }
        o0 o0Var3 = (o0) atomicReferenceFieldUpdater.get(this);
        if (o0Var3 != null) {
            synchronized (o0Var3) {
                n0[] n0VarArr = o0Var3.f29499a;
                n0Var = n0VarArr != null ? n0VarArr[0] : null;
            }
        }
        if (n0Var != n0Var || Thread.currentThread() == (threadP = P())) {
            return;
        }
        LockSupport.unpark(threadP);
    }

    @Override // kotlinx.coroutines.d0
    public j0 k(long j10, r1 r1Var, kotlin.coroutines.h hVar) {
        return a0.f29290a.k(j10, r1Var, hVar);
    }

    @Override // kotlinx.coroutines.q0
    public void shutdown() {
        n0 n0VarB;
        ThreadLocal threadLocal = q1.f29528a;
        q1.f29528a.set(null);
        f29521i.set(this, 1);
        loop0: while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29519g;
            Object obj = atomicReferenceFieldUpdater.get(this);
            la.e eVar = y.f29584k;
            if (obj != null) {
                if (!(obj instanceof kotlinx.coroutines.internal.l)) {
                    if (obj != eVar) {
                        kotlinx.coroutines.internal.l lVar = new kotlinx.coroutines.internal.l(8, true);
                        lVar.a((Runnable) obj);
                        while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, lVar)) {
                            if (atomicReferenceFieldUpdater.get(this) != obj) {
                                break;
                            }
                        }
                        break loop0;
                    }
                    break;
                }
                ((kotlinx.coroutines.internal.l) obj).b();
                break;
            }
            while (!atomicReferenceFieldUpdater.compareAndSet(this, null, eVar)) {
                if (atomicReferenceFieldUpdater.get(this) != null) {
                    break;
                }
            }
            break loop0;
        }
        while (R() <= 0) {
        }
        long jNanoTime = System.nanoTime();
        while (true) {
            o0 o0Var = (o0) f29520h.get(this);
            if (o0Var == null) {
                return;
            }
            synchronized (o0Var) {
                n0VarB = kotlinx.coroutines.internal.y.f29498b.get(o0Var) > 0 ? o0Var.b(0) : null;
            }
            if (n0VarB == null) {
                return;
            } else {
                T(jNanoTime, n0VarB);
            }
        }
    }
}
