package h.a;

import g.e0.f;
import h.a.k1.i;
import h.a.r0;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
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 */
@g.i
public class v0 implements r0, b1 {

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

    /* JADX INFO: compiled from: JobSupport.kt */
    @g.i
    public static final class a extends u0 {

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public final v0 f4333h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public final b f4334i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        public final j f4335j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        public final Object f4336k;

        public a(v0 v0Var, b bVar, j jVar, Object obj) {
            this.f4333h = v0Var;
            this.f4334i = bVar;
            this.f4335j = jVar;
            this.f4336k = obj;
        }

        @Override // g.h0.b.l
        public /* bridge */ /* synthetic */ g.a0 invoke(Throwable th) {
            l(th);
            return g.a0.a;
        }

        @Override // h.a.n
        public void l(Throwable th) {
            v0 v0Var = this.f4333h;
            b bVar = this.f4334i;
            j jVar = this.f4335j;
            Object obj = this.f4336k;
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = v0.f4332b;
            j jVarZ = v0Var.z(jVar);
            if (jVarZ == null || !v0Var.G(bVar, jVarZ, obj)) {
                v0Var.j(v0Var.t(bVar, obj));
            }
        }
    }

    /* JADX INFO: compiled from: JobSupport.kt */
    @g.i
    public static final class b implements m0 {
        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 */
        public final y0 f4337b;

        public b(y0 y0Var, boolean z, Throwable th) {
            this.f4337b = y0Var;
            this._isCompleting = z ? 1 : 0;
            this._rootCause = th;
        }

        @Override // h.a.m0
        public boolean a() {
            return ((Throwable) this._rootCause) == null;
        }

        /* JADX WARN: Multi-variable type inference failed */
        public final void b(Throwable th) {
            Throwable th2 = (Throwable) this._rootCause;
            if (th2 == null) {
                this._rootCause = th;
                return;
            }
            if (th == th2) {
                return;
            }
            Object obj = this._exceptionsHolder;
            if (obj == null) {
                this._exceptionsHolder = th;
                return;
            }
            if (!(obj instanceof Throwable)) {
                if (!(obj instanceof ArrayList)) {
                    throw new IllegalStateException(g.h0.c.l.k("State is ", obj).toString());
                }
                ((ArrayList) obj).add(th);
            } else {
                if (th == obj) {
                    return;
                }
                ArrayList<Throwable> arrayListD = d();
                arrayListD.add(obj);
                arrayListD.add(th);
                this._exceptionsHolder = arrayListD;
            }
        }

        @Override // h.a.m0
        public y0 c() {
            return this.f4337b;
        }

        public final ArrayList<Throwable> d() {
            return new ArrayList<>(4);
        }

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

        public final boolean f() {
            return ((Throwable) this._rootCause) != null;
        }

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

        public final boolean h() {
            return this._exceptionsHolder == w0.f4344e;
        }

        public final List<Throwable> i(Throwable th) {
            ArrayList arrayListD;
            Object obj = this._exceptionsHolder;
            if (obj == null) {
                arrayListD = d();
            } else if (obj instanceof Throwable) {
                ArrayList arrayListD2 = d();
                arrayListD2.add(obj);
                arrayListD = arrayListD2;
            } else {
                if (!(obj instanceof ArrayList)) {
                    throw new IllegalStateException(g.h0.c.l.k("State is ", obj).toString());
                }
                arrayListD = (ArrayList) obj;
            }
            Throwable th2 = (Throwable) this._rootCause;
            if (th2 != null) {
                arrayListD.add(0, th2);
            }
            if (th != null && !g.h0.c.l.a(th, th2)) {
                arrayListD.add(th);
            }
            this._exceptionsHolder = w0.f4344e;
            return arrayListD;
        }

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

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Type inference failed for: r1v2, types: [boolean, int] */
        public String toString() {
            StringBuilder sbB = d.b.a.a.a.B("Finishing[cancelling=");
            sbB.append(f());
            sbB.append(", completing=");
            sbB.append((boolean) this._isCompleting);
            sbB.append(", rootCause=");
            sbB.append((Throwable) this._rootCause);
            sbB.append(", exceptions=");
            sbB.append(this._exceptionsHolder);
            sbB.append(", list=");
            sbB.append(this.f4337b);
            sbB.append(']');
            return sbB.toString();
        }
    }

