package g.a;

import f.w.g;
import g.a.n1;
import g.a.n2.p;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: JobSupport.kt */
/* JADX INFO: loaded from: classes.dex */
public class u1 implements n1, q, c2 {

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

    /* JADX INFO: compiled from: JobSupport.kt */
    private static final class a<T> extends k<T> {
        private final u1 m;

        public a(f.w.d<? super T> dVar, u1 u1Var) {
            super(dVar, 1);
            this.m = u1Var;
        }

        @Override // g.a.k
        protected String B() {
            return "AwaitContinuation";
        }

        @Override // g.a.k
        public Throwable r(n1 n1Var) {
            Throwable thE;
            Object objN = this.m.N();
            return (!(objN instanceof c) || (thE = ((c) objN).e()) == null) ? objN instanceof t ? ((t) objN).a : n1Var.n() : thE;
        }
    }

    /* JADX INFO: compiled from: JobSupport.kt */
    private static final class b extends t1 {

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private final u1 f4154i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private final c f4155j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        private final p f4156k;
        private final Object l;

        public b(u1 u1Var, c cVar, p pVar, Object obj) {
            this.f4154i = u1Var;
            this.f4155j = cVar;
            this.f4156k = pVar;
            this.l = obj;
        }

        @Override // f.z.c.l
        public /* bridge */ /* synthetic */ f.t c(Throwable th) {
            s(th);
            return f.t.a;
        }

        @Override // g.a.v
        public void s(Throwable th) {
            this.f4154i.D(this.f4155j, this.f4156k, this.l);
        }
    }

    /* JADX INFO: compiled from: JobSupport.kt */
    private static final class c implements i1 {
        private volatile /* synthetic */ Object _exceptionsHolder = null;
        private volatile /* synthetic */ int _isCompleting;
        private volatile /* synthetic */ Object _rootCause;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final z1 f4157b;

        public c(z1 z1Var, boolean z, Throwable th) {
            this.f4157b = z1Var;
            this._isCompleting = z ? 1 : 0;
            this._rootCause = th;
        }

        private final ArrayList<Throwable> c() {
            return new ArrayList<>(4);
        }

        private final Object d() {
            return this._exceptionsHolder;
        }

        private final void k(Object obj) {
            this._exceptionsHolder = obj;
        }

        public final void a(Throwable th) {
            Throwable thE = e();
            if (thE == null) {
                l(th);
                return;
            }
            if (th == thE) {
                return;
            }
            Object objD = d();
            if (objD == null) {
                k(th);
                return;
            }
            if (objD instanceof Throwable) {
                if (th == objD) {
                    return;
                }
                ArrayList<Throwable> arrayListC = c();
                arrayListC.add(objD);
                arrayListC.add(th);
                k(arrayListC);
                return;
            }
            if (objD instanceof ArrayList) {
                ((ArrayList) objD).add(th);
                return;
            }
            throw new IllegalStateException(("State is " + objD).toString());
        }

        @Override // g.a.i1
        public z1 b() {
            return this.f4157b;
        }

        public final Throwable e() {
            return (Throwable) this._rootCause;
        }

        public final boolean f() {
            return e() != null;
        }

        /* JADX WARN: Type inference failed for: r0v0, types: [boolean, int] */
        public final boolean g() {
            return this._isCompleting;
        }

        public final boolean h() {
            return d() == v1.f4164e;
        }

        public final List<Throwable> i(Throwable th) {
            ArrayList<Throwable> arrayListC;
            Object objD = d();
            if (objD == null) {
                arrayListC = c();
            } else if (objD instanceof Throwable) {
                ArrayList<Throwable> arrayListC2 = c();
                arrayListC2.add(objD);
                arrayListC = arrayListC2;
            } else {
                if (!(objD instanceof ArrayList)) {
                    throw new IllegalStateException(("State is " + objD).toString());
                }
                arrayListC = (ArrayList) objD;
            }
            Throwable thE = e();
            if (thE != null) {
                arrayListC.add(0, thE);
            }
            if (th != null && !f.z.d.k.a(th, thE)) {
                arrayListC.add(th);
            }
            k(v1.f4164e);
            return arrayListC;
        }

        @Override // g.a.i1
        public boolean isActive() {
            return e() == null;
        }

