package V4;

import I2.C0317a;
import a.AbstractC0872a;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import n2.AbstractC2613d;
import w4.C3137A;

/* JADX INFO: loaded from: classes3.dex */
public class u0 implements InterfaceC0765i0, r, A0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f8337a = AtomicReferenceFieldUpdater.newUpdater(u0.class, Object.class, "_state$volatile");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f8338b = AtomicReferenceFieldUpdater.newUpdater(u0.class, Object.class, "_parentHandle$volatile");
    private volatile /* synthetic */ Object _parentHandle$volatile;
    private volatile /* synthetic */ Object _state$volatile;

    public u0(boolean z6) {
        this._state$volatile = z6 ? E.f8239j : E.f8238i;
    }

    public static C0779q J(a5.i iVar) {
        while (iVar.g()) {
            a5.i iVarD = iVar.d();
            if (iVarD == null) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = a5.i.f10834b;
                Object obj = atomicReferenceFieldUpdater.get(iVar);
                while (true) {
                    iVar = (a5.i) obj;
                    if (!iVar.g()) {
                        break;
                    }
                    obj = atomicReferenceFieldUpdater.get(iVar);
                }
            } else {
                iVar = iVarD;
            }
        }
        while (true) {
            iVar = iVar.f();
            if (!iVar.g()) {
                if (iVar instanceof C0779q) {
                    return (C0779q) iVar;
                }
                if (iVar instanceof w0) {
                    return null;
                }
            }
        }
    }

    public static String Q(Object obj) {
        if (!(obj instanceof C0777o0)) {
            return obj instanceof InterfaceC0755d0 ? ((InterfaceC0755d0) obj).isActive() ? "Active" : "New" : obj instanceof C0784w ? "Cancelled" : "Completed";
        }
        C0777o0 c0777o0 = (C0777o0) obj;
        return c0777o0.d() ? "Cancelling" : C0777o0.f8324b.get(c0777o0) != 0 ? "Completing" : "Active";
    }

    public static CancellationException R(u0 u0Var, Throwable th) {
        u0Var.getClass();
        CancellationException cancellationException = th instanceof CancellationException ? (CancellationException) th : null;
        return cancellationException == null ? new C0767j0(u0Var.q(), th, u0Var) : cancellationException;
    }

    public boolean A(Throwable th) {
        return false;
    }

    public final void C(InterfaceC0765i0 interfaceC0765i0) {
        y0 y0Var = y0.f8351a;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8338b;
        if (interfaceC0765i0 == null) {
            atomicReferenceFieldUpdater.set(this, y0Var);
            return;
        }
        interfaceC0765i0.start();
        InterfaceC0778p interfaceC0778pAttachChild = interfaceC0765i0.attachChild(this);
        atomicReferenceFieldUpdater.set(this, interfaceC0778pAttachChild);
        if (E()) {
            interfaceC0778pAttachChild.dispose();
            atomicReferenceFieldUpdater.set(this, y0Var);
        }
    }

    public final P D(boolean z6, AbstractC0771l0 abstractC0771l0) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        y0 y0Var;
        boolean z7;
        boolean zC;
        abstractC0771l0.f8311d = this;
        loop0: while (true) {
            atomicReferenceFieldUpdater = f8337a;
            Object obj = atomicReferenceFieldUpdater.get(this);
            boolean z8 = obj instanceof S;
            y0Var = y0.f8351a;
            z7 = true;
            if (!z8) {
                if (!(obj instanceof InterfaceC0755d0)) {
                    z7 = false;
                    break;
                }
                InterfaceC0755d0 interfaceC0755d0 = (InterfaceC0755d0) obj;
                w0 w0VarB = interfaceC0755d0.b();
                if (w0VarB == null) {
                    kotlin.jvm.internal.o.f(obj, "null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    O((AbstractC0771l0) obj);
                } else {
                    if (abstractC0771l0.i()) {
                        C0777o0 c0777o0 = interfaceC0755d0 instanceof C0777o0 ? (C0777o0) interfaceC0755d0 : null;
                        Throwable thC = c0777o0 != null ? c0777o0.c() : null;
                        if (thC == null) {
                            zC = w0VarB.c(abstractC0771l0, 5);
                        } else if (z6) {
                            abstractC0771l0.j(thC);
                            return y0Var;
                        }
                    } else {
                        zC = w0VarB.c(abstractC0771l0, 1);
                    }
                    if (zC) {
                        break;
                    }
                }
            } else {
                S s6 = (S) obj;
                if (s6.f8265a) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, abstractC0771l0)) {
                        if (atomicReferenceFieldUpdater.get(this) != obj) {
                            break;
                        }
                    }
                    break loop0;
                }
                N(s6);
            }
        }
        if (z7) {
            return abstractC0771l0;
        }
        if (z6) {
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            C0784w c0784w = obj2 instanceof C0784w ? (C0784w) obj2 : null;
            abstractC0771l0.j(c0784w != null ? c0784w.f8346a : null);
        }
        return y0Var;
    }

    public final boolean E() {
        return !(f8337a.get(this) instanceof InterfaceC0755d0);
    }

    public boolean F() {
        return this instanceof C0762h;
    }

    public final boolean G(Object obj) {
        Object objS;
        do {
            objS = S(f8337a.get(this), obj);
            if (objS == E.f8233d) {
                return false;
            }
            if (objS == E.f8234e) {
                return true;
            }
        } while (objS == E.f8235f);
        h(objS);
        return true;
    }

    public final Object H(Object obj) {
        Object objS;
        do {
            objS = S(f8337a.get(this), obj);
            if (objS == E.f8233d) {
                String str = "Job " + this + " is already complete or completing, but is being completed with " + obj;
                C0784w c0784w = obj instanceof C0784w ? (C0784w) obj : null;
                throw new IllegalStateException(str, c0784w != null ? c0784w.f8346a : null);
            }
        } while (objS == E.f8235f);
        return objS;
    }

    public String I() {
        return getClass().getSimpleName();
    }

    public final void K(w0 w0Var, Throwable th) {
        w0Var.c(new a5.h(4), 4);
        Object obj = a5.i.f10833a.get(w0Var);
        kotlin.jvm.internal.o.f(obj, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode");
        E3.v vVar = null;
        for (a5.i iVarF = (a5.i) obj; !iVarF.equals(w0Var); iVarF = iVarF.f()) {
            if ((iVarF instanceof AbstractC0771l0) && ((AbstractC0771l0) iVarF).i()) {
                try {
                    ((AbstractC0771l0) iVarF).j(th);
                } catch (Throwable th2) {
                    if (vVar != null) {
                        AbstractC2613d.b(vVar, th2);
                    } else {
                        vVar = new E3.v("Exception in completion handler " + iVarF + " for " + this, th2);
                    }
                }
            }
        }
        if (vVar != null) {
            B(vVar);
        }
        p(th);
    }

    public final void N(S s6) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        w0 w0Var = new w0();
        Object c0753c0 = w0Var;
        if (!s6.f8265a) {
            c0753c0 = new C0753c0(w0Var);
        }
        do {
            atomicReferenceFieldUpdater = f8337a;
            if (atomicReferenceFieldUpdater.compareAndSet(this, s6, c0753c0)) {
                return;
            }
        } while (atomicReferenceFieldUpdater.get(this) == s6);
    }

    public final void O(AbstractC0771l0 abstractC0771l0) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        w0 w0Var = new w0();
        abstractC0771l0.getClass();
        a5.i.f10834b.set(w0Var, abstractC0771l0);
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = a5.i.f10833a;
        atomicReferenceFieldUpdater2.set(w0Var, abstractC0771l0);
        loop0: while (true) {
            if (atomicReferenceFieldUpdater2.get(abstractC0771l0) == abstractC0771l0) {
                while (!atomicReferenceFieldUpdater2.compareAndSet(abstractC0771l0, abstractC0771l0, w0Var)) {
                    if (atomicReferenceFieldUpdater2.get(abstractC0771l0) != abstractC0771l0) {
                        break;
                    }
                }
                w0Var.e(abstractC0771l0);
                break loop0;
            }
            break;
        }
        a5.i iVarF = abstractC0771l0.f();
        do {
            atomicReferenceFieldUpdater = f8337a;
            if (atomicReferenceFieldUpdater.compareAndSet(this, abstractC0771l0, iVarF)) {
                return;
            }
        } while (atomicReferenceFieldUpdater.get(this) == abstractC0771l0);
    }

    public final int P(Object obj) {
        boolean z6 = obj instanceof S;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8337a;
        if (z6) {
            if (((S) obj).f8265a) {
                return 0;
            }
            S s6 = E.f8239j;
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, s6)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    return -1;
                }
            }
            M();
            return 1;
        }
        if (!(obj instanceof C0753c0)) {
            return 0;
        }
        w0 w0Var = ((C0753c0) obj).f8287a;
        while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, w0Var)) {
            if (atomicReferenceFieldUpdater.get(this) != obj) {
                return -1;
            }
        }
        M();
        return 1;
    }

    public final Object S(Object obj, Object obj2) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        if (!(obj instanceof InterfaceC0755d0)) {
            return E.f8233d;
        }
        if (((obj instanceof S) || (obj instanceof AbstractC0771l0)) && !(obj instanceof C0779q) && !(obj2 instanceof C0784w)) {
            InterfaceC0755d0 interfaceC0755d0 = (InterfaceC0755d0) obj;
            Object c0757e0 = obj2 instanceof InterfaceC0755d0 ? new C0757e0((InterfaceC0755d0) obj2) : obj2;
            do {
                atomicReferenceFieldUpdater = f8337a;
                if (atomicReferenceFieldUpdater.compareAndSet(this, interfaceC0755d0, c0757e0)) {
                    L(obj2);
                    s(interfaceC0755d0, obj2);
                    return obj2;
                }
            } while (atomicReferenceFieldUpdater.get(this) == interfaceC0755d0);
            return E.f8235f;
        }
        InterfaceC0755d0 interfaceC0755d02 = (InterfaceC0755d0) obj;
        w0 w0VarZ = z(interfaceC0755d02);
        if (w0VarZ == null) {
            return E.f8235f;
        }
        C0777o0 c0777o0 = interfaceC0755d02 instanceof C0777o0 ? (C0777o0) interfaceC0755d02 : null;
        if (c0777o0 == null) {
            c0777o0 = new C0777o0(w0VarZ, null);
        }
        synchronized (c0777o0) {
            try {
                AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = C0777o0.f8324b;
                if (atomicIntegerFieldUpdater.get(c0777o0) != 0) {
                    return E.f8233d;
                }
                atomicIntegerFieldUpdater.set(c0777o0, 1);
                if (c0777o0 != interfaceC0755d02) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = f8337a;
                    while (!atomicReferenceFieldUpdater2.compareAndSet(this, interfaceC0755d02, c0777o0)) {
                        if (atomicReferenceFieldUpdater2.get(this) != interfaceC0755d02) {
                            return E.f8235f;
                        }
                    }
                }
                boolean zD = c0777o0.d();
                C0784w c0784w = obj2 instanceof C0784w ? (C0784w) obj2 : null;
                if (c0784w != null) {
                    c0777o0.a(c0784w.f8346a);
                }
                Throwable thC = zD ? null : c0777o0.c();
                if (thC != null) {
                    K(w0VarZ, thC);
                }
                C0779q c0779qJ = J(w0VarZ);
                if (c0779qJ != null && T(c0777o0, c0779qJ, obj2)) {
                    return E.f8234e;
                }
                w0VarZ.c(new a5.h(2), 2);
                C0779q c0779qJ2 = J(w0VarZ);
                return (c0779qJ2 == null || !T(c0777o0, c0779qJ2, obj2)) ? u(c0777o0, obj2) : E.f8234e;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean T(C0777o0 c0777o0, C0779q c0779q, Object obj) {
        while (E.t(c0779q.f8328e, false, new C0775n0(this, c0777o0, c0779q, obj)) == y0.f8351a) {
            c0779q = J(c0779q);
            if (c0779q == null) {
                return false;
            }
        }
        return true;
    }

    @Override // V4.InterfaceC0765i0
    public final InterfaceC0778p attachChild(r rVar) {
        C0779q c0779q = new C0779q(rVar);
        c0779q.f8311d = this;
        loop0: while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8337a;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof S) {
                S s6 = (S) obj;
                if (s6.f8265a) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0779q)) {
                        if (atomicReferenceFieldUpdater.get(this) != obj) {
                            break;
                        }
                    }
                    break loop0;
                }
                N(s6);
            } else {
                boolean z6 = obj instanceof InterfaceC0755d0;
                y0 y0Var = y0.f8351a;
                if (!z6) {
                    Object obj2 = atomicReferenceFieldUpdater.get(this);
                    C0784w c0784w = obj2 instanceof C0784w ? (C0784w) obj2 : null;
                    c0779q.j(c0784w != null ? c0784w.f8346a : null);
                    return y0Var;
                }
                w0 w0VarB = ((InterfaceC0755d0) obj).b();
                if (w0VarB == null) {
                    kotlin.jvm.internal.o.f(obj, "null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    O((AbstractC0771l0) obj);
                } else if (!w0VarB.c(c0779q, 7)) {
                    boolean zC = w0VarB.c(c0779q, 3);
                    Object obj3 = atomicReferenceFieldUpdater.get(this);
                    if (obj3 instanceof C0777o0) {
                        thC = ((C0777o0) obj3).c();
                    } else {
                        C0784w c0784w2 = obj3 instanceof C0784w ? (C0784w) obj3 : null;
                        if (c0784w2 != null) {
                            thC = c0784w2.f8346a;
                        }
                    }
                    c0779q.j(thC);
                    if (zC) {
                        break loop0;
                    }
                    return y0Var;
                }
            }
        }
        return c0779q;
    }

    @Override // V4.InterfaceC0765i0
    public void cancel(CancellationException cancellationException) {
        if (cancellationException == null) {
            cancellationException = new C0767j0(q(), null, this);
        }
        o(cancellationException);
    }

    @Override // A4.j
    public final Object fold(Object obj, J4.e eVar) {
        return eVar.invoke(obj, this);
    }

    @Override // A4.j
    public final A4.h get(A4.i iVar) {
        return AbstractC0872a.H(this, iVar);
    }

    @Override // V4.InterfaceC0765i0
    public final CancellationException getCancellationException() {
        Object obj = f8337a.get(this);
        if (!(obj instanceof C0777o0)) {
            if (!(obj instanceof InterfaceC0755d0)) {
                return obj instanceof C0784w ? R(this, ((C0784w) obj).f8346a) : new C0767j0(getClass().getSimpleName().concat(" has completed normally"), null, this);
            }
            throw new IllegalStateException(("Job is still new or active: " + this).toString());
        }
        Throwable thC = ((C0777o0) obj).c();
        if (thC == null) {
            throw new IllegalStateException(("Job is still new or active: " + this).toString());
        }
        String strConcat = getClass().getSimpleName().concat(" is cancelling");
        CancellationException c0767j0 = thC instanceof CancellationException ? (CancellationException) thC : null;
        if (c0767j0 == null) {
            if (strConcat == null) {
                strConcat = q();
            }
            c0767j0 = new C0767j0(strConcat, thC, this);
        }
        return c0767j0;
    }

    @Override // V4.InterfaceC0765i0
    public final R4.k getChildren() {
        return new H4.f(new q0(null, this));
    }

    public Object getCompleted() {
        return v();
    }

    public final Throwable getCompletionExceptionOrNull() {
        Object obj = f8337a.get(this);
        if (obj instanceof InterfaceC0755d0) {
            throw new IllegalStateException("This job has not completed yet");
        }
        C0784w c0784w = obj instanceof C0784w ? (C0784w) obj : null;
        if (c0784w != null) {
            return c0784w.f8346a;
        }
        return null;
    }

    @Override // A4.h
    public final A4.i getKey() {
        return C0763h0.f8306a;
    }

    @Override // V4.InterfaceC0765i0
    public final P invokeOnCompletion(J4.c cVar) {
        return D(true, new Q(cVar, 1));
    }

    @Override // V4.InterfaceC0765i0
    public boolean isActive() {
        Object obj = f8337a.get(this);
        return (obj instanceof InterfaceC0755d0) && ((InterfaceC0755d0) obj).isActive();
    }

    @Override // V4.InterfaceC0765i0
    public final boolean isCancelled() {
        Object obj = f8337a.get(this);
        if (obj instanceof C0784w) {
            return true;
        }
        return (obj instanceof C0777o0) && ((C0777o0) obj).d();
    }

    public void j(Object obj) {
        h(obj);
    }

    @Override // V4.InterfaceC0765i0
    public final Object join(A4.d dVar) {
        boolean z6;
        while (true) {
            Object obj = f8337a.get(this);
            if (!(obj instanceof InterfaceC0755d0)) {
                z6 = false;
                break;
            }
            if (P(obj) >= 0) {
                z6 = true;
                break;
            }
        }
        C3137A c3137a = C3137A.f25453a;
        if (!z6) {
            E.m(dVar.getContext());
            return c3137a;
        }
        C0772m c0772m = new C0772m(1, AbstractC0872a.V(dVar));
        c0772m.s();
        c0772m.v(new C0766j(E.t(this, true, new C0776o(c0772m, 1)), 2));
        Object objR = c0772m.r();
        B4.a aVar = B4.a.f492a;
        if (objR != aVar) {
            objR = c3137a;
        }
        return objR == aVar ? objR : c3137a;
    }

    public final Object m(A4.d dVar) throws Throwable {
        Object obj;
        do {
            obj = f8337a.get(this);
            if (!(obj instanceof InterfaceC0755d0)) {
                if (obj instanceof C0784w) {
                    throw ((C0784w) obj).f8346a;
                }
                return E.F(obj);
            }
        } while (P(obj) < 0);
        C0773m0 c0773m0 = new C0773m0(AbstractC0872a.V(dVar), this);
        c0773m0.s();
        c0773m0.v(new C0766j(E.t(this, true, new Q(c0773m0, 3)), 2));
        Object objR = c0773m0.r();
        B4.a aVar = B4.a.f492a;
        return objR;
    }

    @Override // A4.j
    public final A4.j minusKey(A4.i iVar) {
        return AbstractC0872a.Y(this, iVar);
    }

    /* JADX WARN: Code restructure failed: missing block: B:31:0x006a, code lost:
    
        r0 = r10;
     */
    /* JADX WARN: Removed duplicated region for block: B:18:0x0041 A[PHI: r0
      0x0041: PHI (r0v1 java.lang.Object) = (r0v0 java.lang.Object), (r0v13 java.lang.Object) binds: [B:3:0x0008, B:16:0x003d] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean n(Object obj) {
        C0317a c0317a;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        Object objS = E.f8233d;
        if (y()) {
            do {
                Object obj2 = f8337a.get(this);
                if (obj2 instanceof InterfaceC0755d0) {
                    if (obj2 instanceof C0777o0) {
                        C0777o0 c0777o0 = (C0777o0) obj2;
                        c0777o0.getClass();
                        if (C0777o0.f8324b.get(c0777o0) != 0) {
                        }
                    }
                    objS = S(obj2, new C0784w(t(obj), false));
                }
                objS = E.f8233d;
                break;
            } while (objS == E.f8235f);
            if (objS != E.f8234e) {
                if (objS == E.f8233d) {
                    Throwable thT = null;
                    loop1: while (true) {
                        Object obj3 = f8337a.get(this);
                        if (!(obj3 instanceof C0777o0)) {
                            if (!(obj3 instanceof InterfaceC0755d0)) {
                                c0317a = E.f8236g;
                                break;
                            }
                            if (thT == null) {
                                thT = t(obj);
                            }
                            InterfaceC0755d0 interfaceC0755d0 = (InterfaceC0755d0) obj3;
                            if (interfaceC0755d0.isActive()) {
                                w0 w0VarZ = z(interfaceC0755d0);
                                if (w0VarZ == null) {
                                    continue;
                                } else {
                                    C0777o0 c0777o02 = new C0777o0(w0VarZ, thT);
                                    do {
                                        atomicReferenceFieldUpdater = f8337a;
                                        if (atomicReferenceFieldUpdater.compareAndSet(this, interfaceC0755d0, c0777o02)) {
                                            K(w0VarZ, thT);
                                            c0317a = E.f8233d;
                                            break loop1;
                                        }
                                    } while (atomicReferenceFieldUpdater.get(this) == interfaceC0755d0);
                                }
                            } else {
                                Object objS2 = S(obj3, new C0784w(thT, false));
                                if (objS2 == E.f8233d) {
                                    throw new IllegalStateException(("Cannot happen in " + obj3).toString());
                                }
                                if (objS2 != E.f8235f) {
                                    objS = objS2;
                                    break;
                                }
                            }
                        } else {
                            synchronized (obj3) {
                                try {
                                    C0777o0 c0777o03 = (C0777o0) obj3;
                                    c0777o03.getClass();
                                    if (C0777o0.f8326d.get(c0777o03) == E.f8237h) {
                                        c0317a = E.f8236g;
                                    } else {
                                        boolean zD = ((C0777o0) obj3).d();
                                        if (thT == null) {
                                            thT = t(obj);
                                        }
                                        ((C0777o0) obj3).a(thT);
                                        Throwable thC = zD ? null : ((C0777o0) obj3).c();
                                        if (thC != null) {
                                            K(((C0777o0) obj3).f8327a, thC);
                                        }
                                        c0317a = E.f8233d;
                                    }
                                } catch (Throwable th) {
                                    throw th;
                                }
                            }
                        }
                    }
                }
                if (objS != E.f8233d && objS != E.f8234e) {
                    if (objS == E.f8236g) {
                        return false;
                    }
                    h(objS);
                    return true;
                }
            }
        }
        return true;
    }

    public void o(CancellationException cancellationException) {
        n(cancellationException);
    }

    public final boolean p(Throwable th) {
        if (F()) {
            return true;
        }
        boolean z6 = th instanceof CancellationException;
        InterfaceC0778p interfaceC0778p = (InterfaceC0778p) f8338b.get(this);
        return (interfaceC0778p == null || interfaceC0778p == y0.f8351a) ? z6 : interfaceC0778p.a(th) || z6;
    }

    @Override // A4.j
    public final A4.j plus(A4.j jVar) {
        return AbstractC0872a.a0(this, jVar);
    }

    public String q() {
        return "Job was cancelled";
    }

    public boolean r(Throwable th) {
        if (th instanceof CancellationException) {
            return true;
        }
        return n(th) && x();
    }

    public final void s(InterfaceC0755d0 interfaceC0755d0, Object obj) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8338b;
        InterfaceC0778p interfaceC0778p = (InterfaceC0778p) atomicReferenceFieldUpdater.get(this);
        if (interfaceC0778p != null) {
            interfaceC0778p.dispose();
            atomicReferenceFieldUpdater.set(this, y0.f8351a);
        }
        E3.v vVar = null;
        C0784w c0784w = obj instanceof C0784w ? (C0784w) obj : null;
        Throwable th = c0784w != null ? c0784w.f8346a : null;
        if (interfaceC0755d0 instanceof AbstractC0771l0) {
            try {
                ((AbstractC0771l0) interfaceC0755d0).j(th);
                return;
            } catch (Throwable th2) {
                B(new E3.v("Exception in completion handler " + interfaceC0755d0 + " for " + this, th2));
                return;
            }
        }
        w0 w0VarB = interfaceC0755d0.b();
        if (w0VarB != null) {
            w0VarB.c(new a5.h(1), 1);
            Object obj2 = a5.i.f10833a.get(w0VarB);
            kotlin.jvm.internal.o.f(obj2, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode");
            for (a5.i iVarF = (a5.i) obj2; !iVarF.equals(w0VarB); iVarF = iVarF.f()) {
                if (iVarF instanceof AbstractC0771l0) {
                    try {
                        ((AbstractC0771l0) iVarF).j(th);
                    } catch (Throwable th3) {
                        if (vVar != null) {
                            AbstractC2613d.b(vVar, th3);
                        } else {
                            vVar = new E3.v("Exception in completion handler " + iVarF + " for " + this, th3);
                        }
                    }
                }
            }
            if (vVar != null) {
                B(vVar);
            }
        }
    }

    @Override // V4.InterfaceC0765i0
    public final boolean start() {
        int iP;
        do {
            iP = P(f8337a.get(this));
            if (iP == 0) {
                return false;
            }
        } while (iP != 1);
        return true;
    }

    public final Throwable t(Object obj) {
        Throwable thC;
        if (obj instanceof Throwable) {
            return (Throwable) obj;
        }
        u0 u0Var = (u0) ((A0) obj);
        Object obj2 = f8337a.get(u0Var);
        if (obj2 instanceof C0777o0) {
            thC = ((C0777o0) obj2).c();
        } else if (obj2 instanceof C0784w) {
            thC = ((C0784w) obj2).f8346a;
        } else {
            if (obj2 instanceof InterfaceC0755d0) {
                throw new IllegalStateException(("Cannot be cancelling child in this state: " + obj2).toString());
            }
            thC = null;
        }
        CancellationException cancellationException = thC instanceof CancellationException ? (CancellationException) thC : null;
        return cancellationException == null ? new C0767j0("Parent job is ".concat(Q(obj2)), thC, u0Var) : cancellationException;
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(I() + '{' + Q(f8337a.get(this)) + '}');
        sb.append('@');
        sb.append(E.p(this));
        return sb.toString();
    }

    public final Object u(C0777o0 c0777o0, Object obj) {
        Throwable thW;
        C0784w c0784w = obj instanceof C0784w ? (C0784w) obj : null;
        Throwable th = c0784w != null ? c0784w.f8346a : null;
        synchronized (c0777o0) {
            c0777o0.d();
            ArrayList<Throwable> arrayListE = c0777o0.e(th);
            thW = w(c0777o0, arrayListE);
            if (thW != null && arrayListE.size() > 1) {
                Set setNewSetFromMap = Collections.newSetFromMap(new IdentityHashMap(arrayListE.size()));
                for (Throwable th2 : arrayListE) {
                    if (th2 != thW && th2 != thW && !(th2 instanceof CancellationException) && setNewSetFromMap.add(th2)) {
                        AbstractC2613d.b(thW, th2);
                    }
                }
            }
        }
        if (thW != null && thW != th) {
            obj = new C0784w(thW, false);
        }
        if (thW != null && (p(thW) || A(thW))) {
            kotlin.jvm.internal.o.f(obj, "null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
            C0784w.f8345b.compareAndSet((C0784w) obj, 0, 1);
        }
        L(obj);
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8337a;
        Object c0757e0 = obj instanceof InterfaceC0755d0 ? new C0757e0((InterfaceC0755d0) obj) : obj;
        while (!atomicReferenceFieldUpdater.compareAndSet(this, c0777o0, c0757e0) && atomicReferenceFieldUpdater.get(this) == c0777o0) {
        }
        s(c0777o0, obj);
        return obj;
    }

    public final Object v() throws Throwable {
        Object obj = f8337a.get(this);
        if (obj instanceof InterfaceC0755d0) {
            throw new IllegalStateException("This job has not completed yet");
        }
        if (obj instanceof C0784w) {
            throw ((C0784w) obj).f8346a;
        }
        return E.F(obj);
    }

    public final Throwable w(C0777o0 c0777o0, ArrayList arrayList) {
        Object next;
        Object obj = null;
        if (arrayList.isEmpty()) {
            if (c0777o0.d()) {
                return new C0767j0(q(), null, this);
            }
            return null;
        }
        Iterator it = arrayList.iterator();
        while (true) {
            if (!it.hasNext()) {
                next = null;
                break;
            }
            next = it.next();
            if (!(((Throwable) next) instanceof CancellationException)) {
                break;
            }
        }
        Throwable th = (Throwable) next;
        if (th != null) {
            return th;
        }
        Throwable th2 = (Throwable) arrayList.get(0);
        if (th2 instanceof H0) {
            Iterator it2 = arrayList.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break;
                }
                Object next2 = it2.next();
                Throwable th3 = (Throwable) next2;
                if (th3 != th2 && (th3 instanceof H0)) {
                    obj = next2;
                    break;
                }
            }
            Throwable th4 = (Throwable) obj;
            if (th4 != null) {
                return th4;
            }
        }
        return th2;
    }

    public boolean x() {
        return true;
    }

    public boolean y() {
        return this instanceof C0781t;
    }

    public final w0 z(InterfaceC0755d0 interfaceC0755d0) {
        w0 w0VarB = interfaceC0755d0.b();
        if (w0VarB != null) {
            return w0VarB;
        }
        if (interfaceC0755d0 instanceof S) {
            return new w0();
        }
        if (interfaceC0755d0 instanceof AbstractC0771l0) {
            O((AbstractC0771l0) interfaceC0755d0);
            return null;
        }
        throw new IllegalStateException(("State should have list: " + interfaceC0755d0).toString());
    }

    @Override // V4.InterfaceC0765i0
    public final P invokeOnCompletion(boolean z6, boolean z7, J4.c cVar) {
        AbstractC0771l0 q6;
        if (z6) {
            q6 = new C0761g0(cVar);
        } else {
            q6 = new Q(cVar, 1);
        }
        return D(z7, q6);
    }

    public void M() {
    }

    public void B(E3.v vVar) {
        throw vVar;
    }

    public void L(Object obj) {
    }

    public void h(Object obj) {
    }
}