    /* JADX INFO: compiled from: LockFreeLinkedList.kt */
    @g.i
    public static final class c extends i.a {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final /* synthetic */ v0 f4338d;

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public c(h.a.k1.i iVar, v0 v0Var, Object obj) {
            super(iVar);
            this.f4338d = v0Var;
            this.f4339e = obj;
        }

        @Override // h.a.k1.c
        public Object c(h.a.k1.i iVar) {
            if (this.f4338d.v() == this.f4339e) {
                return null;
            }
            return h.a.k1.h.a;
        }
    }

    public final void A(y0 y0Var, Throwable th) throws Throwable {
        o oVar;
        o oVar2 = null;
        for (h.a.k1.i iVarH = (h.a.k1.i) y0Var.g(); !g.h0.c.l.a(iVarH, y0Var); iVarH = iVarH.h()) {
            if (iVarH instanceof t0) {
                u0 u0Var = (u0) iVarH;
                try {
                    u0Var.l(th);
                } catch (Throwable th2) {
                    if (oVar2 == null) {
                        oVar = null;
                    } else {
                        f.a.y.a.m(oVar2, th2);
                        oVar = oVar2;
                    }
                    if (oVar == null) {
                        oVar2 = new o("Exception in completion handler " + u0Var + " for " + this, th2);
                    }
                }
            }
        }
        if (oVar2 != null) {
            w(oVar2);
        }
        n(th);
    }

    public void B(Object obj) {
    }

    public final void C(u0 u0Var) {
        y0 y0Var = new y0();
        h.a.k1.i.f4255e.lazySet(y0Var, u0Var);
        h.a.k1.i.f4254b.lazySet(y0Var, u0Var);
        while (true) {
            if (u0Var.g() != u0Var) {
                break;
            } else if (h.a.k1.i.f4254b.compareAndSet(u0Var, u0Var, y0Var)) {
                y0Var.f(u0Var);
                break;
            }
        }
        f4332b.compareAndSet(this, u0Var, u0Var.h());
    }

    public final String D(Object obj) {
        if (!(obj instanceof b)) {
            return obj instanceof m0 ? ((m0) obj).a() ? "Active" : "New" : obj instanceof l ? "Cancelled" : "Completed";
        }
        b bVar = (b) obj;
        return bVar.f() ? "Cancelling" : bVar.g() ? "Completing" : "Active";
    }

    public final CancellationException E(Throwable th, String str) {
        CancellationException s0Var = th instanceof CancellationException ? (CancellationException) th : null;
        if (s0Var == null) {
            if (str == null) {
                str = q();
            }
            s0Var = new s0(str, th, this);
        }
        return s0Var;
    }