        public final void j(boolean z) {
            this._isCompleting = z ? 1 : 0;
        }

        public final void l(Throwable th) {
            this._rootCause = th;
        }

        public String toString() {
            return "Finishing[cancelling=" + f() + ", completing=" + g() + ", rootCause=" + e() + ", exceptions=" + d() + ", list=" + b() + ']';
        }
    }

    /* JADX INFO: compiled from: LockFreeLinkedList.kt */
    public static final class d extends p.a {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        final /* synthetic */ u1 f4158d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ Object f4159e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(g.a.n2.p pVar, u1 u1Var, Object obj) {
            super(pVar);
            this.f4158d = u1Var;
            this.f4159e = obj;
        }

        @Override // g.a.n2.c
        /* JADX INFO: renamed from: i, reason: merged with bridge method [inline-methods] */
        public Object g(g.a.n2.p pVar) {
            if (this.f4158d.N() == this.f4159e) {
                return null;
            }
            return g.a.n2.o.a();
        }
    }

    public u1(boolean z) {
        this._state = z ? v1.f4166g : v1.f4165f;
        this._parentHandle = null;
    }

    private final void C(i1 i1Var, Object obj) throws Throwable {
        o oVarM = M();
        if (oVarM != null) {
            oVarM.dispose();
            h0(a2.f4009b);
        }
        t tVar = obj instanceof t ? (t) obj : null;
        Throwable th = tVar != null ? tVar.a : null;
        if (!(i1Var instanceof t1)) {
            z1 z1VarB = i1Var.b();
            if (z1VarB != null) {
                a0(z1VarB, th);
                return;
            }
            return;
        }
        try {
            ((t1) i1Var).s(th);
        } catch (Throwable th2) {
            P(new w("Exception in completion handler " + i1Var + " for " + this, th2));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void D(c cVar, p pVar, Object obj) {
        if (l0.a()) {
            if (!(N() == cVar)) {
                throw new AssertionError();
            }
        }
        p pVarY = Y(pVar);
        if (pVarY == null || !r0(cVar, pVarY, obj)) {
            s(F(cVar, obj));
        }
    }

    private final Throwable E(Object obj) {
        if (obj == null ? true : obj instanceof Throwable) {
            Throwable th = (Throwable) obj;
            return th == null ? new o1(A(), null, this) : th;
        }
        if (obj != null) {
            return ((c2) obj).k();
        }
        throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.ParentJob");
    }

    private final Object F(c cVar, Object obj) throws Throwable {
        boolean zF;
        Throwable thI;
        boolean z = true;
        if (l0.a()) {
            if (!(N() == cVar)) {
                throw new AssertionError();
            }
        }
        if (l0.a() && !(!cVar.h())) {
            throw new AssertionError();
        }
        if (l0.a() && !cVar.g()) {
            throw new AssertionError();
        }
        t tVar = obj instanceof t ? (t) obj : null;
        Throwable th = tVar != null ? tVar.a : null;
        synchronized (cVar) {
            zF = cVar.f();
            List<Throwable> listI = cVar.i(th);
            thI = I(cVar, listI);
            if (thI != null) {
                l(thI, listI);
            }
        }
        if (thI != null && thI != th) {
            obj = new t(thI, false, 2, null);
        }
        if (thI != null) {
            if (!z(thI) && !O(thI)) {
                z = false;
            }
            if (z) {
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
                }
                ((t) obj).b();
            }
        }
        if (!zF) {
            b0(thI);
        }
        c0(obj);
        boolean zCompareAndSet = f4153b.compareAndSet(this, cVar, v1.g(obj));
        if (l0.a() && !zCompareAndSet) {
            throw new AssertionError();
        }
        C(cVar, obj);
        return obj;
    }

    private final p G(i1 i1Var) {
        p pVar = i1Var instanceof p ? (p) i1Var : null;
        if (pVar != null) {
            return pVar;
        }
        z1 z1VarB = i1Var.b();
        if (z1VarB != null) {
            return Y(z1VarB);
        }
        return null;
    }

    private final Throwable H(Object obj) {
        t tVar = obj instanceof t ? (t) obj : null;
        if (tVar != null) {
            return tVar.a;
        }
        return null;
    }

    private final Throwable I(c cVar, List<? extends Throwable> list) {
        Object obj = null;
        if (list.isEmpty()) {
            if (cVar.f()) {
                return new o1(A(), null, this);
            }
            return null;
        }
        Iterator<T> it = list.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Object next = it.next();
            if (!(((Throwable) next) instanceof CancellationException)) {
                obj = next;
                break;
            }
        }
        Throwable th = (Throwable) obj;
        return th != null ? th : list.get(0);
    }