    public final Object F(Object obj, Object obj2) {
        if (!(obj instanceof m0)) {
            return w0.a;
        }
        boolean z = true;
        if (((obj instanceof e0) || (obj instanceof u0)) && !(obj instanceof j) && !(obj2 instanceof l)) {
            m0 m0Var = (m0) obj;
            if (f4332b.compareAndSet(this, m0Var, obj2 instanceof m0 ? new n0((m0) obj2) : obj2)) {
                B(obj2);
                r(m0Var, obj2);
            } else {
                z = false;
            }
            return z ? obj2 : w0.f4342c;
        }
        m0 m0Var2 = (m0) obj;
        y0 y0VarU = u(m0Var2);
        if (y0VarU == null) {
            return w0.f4342c;
        }
        j jVarZ = null;
        b bVar = m0Var2 instanceof b ? (b) m0Var2 : null;
        if (bVar == null) {
            bVar = new b(y0VarU, false, null);
        }
        synchronized (bVar) {
            if (bVar.g()) {
                return w0.a;
            }
            bVar.j(true);
            if (bVar != m0Var2 && !f4332b.compareAndSet(this, m0Var2, bVar)) {
                return w0.f4342c;
            }
            boolean zF = bVar.f();
            l lVar = obj2 instanceof l ? (l) obj2 : null;
            if (lVar != null) {
                bVar.b(lVar.f4276b);
            }
            Throwable thE = bVar.e();
            if (!(true ^ zF)) {
                thE = null;
            }
            if (thE != null) {
                A(y0VarU, thE);
            }
            j jVar = m0Var2 instanceof j ? (j) m0Var2 : null;
            if (jVar == null) {
                y0 y0VarC = m0Var2.c();
                if (y0VarC != null) {
                    jVarZ = z(y0VarC);
                }
            } else {
                jVarZ = jVar;
            }
            return (jVarZ == null || !G(bVar, jVarZ, obj2)) ? t(bVar, obj2) : w0.f4341b;
        }
    }

    public final boolean G(b bVar, j jVar, Object obj) {
        while (f.a.y.a.n0(null, false, false, new a(this, bVar, jVar, obj), 1, null) == z0.f4348b) {
            jVar = z(jVar);
            if (jVar == null) {
                return false;
            }
        }
        return true;
    }

    @Override // h.a.r0
    public boolean a() {
        Object objV = v();
        return (objV instanceof m0) && ((m0) objV).a();
    }