    private final z1 L(i1 i1Var) {
        z1 z1VarB = i1Var.b();
        if (z1VarB != null) {
            return z1VarB;
        }
        if (i1Var instanceof a1) {
            return new z1();
        }
        if (i1Var instanceof t1) {
            f0((t1) i1Var);
            return null;
        }
        throw new IllegalStateException(("State should have list: " + i1Var).toString());
    }

    private final Object U(Object obj) throws Throwable {
        Throwable thE = null;
        while (true) {
            Object objN = N();
            if (objN instanceof c) {
                synchronized (objN) {
                    if (((c) objN).h()) {
                        return v1.f4163d;
                    }
                    boolean zF = ((c) objN).f();
                    if (obj != null || !zF) {
                        if (thE == null) {
                            thE = E(obj);
                        }
                        ((c) objN).a(thE);
                    }
                    Throwable thE2 = zF ^ true ? ((c) objN).e() : null;
                    if (thE2 != null) {
                        Z(((c) objN).b(), thE2);
                    }
                    return v1.a;
                }
            }
            if (!(objN instanceof i1)) {
                return v1.f4163d;
            }
            if (thE == null) {
                thE = E(obj);
            }
            i1 i1Var = (i1) objN;
            if (!i1Var.isActive()) {
                Object objP0 = p0(objN, new t(thE, false, 2, null));
                if (objP0 == v1.a) {
                    throw new IllegalStateException(("Cannot happen in " + objN).toString());
                }
                if (objP0 != v1.f4162c) {
                    return objP0;
                }
            } else if (o0(i1Var, thE)) {
                return v1.a;
            }
        }
    }

    private final t1 W(f.z.c.l<? super Throwable, f.t> lVar, boolean z) {
        t1 m1Var;
        if (z) {
            m1Var = lVar instanceof p1 ? (p1) lVar : null;
            if (m1Var == null) {
                m1Var = new l1(lVar);
            }
        } else {
            m1Var = lVar instanceof t1 ? (t1) lVar : null;
            if (m1Var == null) {
                m1Var = new m1(lVar);
            } else if (l0.a() && !(!(m1Var instanceof p1))) {
                throw new AssertionError();
            }
        }
        m1Var.u(this);
        return m1Var;
    }

    private final p Y(g.a.n2.p pVar) {
        while (pVar.n()) {
            pVar = pVar.m();
        }
        while (true) {
            pVar = pVar.l();
            if (!pVar.n()) {
                if (pVar instanceof p) {
                    return (p) pVar;
                }
                if (pVar instanceof z1) {
                    return null;
                }
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0023  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private final void Z(g.a.z1 r7, java.lang.Throwable r8) throws java.lang.Throwable {
        /*
            r6 = this;
            r6.b0(r8)
            java.lang.Object r0 = r7.k()
            g.a.n2.p r0 = (g.a.n2.p) r0
            r1 = 0
        La:
            boolean r2 = f.z.d.k.a(r0, r7)
            if (r2 != 0) goto L48
            boolean r2 = r0 instanceof g.a.p1
            if (r2 == 0) goto L43
            r2 = r0
            g.a.t1 r2 = (g.a.t1) r2
            r2.s(r8)     // Catch: java.lang.Throwable -> L1b
            goto L43
        L1b:
            r3 = move-exception
            if (r1 == 0) goto L23
            f.a.a(r1, r3)
            if (r1 != 0) goto L43
        L23:
            g.a.w r1 = new g.a.w
            java.lang.StringBuilder r4 = new java.lang.StringBuilder
            r4.<init>()
            java.lang.String r5 = "Exception in completion handler "
            r4.append(r5)
            r4.append(r2)
            java.lang.String r2 = " for "
            r4.append(r2)
            r4.append(r6)
            java.lang.String r2 = r4.toString()
            r1.<init>(r2, r3)
            f.t r2 = f.t.a
        L43:
            g.a.n2.p r0 = r0.l()
            goto La
        L48:
            if (r1 == 0) goto L4d
            r6.P(r1)
        L4d:
            r6.z(r8)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: g.a.u1.Z(g.a.z1, java.lang.Throwable):void");
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0020  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private final void a0(g.a.z1 r7, java.lang.Throwable r8) throws java.lang.Throwable {
        /*
            r6 = this;
            java.lang.Object r0 = r7.k()
            g.a.n2.p r0 = (g.a.n2.p) r0
            r1 = 0
        L7:
            boolean r2 = f.z.d.k.a(r0, r7)
            if (r2 != 0) goto L45
            boolean r2 = r0 instanceof g.a.t1
            if (r2 == 0) goto L40
            r2 = r0
            g.a.t1 r2 = (g.a.t1) r2
            r2.s(r8)     // Catch: java.lang.Throwable -> L18
            goto L40
        L18:
            r3 = move-exception
            if (r1 == 0) goto L20
            f.a.a(r1, r3)
            if (r1 != 0) goto L40
        L20:
            g.a.w r1 = new g.a.w
            java.lang.StringBuilder r4 = new java.lang.StringBuilder
            r4.<init>()
            java.lang.String r5 = "Exception in completion handler "
            r4.append(r5)
            r4.append(r2)
            java.lang.String r2 = " for "
            r4.append(r2)
            r4.append(r6)
            java.lang.String r2 = r4.toString()
            r1.<init>(r2, r3)
            f.t r2 = f.t.a
        L40:
            g.a.n2.p r0 = r0.l()
            goto L7
        L45:
            if (r1 == 0) goto L4a
            r6.P(r1)
        L4a:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: g.a.u1.a0(g.a.z1, java.lang.Throwable):void");
    }

    private final void e0(a1 a1Var) {
        z1 z1Var = new z1();
        Object h1Var = z1Var;
        if (!a1Var.isActive()) {
            h1Var = new h1(z1Var);
        }
        f4153b.compareAndSet(this, a1Var, h1Var);
    }

    private final void f0(t1 t1Var) {
        t1Var.g(new z1());
        f4153b.compareAndSet(this, t1Var, t1Var.l());
    }

    private final boolean i(Object obj, z1 z1Var, t1 t1Var) {
        int iR;
        d dVar = new d(t1Var, this, obj);
        do {
            iR = z1Var.m().r(t1Var, z1Var, dVar);
            if (iR == 1) {
                return true;
            }
        } while (iR != 2);
        return false;
    }

    private final int i0(Object obj) {
        if (obj instanceof a1) {
            if (((a1) obj).isActive()) {
                return 0;
            }
            if (!f4153b.compareAndSet(this, obj, v1.f4166g)) {
                return -1;
            }
            d0();
            return 1;
        }
        if (!(obj instanceof h1)) {
            return 0;
        }
        if (!f4153b.compareAndSet(this, obj, ((h1) obj).b())) {
            return -1;
        }
        d0();
        return 1;
    }

    private final String j0(Object obj) {
        if (!(obj instanceof c)) {
            return obj instanceof i1 ? ((i1) obj).isActive() ? "Active" : "New" : obj instanceof t ? "Cancelled" : "Completed";
        }
        c cVar = (c) obj;
        return cVar.f() ? "Cancelling" : cVar.g() ? "Completing" : "Active";
    }

    private final void l(Throwable th, List<? extends Throwable> list) {
        if (list.size() <= 1) {
            return;
        }
        Set setNewSetFromMap = Collections.newSetFromMap(new IdentityHashMap(list.size()));
        Throwable thL = !l0.d() ? th : g.a.n2.a0.l(th);
        for (Throwable thL2 : list) {
            if (l0.d()) {
                thL2 = g.a.n2.a0.l(thL2);
            }
            if (thL2 != th && thL2 != thL && !(thL2 instanceof CancellationException) && setNewSetFromMap.add(thL2)) {
                f.b.a(th, thL2);
            }
        }
    }

    public static /* synthetic */ CancellationException l0(u1 u1Var, Throwable th, String str, int i2, Object obj) {
        if (obj != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: toCancellationException");
        }
        if ((i2 & 1) != 0) {
            str = null;
        }
        return u1Var.k0(th, str);
    }

    private final boolean n0(i1 i1Var, Object obj) throws Throwable {
        if (l0.a()) {
            if (!((i1Var instanceof a1) || (i1Var instanceof t1))) {
                throw new AssertionError();
            }
        }
        if (l0.a() && !(!(obj instanceof t))) {
            throw new AssertionError();
        }
        if (!f4153b.compareAndSet(this, i1Var, v1.g(obj))) {
            return false;
        }
        b0(null);
        c0(obj);
        C(i1Var, obj);
        return true;
    }

    private final boolean o0(i1 i1Var, Throwable th) throws Throwable {
        if (l0.a() && !(!(i1Var instanceof c))) {
            throw new AssertionError();
        }
        if (l0.a() && !i1Var.isActive()) {
            throw new AssertionError();
        }
        z1 z1VarL = L(i1Var);
        if (z1VarL == null) {
            return false;
        }
        if (!f4153b.compareAndSet(this, i1Var, new c(z1VarL, false, th))) {
            return false;
        }
        Z(z1VarL, th);
        return true;
    }

    private final Object p0(Object obj, Object obj2) {
        return !(obj instanceof i1) ? v1.a : ((!(obj instanceof a1) && !(obj instanceof t1)) || (obj instanceof p) || (obj2 instanceof t)) ? q0((i1) obj, obj2) : n0((i1) obj, obj2) ? obj2 : v1.f4162c;
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final Object q0(i1 i1Var, Object obj) throws Throwable {
        z1 z1VarL = L(i1Var);
        if (z1VarL == null) {
            return v1.f4162c;
        }
        c cVar = i1Var instanceof c ? (c) i1Var : null;
        if (cVar == null) {
            cVar = new c(z1VarL, false, null);
        }
        f.z.d.u uVar = new f.z.d.u();
        synchronized (cVar) {
            if (cVar.g()) {
                return v1.a;
            }
            cVar.j(true);
            if (cVar != i1Var && !f4153b.compareAndSet(this, i1Var, cVar)) {
                return v1.f4162c;
            }
            if (l0.a() && !(!cVar.h())) {
                throw new AssertionError();
            }
            boolean zF = cVar.f();
            t tVar = obj instanceof t ? (t) obj : null;
            if (tVar != null) {
                cVar.a(tVar.a);
            }
            T tE = Boolean.valueOf(zF ? false : true).booleanValue() ? cVar.e() : 0;
            uVar.f4002b = tE;
            f.t tVar2 = f.t.a;
            Throwable th = (Throwable) tE;
            if (th != null) {
                Z(z1VarL, th);
            }
            p pVarG = G(i1Var);
            return (pVarG == null || !r0(cVar, pVarG, obj)) ? F(cVar, obj) : v1.f4161b;
        }
    }

    private final boolean r0(c cVar, p pVar, Object obj) {
        while (n1.a.d(pVar.f4106i, false, false, new b(this, cVar, pVar, obj), 1, null) == a2.f4009b) {
            pVar = Y(pVar);
            if (pVar == null) {
                return false;
            }
        }
        return true;
    }

    private final Object v(f.w.d<Object> dVar) {
        a aVar = new a(f.w.j.c.c(dVar), this);
        aVar.v();
        l.a(aVar, R(new d2(aVar)));
        Object objS = aVar.s();
        if (objS == f.w.j.d.d()) {
            f.w.k.a.h.c(dVar);
        }
        return objS;
    }

    private final Object y(Object obj) {
        Object objP0;
        do {
            Object objN = N();
            if (!(objN instanceof i1) || ((objN instanceof c) && ((c) objN).g())) {
                return v1.a;
            }
            objP0 = p0(objN, new t(E(obj), false, 2, null));
        } while (objP0 == v1.f4162c);
        return objP0;
    }

    private final boolean z(Throwable th) {
        if (T()) {
            return true;
        }
        boolean z = th instanceof CancellationException;
        o oVarM = M();
        return (oVarM == null || oVarM == a2.f4009b) ? z : oVarM.e(th) || z;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public String A() {
        return "Job was cancelled";
    }

    public boolean B(Throwable th) {
        if (th instanceof CancellationException) {
            return true;
        }
        return w(th) && J();
    }

    public boolean J() {
        return true;
    }

    public boolean K() {
        return false;
    }

    public final o M() {
        return (o) this._parentHandle;
    }

    public final Object N() {
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof g.a.n2.w)) {
                return obj;
            }
            ((g.a.n2.w) obj).c(this);
        }
    }

    protected boolean O(Throwable th) {
        return false;
    }

    public void P(Throwable th) throws Throwable {
        throw th;
    }

    protected final void Q(n1 n1Var) {
        if (l0.a()) {
            if (!(M() == null)) {
                throw new AssertionError();
            }
        }
        if (n1Var == null) {
            h0(a2.f4009b);
            return;
        }
        n1Var.start();
        o oVarU = n1Var.u(this);
        h0(oVarU);
        if (S()) {
            oVarU.dispose();
            h0(a2.f4009b);
        }
    }

    public final y0 R(f.z.c.l<? super Throwable, f.t> lVar) {
        return m(false, true, lVar);
    }

    public final boolean S() {
        return !(N() instanceof i1);
    }

    protected boolean T() {
        return false;
    }

    public final Object V(Object obj) {
        Object objP0;
        do {
            objP0 = p0(N(), obj);
            if (objP0 == v1.a) {
                throw new IllegalStateException("Job " + this + " is already complete or completing, but is being completed with " + obj, H(obj));
            }
        } while (objP0 == v1.f4162c);
        return objP0;
    }

    public String X() {
        return m0.a(this);
    }

    protected void b0(Throwable th) {
    }

    protected void c0(Object obj) {
    }

    protected void d0() {
    }

    @Override // f.w.g
    public <R> R fold(R r, f.z.c.p<? super R, ? super g.b, ? extends R> pVar) {
        return (R) n1.a.b(this, r, pVar);
    }

    public final void g0(t1 t1Var) {
        Object objN;
        do {
            objN = N();
            if (!(objN instanceof t1)) {
                if (!(objN instanceof i1) || ((i1) objN).b() == null) {
                    return;
                }
                t1Var.o();
                return;
            }
            if (objN != t1Var) {
                return;
            }
        } while (!f4153b.compareAndSet(this, objN, v1.f4166g));
    }

    @Override // f.w.g.b, f.w.g
    public <E extends g.b> E get(g.c<E> cVar) {
        return (E) n1.a.c(this, cVar);
    }

    @Override // f.w.g.b
    public final g.c<?> getKey() {
        return n1.f4054e;
    }

    public final void h0(o oVar) {
        this._parentHandle = oVar;
    }

    @Override // g.a.n1
    public boolean isActive() {
        Object objN = N();
        return (objN instanceof i1) && ((i1) objN).isActive();
    }

    @Override // g.a.c2
    public CancellationException k() {
        Throwable thE;
        Object objN = N();
        if (objN instanceof c) {
            thE = ((c) objN).e();
        } else if (objN instanceof t) {
            thE = ((t) objN).a;
        } else {
            if (objN instanceof i1) {
                throw new IllegalStateException(("Cannot be cancelling child in this state: " + objN).toString());
            }
            thE = null;
        }
        CancellationException cancellationException = thE instanceof CancellationException ? (CancellationException) thE : null;
        if (cancellationException != null) {
            return cancellationException;
        }
        return new o1("Parent job is " + j0(objN), thE, this);
    }

    protected final CancellationException k0(Throwable th, String str) {
        CancellationException o1Var = th instanceof CancellationException ? (CancellationException) th : null;
        if (o1Var == null) {
            if (str == null) {
                str = A();
            }
            o1Var = new o1(str, th, this);
        }
        return o1Var;
    }

    @Override // g.a.n1
    public final y0 m(boolean z, boolean z2, f.z.c.l<? super Throwable, f.t> lVar) {
        t1 t1VarW = W(lVar, z);
        while (true) {
            Object objN = N();
            if (objN instanceof a1) {
                a1 a1Var = (a1) objN;
                if (!a1Var.isActive()) {
                    e0(a1Var);
                } else if (f4153b.compareAndSet(this, objN, t1VarW)) {
                    return t1VarW;
                }
            } else {
                if (!(objN instanceof i1)) {
                    if (z2) {
                        t tVar = objN instanceof t ? (t) objN : null;
                        lVar.c(tVar != null ? tVar.a : null);
                    }
                    return a2.f4009b;
                }
                z1 z1VarB = ((i1) objN).b();
                if (z1VarB != null) {
                    y0 y0Var = a2.f4009b;
                    if (z && (objN instanceof c)) {
                        synchronized (objN) {
                            thE = ((c) objN).e();
                            if (thE == null || ((lVar instanceof p) && !((c) objN).g())) {
                                if (i(objN, z1VarB, t1VarW)) {
                                    if (thE == null) {
                                        return t1VarW;
                                    }
                                    y0Var = t1VarW;
                                }
                            }
                            f.t tVar2 = f.t.a;
                        }
                    }
                    if (thE != null) {
                        if (z2) {
                            lVar.c(thE);
                        }
                        return y0Var;
                    }
                    if (i(objN, z1VarB, t1VarW)) {
                        return t1VarW;
                    }
                } else {
                    if (objN == null) {
                        throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    }
                    f0((t1) objN);
                }
            }
        }
    }

    public final String m0() {
        return X() + '{' + j0(N()) + '}';
    }

    @Override // f.w.g
    public f.w.g minusKey(g.c<?> cVar) {
        return n1.a.e(this, cVar);
    }

    @Override // g.a.n1
    public final CancellationException n() {
        Object objN = N();
        if (!(objN instanceof c)) {
            if (objN instanceof i1) {
                throw new IllegalStateException(("Job is still new or active: " + this).toString());
            }
            if (objN instanceof t) {
                return l0(this, ((t) objN).a, null, 1, null);
            }
            return new o1(m0.a(this) + " has completed normally", null, this);
        }
        Throwable thE = ((c) objN).e();
        if (thE != null) {
            CancellationException cancellationExceptionK0 = k0(thE, m0.a(this) + " is cancelling");
            if (cancellationExceptionK0 != null) {
                return cancellationExceptionK0;
            }
        }
        throw new IllegalStateException(("Job is still new or active: " + this).toString());
    }

    @Override // g.a.n1
    public void o(CancellationException cancellationException) throws Throwable {
        if (cancellationException == null) {
            cancellationException = new o1(A(), null, this);
        }
        x(cancellationException);
    }

    @Override // f.w.g
    public f.w.g plus(f.w.g gVar) {
        return n1.a.f(this, gVar);
    }

    @Override // g.a.q
    public final void q(c2 c2Var) throws Throwable {
        w(c2Var);
    }

    protected void s(Object obj) {
    }

    @Override // g.a.n1
    public final boolean start() {
        int iI0;
        do {
            iI0 = i0(N());
            if (iI0 == 0) {
                return false;
            }
        } while (iI0 != 1);
        return true;
    }

    public final Object t(f.w.d<Object> dVar) throws Throwable {
        Object objN;
        do {
            objN = N();
            if (!(objN instanceof i1)) {
                if (!(objN instanceof t)) {
                    return v1.h(objN);
                }
                Throwable th = ((t) objN).a;
                if (l0.d() && (dVar instanceof f.w.k.a.e)) {
                    throw g.a.n2.a0.j(th, (f.w.k.a.e) dVar);
                }
                throw th;
            }
        } while (i0(objN) < 0);
        return v(dVar);
    }

    public String toString() {
        return m0() + '@' + m0.b(this);
    }

    @Override // g.a.n1
    public final o u(q qVar) {
        return (o) n1.a.d(this, true, false, new p(qVar), 2, null);
    }

    public final boolean w(Object obj) throws Throwable {
        Object objU = v1.a;
        if (K() && (objU = y(obj)) == v1.f4161b) {
            return true;
        }
        if (objU == v1.a) {
            objU = U(obj);
        }
        if (objU == v1.a || objU == v1.f4161b) {
            return true;
        }
        if (objU == v1.f4163d) {
            return false;
        }
        s(objU);
        return true;
    }

    public void x(Throwable th) throws Throwable {
        w(th);
    }
}