    @Override // g.e0.f
    public <R> R fold(R r, g.h0.b.p<? super R, ? super f.a, ? extends R> pVar) {
        return (R) f.a.C0077a.a(this, r, pVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:65:0x00a9  */
    /* JADX WARN: Removed duplicated region for block: B:92:0x00a3 A[SYNTHETIC] */
    @Override // h.a.r0
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final h.a.d0 g(boolean r8, boolean r9, g.h0.b.l<? super java.lang.Throwable, g.a0> r10) {
        /*
            r7 = this;
            r0 = 0
            if (r8 == 0) goto L14
            boolean r1 = r10 instanceof h.a.t0
            if (r1 == 0) goto Lb
            r1 = r10
            h.a.t0 r1 = (h.a.t0) r1
            goto Lc
        Lb:
            r1 = r0
        Lc:
            if (r1 != 0) goto L27
            h.a.p0 r1 = new h.a.p0
            r1.<init>(r10)
            goto L27
        L14:
            boolean r1 = r10 instanceof h.a.u0
            if (r1 == 0) goto L1c
            r1 = r10
            h.a.u0 r1 = (h.a.u0) r1
            goto L1d
        L1c:
            r1 = r0
        L1d:
            if (r1 != 0) goto L20
            r1 = r0
        L20:
            if (r1 != 0) goto L27
            h.a.q0 r1 = new h.a.q0
            r1.<init>(r10)
        L27:
            r1.f4329g = r7
        L29:
            java.lang.Object r2 = r7.v()
            boolean r3 = r2 instanceof h.a.e0
            if (r3 == 0) goto L57
            r3 = r2
            h.a.e0 r3 = (h.a.e0) r3
            boolean r4 = r3.f4210b
            if (r4 == 0) goto L41
            java.util.concurrent.atomic.AtomicReferenceFieldUpdater r3 = h.a.v0.f4332b
            boolean r2 = r3.compareAndSet(r7, r2, r1)
            if (r2 == 0) goto L29
            return r1
        L41:
            h.a.y0 r2 = new h.a.y0
            r2.<init>()
            boolean r4 = r3.f4210b
            if (r4 == 0) goto L4b
            goto L51
        L4b:
            h.a.l0 r4 = new h.a.l0
            r4.<init>(r2)
            r2 = r4
        L51:
            java.util.concurrent.atomic.AtomicReferenceFieldUpdater r4 = h.a.v0.f4332b
            r4.compareAndSet(r7, r3, r2)
            goto L29
        L57:
            boolean r3 = r2 instanceof h.a.m0
            if (r3 == 0) goto Lb0
            r3 = r2
            h.a.m0 r3 = (h.a.m0) r3
            h.a.y0 r3 = r3.c()
            if (r3 != 0) goto L6f
            java.lang.String r3 = "null cannot be cast to non-null type kotlinx.coroutines.JobNode"
            java.util.Objects.requireNonNull(r2, r3)
            h.a.u0 r2 = (h.a.u0) r2
            r7.C(r2)
            goto L29
        L6f:
            h.a.z0 r4 = h.a.z0.f4348b
            if (r8 == 0) goto La0
            boolean r5 = r2 instanceof h.a.v0.b
            if (r5 == 0) goto La0
            monitor-enter(r2)
            r5 = r2
            h.a.v0$b r5 = (h.a.v0.b) r5     // Catch: java.lang.Throwable -> L9d
            java.lang.Throwable r5 = r5.e()     // Catch: java.lang.Throwable -> L9d
            if (r5 == 0) goto L8e
            boolean r6 = r10 instanceof h.a.j     // Catch: java.lang.Throwable -> L9d
            if (r6 == 0) goto L9b
            r6 = r2
            h.a.v0$b r6 = (h.a.v0.b) r6     // Catch: java.lang.Throwable -> L9d
            boolean r6 = r6.g()     // Catch: java.lang.Throwable -> L9d
            if (r6 != 0) goto L9b
        L8e:
            boolean r4 = r7.h(r2, r3, r1)     // Catch: java.lang.Throwable -> L9d
            if (r4 != 0) goto L96
            monitor-exit(r2)
            goto L29
        L96:
            if (r5 != 0) goto L9a
            monitor-exit(r2)
            return r1
        L9a:
            r4 = r1
        L9b:
            monitor-exit(r2)
            goto La1
        L9d:
            r8 = move-exception
            monitor-exit(r2)
            throw r8
        La0:
            r5 = r0
        La1:
            if (r5 == 0) goto La9
            if (r9 == 0) goto La8
            r10.invoke(r5)
        La8:
            return r4
        La9:
            boolean r2 = r7.h(r2, r3, r1)
            if (r2 == 0) goto L29
            return r1
        Lb0:
            if (r9 == 0) goto Lc2
            boolean r8 = r2 instanceof h.a.l
            if (r8 == 0) goto Lb9
            h.a.l r2 = (h.a.l) r2
            goto Lba
        Lb9:
            r2 = r0
        Lba:
            if (r2 != 0) goto Lbd
            goto Lbf
        Lbd:
            java.lang.Throwable r0 = r2.f4276b
        Lbf:
            r10.invoke(r0)
        Lc2:
            h.a.z0 r8 = h.a.z0.f4348b
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: h.a.v0.g(boolean, boolean, g.h0.b.l):h.a.d0");
    }

    @Override // g.e0.f.a, g.e0.f
    public <E extends f.a> E get(f.b<E> bVar) {
        return (E) f.a.C0077a.b(this, bVar);
    }

    @Override // g.e0.f.a
    public final f.b<?> getKey() {
        return r0.a.f4327b;
    }

    public final boolean h(Object obj, y0 y0Var, u0 u0Var) {
        char c2;
        c cVar = new c(u0Var, this, obj);
        do {
            h.a.k1.i iVarI = y0Var.i();
            h.a.k1.i.f4255e.lazySet(u0Var, iVarI);
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = h.a.k1.i.f4254b;
            atomicReferenceFieldUpdater.lazySet(u0Var, y0Var);
            cVar.f4258c = y0Var;
            c2 = !atomicReferenceFieldUpdater.compareAndSet(iVarI, y0Var, cVar) ? (char) 0 : cVar.a(iVarI) == null ? (char) 1 : (char) 2;
            if (c2 == 1) {
                return true;
            }
        } while (c2 != 2);
        return false;
    }

    @Override // h.a.r0
    public final CancellationException i() {
        Object objV = v();
        if (!(objV instanceof b)) {
            if (objV instanceof m0) {
                throw new IllegalStateException(g.h0.c.l.k("Job is still new or active: ", this).toString());
            }
            return objV instanceof l ? E(((l) objV).f4276b, null) : new s0(g.h0.c.l.k(getClass().getSimpleName(), " has completed normally"), null, this);
        }
        Throwable thE = ((b) objV).e();
        CancellationException cancellationExceptionE = thE != null ? E(thE, g.h0.c.l.k(getClass().getSimpleName(), " is cancelling")) : null;
        if (cancellationExceptionE != null) {
            return cancellationExceptionE;
        }
        throw new IllegalStateException(g.h0.c.l.k("Job is still new or active: ", this).toString());
    }

    public void j(Object obj) {
    }

    /* JADX WARN: Code restructure failed: missing block: B:55:0x00ad, code lost:
    
        r0 = r9;
     */
    /* JADX WARN: Removed duplicated region for block: B:42:0x007a  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x0082 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:77:0x0008 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean k(java.lang.Object r9) throws java.lang.Throwable {
        /*
            r8 = this;
            h.a.k1.r r0 = h.a.w0.a
            r1 = 0
            r2 = 1
            if (r0 != r0) goto Lae
            r0 = 0
            r3 = r0
        L8:
            java.lang.Object r4 = r8.v()
            boolean r5 = r4 instanceof h.a.v0.b
            if (r5 == 0) goto L53
            monitor-enter(r4)
            r5 = r4
            h.a.v0$b r5 = (h.a.v0.b) r5     // Catch: java.lang.Throwable -> L50
            boolean r5 = r5.h()     // Catch: java.lang.Throwable -> L50
            if (r5 == 0) goto L1f
            h.a.k1.r r9 = h.a.w0.f4343d     // Catch: java.lang.Throwable -> L50
            monitor-exit(r4)
            goto Lad
        L1f:
            r5 = r4
            h.a.v0$b r5 = (h.a.v0.b) r5     // Catch: java.lang.Throwable -> L50
            boolean r5 = r5.f()     // Catch: java.lang.Throwable -> L50
            if (r9 != 0) goto L2a
            if (r5 != 0) goto L36
        L2a:
            if (r3 != 0) goto L30
            java.lang.Throwable r3 = r8.s(r9)     // Catch: java.lang.Throwable -> L50
        L30:
            r9 = r4
            h.a.v0$b r9 = (h.a.v0.b) r9     // Catch: java.lang.Throwable -> L50
            r9.b(r3)     // Catch: java.lang.Throwable -> L50
        L36:
            r9 = r4
            h.a.v0$b r9 = (h.a.v0.b) r9     // Catch: java.lang.Throwable -> L50
            java.lang.Throwable r9 = r9.e()     // Catch: java.lang.Throwable -> L50
            r3 = r5 ^ 1
            if (r3 == 0) goto L42
            r0 = r9
        L42:
            monitor-exit(r4)
            if (r0 != 0) goto L46
            goto L4d
        L46:
            h.a.v0$b r4 = (h.a.v0.b) r4
            h.a.y0 r9 = r4.f4337b
            r8.A(r9, r0)
        L4d:
            h.a.k1.r r9 = h.a.w0.a
            goto Lad
        L50:
            r9 = move-exception
            monitor-exit(r4)
            throw r9
        L53:
            boolean r5 = r4 instanceof h.a.m0
            if (r5 == 0) goto Lab
            if (r3 != 0) goto L5d
            java.lang.Throwable r3 = r8.s(r9)
        L5d:
            r5 = r4
            h.a.m0 r5 = (h.a.m0) r5
            boolean r6 = r5.a()
            if (r6 == 0) goto L85
            h.a.y0 r4 = r8.u(r5)
            if (r4 != 0) goto L6d
            goto L7a
        L6d:
            h.a.v0$b r6 = new h.a.v0$b
            r6.<init>(r4, r1, r3)
            java.util.concurrent.atomic.AtomicReferenceFieldUpdater r7 = h.a.v0.f4332b
            boolean r5 = r7.compareAndSet(r8, r5, r6)
            if (r5 != 0) goto L7c
        L7a:
            r4 = r1
            goto L80
        L7c:
            r8.A(r4, r3)
            r4 = r2
        L80:
            if (r4 == 0) goto L8
            h.a.k1.r r9 = h.a.w0.a
            goto Lad
        L85:
            h.a.l r5 = new h.a.l
            r6 = 2
            r5.<init>(r3, r1, r6)
            java.lang.Object r5 = r8.F(r4, r5)
            h.a.k1.r r6 = h.a.w0.a
            if (r5 == r6) goto L9b
            h.a.k1.r r4 = h.a.w0.f4342c
            if (r5 != r4) goto L99
            goto L8
        L99:
            r0 = r5
            goto Lae
        L9b:
            java.lang.IllegalStateException r9 = new java.lang.IllegalStateException
            java.lang.String r0 = "Cannot happen in "
            java.lang.String r0 = g.h0.c.l.k(r0, r4)
            java.lang.String r0 = r0.toString()
            r9.<init>(r0)
            throw r9
        Lab:
            h.a.k1.r r9 = h.a.w0.f4343d
        Lad:
            r0 = r9
        Lae:
            h.a.k1.r r9 = h.a.w0.a
            if (r0 != r9) goto Lb4
        Lb2:
            r1 = r2
            goto Lc2
        Lb4:
            h.a.k1.r r9 = h.a.w0.f4341b
            if (r0 != r9) goto Lb9
            goto Lb2
        Lb9:
            h.a.k1.r r9 = h.a.w0.f4343d
            if (r0 != r9) goto Lbe
            goto Lc2
        Lbe:
            r8.j(r0)
            goto Lb2
        Lc2:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: h.a.v0.k(java.lang.Object):boolean");
    }

    @Override // h.a.b1
    public CancellationException m() {
        Throwable thE;
        Object objV = v();
        if (objV instanceof b) {
            thE = ((b) objV).e();
        } else if (objV instanceof l) {
            thE = ((l) objV).f4276b;
        } else {
            if (objV instanceof m0) {
                throw new IllegalStateException(g.h0.c.l.k("Cannot be cancelling child in this state: ", objV).toString());
            }
            thE = null;
        }
        CancellationException cancellationException = thE instanceof CancellationException ? (CancellationException) thE : null;
        return cancellationException == null ? new s0(g.h0.c.l.k("Parent job is ", D(objV)), thE, this) : cancellationException;
    }

    @Override // g.e0.f
    public g.e0.f minusKey(f.b<?> bVar) {
        return f.a.C0077a.c(this, bVar);
    }

    public final boolean n(Throwable th) {
        if (x()) {
            return true;
        }
        boolean z = th instanceof CancellationException;
        i iVar = (i) this._parentHandle;
        return (iVar == null || iVar == z0.f4348b) ? z : iVar.b(th) || z;
    }

    @Override // h.a.r0
    public void o(CancellationException cancellationException) throws Throwable {
        if (cancellationException == null) {
            cancellationException = new s0(q(), null, this);
        }
        k(cancellationException);
    }

    @Override // g.e0.f
    public g.e0.f plus(g.e0.f fVar) {
        return f.a.C0077a.d(this, fVar);
    }

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

    public final void r(m0 m0Var, Object obj) throws Throwable {
        o oVar;
        i iVar = (i) this._parentHandle;
        if (iVar != null) {
            iVar.dispose();
            this._parentHandle = z0.f4348b;
        }
        l lVar = obj instanceof l ? (l) obj : null;
        Throwable th = lVar == null ? null : lVar.f4276b;
        if (m0Var instanceof u0) {
            try {
                ((u0) m0Var).l(th);
                return;
            } catch (Throwable th2) {
                w(new o("Exception in completion handler " + m0Var + " for " + this, th2));
                return;
            }
        }
        y0 y0VarC = m0Var.c();
        if (y0VarC == null) {
            return;
        }
        o oVar2 = null;
        for (h.a.k1.i iVarH = (h.a.k1.i) y0VarC.g(); !g.h0.c.l.a(iVarH, y0VarC); iVarH = iVarH.h()) {
            if (iVarH instanceof u0) {
                u0 u0Var = (u0) iVarH;
                try {
                    u0Var.l(th);
                } catch (Throwable th3) {
                    if (oVar2 == null) {
                        oVar = null;
                    } else {
                        f.a.y.a.m(oVar2, th3);
                        oVar = oVar2;
                    }
                    if (oVar == null) {
                        oVar2 = new o("Exception in completion handler " + u0Var + " for " + this, th3);
                    }
                }
            }
        }
        if (oVar2 == null) {
            return;
        }
        w(oVar2);
    }

    public final Throwable s(Object obj) {
        if (obj == null ? true : obj instanceof Throwable) {
            Throwable th = (Throwable) obj;
            return th == null ? new s0(q(), null, this) : th;
        }
        Objects.requireNonNull(obj, "null cannot be cast to non-null type kotlinx.coroutines.ParentJob");
        return ((b1) obj).m();
    }

    public final Object t(b bVar, Object obj) throws Throwable {
        Object obj2 = null;
        Throwable s0Var = null;
        l lVar = obj instanceof l ? (l) obj : null;
        Throwable th = lVar == null ? null : lVar.f4276b;
        synchronized (bVar) {
            bVar.f();
            List<Throwable> listI = bVar.i(th);
            if (!listI.isEmpty()) {
                Iterator<T> it = listI.iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    Object next = it.next();
                    if (!(((Throwable) next) instanceof CancellationException)) {
                        obj2 = next;
                        break;
                    }
                }
                s0Var = (Throwable) obj2;
                if (s0Var == null) {
                    s0Var = listI.get(0);
                }
            } else if (bVar.f()) {
                s0Var = new s0(q(), null, this);
            }
            if (s0Var != null && listI.size() > 1) {
                Set setNewSetFromMap = Collections.newSetFromMap(new IdentityHashMap(listI.size()));
                for (Throwable th2 : listI) {
                    if (th2 != s0Var && th2 != s0Var && !(th2 instanceof CancellationException) && setNewSetFromMap.add(th2)) {
                        f.a.y.a.m(s0Var, th2);
                    }
                }
            }
        }
        if (s0Var != null && s0Var != th) {
            obj = new l(s0Var, false, 2);
        }
        if (s0Var != null && n(s0Var)) {
            Objects.requireNonNull(obj, "null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
            l.a.compareAndSet((l) obj, 0, 1);
        }
        B(obj);
        f4332b.compareAndSet(this, bVar, obj instanceof m0 ? new n0((m0) obj) : obj);
        r(bVar, obj);
        return obj;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(y() + '{' + D(v()) + '}');
        sb.append('@');
        sb.append(u.b(this));
        return sb.toString();
    }

    public final y0 u(m0 m0Var) {
        y0 y0VarC = m0Var.c();
        if (y0VarC != null) {
            return y0VarC;
        }
        if (m0Var instanceof e0) {
            return new y0();
        }
        if (!(m0Var instanceof u0)) {
            throw new IllegalStateException(g.h0.c.l.k("State should have list: ", m0Var).toString());
        }
        C((u0) m0Var);
        return null;
    }

    public final Object v() {
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof h.a.k1.n)) {
                return obj;
            }
            ((h.a.k1.n) obj).a(this);
        }
    }

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

    public boolean x() {
        return false;
    }

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

    public final j z(h.a.k1.i iVar) {
        while (iVar.j()) {
            iVar = iVar.i();
        }
        while (true) {
            iVar = iVar.h();
            if (!iVar.j()) {
                if (iVar instanceof j) {
                    return (j) iVar;
                }
                if (iVar instanceof y0) {
                    return null;
                }
            }
        }
    }
}
