package kotlinx.coroutines;

import h.u.g;
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;
import kotlinx.coroutines.o2.j;
import kotlinx.coroutines.s1;

/* JADX INFO: compiled from: JobSupport.kt */
/* JADX INFO: loaded from: classes.dex */
public class z1 implements s1, u, f2 {
    private static final /* synthetic */ AtomicReferenceFieldUpdater b = AtomicReferenceFieldUpdater.newUpdater(z1.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 o<T> {
        private final z1 l;

        public a(h.u.d<? super T> dVar, z1 z1Var) {
            super(dVar, 1);
            this.l = z1Var;
        }

        @Override // kotlinx.coroutines.o
        protected String E() {
            return "AwaitContinuation";
        }

        @Override // kotlinx.coroutines.o
        public Throwable u(s1 s1Var) {
            Throwable thE;
            Object objX = this.l.X();
            return (!(objX instanceof c) || (thE = ((c) objX).e()) == null) ? objX instanceof b0 ? ((b0) objX).a : s1Var.s() : thE;
        }
    }

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

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

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

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

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

        public b(z1 z1Var, c cVar, t tVar, Object obj) {
            this.f1176h = z1Var;
            this.f1177i = cVar;
            this.f1178j = tVar;
            this.f1179k = obj;
        }

        @Override // h.x.c.l
        public /* bridge */ /* synthetic */ h.r invoke(Throwable th) {
            q(th);
            return h.r.a;
        }

        @Override // kotlinx.coroutines.d0
        public void q(Throwable th) {
            this.f1176h.N(this.f1177i, this.f1178j, this.f1179k);
        }
    }

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

        public c(c2 c2Var, boolean z, Throwable th) {
            this.b = c2Var;
            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 (!(objD instanceof ArrayList)) {
                    throw new IllegalStateException(h.x.d.k.i("State is ", objD).toString());
                }
                ((ArrayList) objD).add(th);
            } else {
                if (th == objD) {
                    return;
                }
                ArrayList<Throwable> arrayListC = c();
                arrayListC.add(objD);
                arrayListC.add(th);
                h.r rVar = h.r.a;
                k(arrayListC);
            }
        }

        @Override // kotlinx.coroutines.n1
        public c2 b() {
            return this.b;
        }

        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() == a2.f1070e;
        }

        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(h.x.d.k.i("State is ", objD).toString());
                }
                arrayListC = (ArrayList) objD;
            }
            Throwable thE = e();
            if (thE != null) {
                arrayListC.add(0, thE);
            }
            if (th != null && !h.x.d.k.a(th, thE)) {
                arrayListC.add(th);
            }
            k(a2.f1070e);
            return arrayListC;
        }

        @Override // kotlinx.coroutines.n1
        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 j.a {

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

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(kotlinx.coroutines.o2.j jVar, z1 z1Var, Object obj) {
            super(jVar);
            this.f1180d = z1Var;
            this.f1181e = obj;
        }

        @Override // kotlinx.coroutines.o2.c
        /* JADX INFO: renamed from: i, reason: merged with bridge method [inline-methods] */
        public Object g(kotlinx.coroutines.o2.j jVar) {
            if (this.f1180d.X() == this.f1181e) {
                return null;
            }
            return kotlinx.coroutines.o2.i.a();
        }
    }

    public z1(boolean z) {
        this._state = z ? a2.f1072g : a2.f1071f;
        this._parentHandle = null;
    }

    private final Object A0(Object obj, Object obj2) {
        return !(obj instanceof n1) ? a2.a : ((!(obj instanceof e1) && !(obj instanceof y1)) || (obj instanceof t) || (obj2 instanceof b0)) ? B0((n1) obj, obj2) : y0((n1) obj, obj2) ? obj2 : a2.f1068c;
    }

    private final Object B0(n1 n1Var, Object obj) throws Throwable {
        c2 c2VarV = V(n1Var);
        if (c2VarV == null) {
            return a2.f1068c;
        }
        c cVar = n1Var instanceof c ? (c) n1Var : null;
        if (cVar == null) {
            cVar = new c(c2VarV, false, null);
        }
        synchronized (cVar) {
            if (cVar.g()) {
                return a2.a;
            }
            cVar.j(true);
            if (cVar != n1Var && !b.compareAndSet(this, n1Var, cVar)) {
                return a2.f1068c;
            }
            if (r0.a() && !(!cVar.h())) {
                throw new AssertionError();
            }
            boolean zF = cVar.f();
            b0 b0Var = obj instanceof b0 ? (b0) obj : null;
            if (b0Var != null) {
                cVar.a(b0Var.a);
            }
            Throwable thE = true ^ zF ? cVar.e() : null;
            h.r rVar = h.r.a;
            if (thE != null) {
                k0(c2VarV, thE);
            }
            t tVarQ = Q(n1Var);
            return (tVarQ == null || !C0(cVar, tVarQ, obj)) ? P(cVar, obj) : a2.b;
        }
    }

    private final Object C(h.u.d<Object> dVar) {
        a aVar = new a(h.u.i.c.c(dVar), this);
        aVar.y();
        p.a(aVar, b0(new g2(aVar)));
        Object objV = aVar.v();
        if (objV == h.u.i.d.d()) {
            h.u.j.a.h.c(dVar);
        }
        return objV;
    }

    private final boolean C0(c cVar, t tVar, Object obj) {
        while (s1.a.d(tVar.f1167h, false, false, new b(this, cVar, tVar, obj), 1, null) == d2.b) {
            tVar = j0(tVar);
            if (tVar == null) {
                return false;
            }
        }
        return true;
    }

    private final Object I(Object obj) {
        Object objA0;
        do {
            Object objX = X();
            if (!(objX instanceof n1) || ((objX instanceof c) && ((c) objX).g())) {
                return a2.a;
            }
            objA0 = A0(objX, new b0(O(obj), false, 2, null));
        } while (objA0 == a2.f1068c);
        return objA0;
    }

    private final boolean J(Throwable th) {
        if (d0()) {
            return true;
        }
        boolean z = th instanceof CancellationException;
        s sVarW = W();
        return (sVarW == null || sVarW == d2.b) ? z : sVarW.c(th) || z;
    }

    private final void M(n1 n1Var, Object obj) throws Throwable {
        s sVarW = W();
        if (sVarW != null) {
            sVarW.dispose();
            s0(d2.b);
        }
        b0 b0Var = obj instanceof b0 ? (b0) obj : null;
        Throwable th = b0Var != null ? b0Var.a : null;
        if (!(n1Var instanceof y1)) {
            c2 c2VarB = n1Var.b();
            if (c2VarB == null) {
                return;
            }
            l0(c2VarB, th);
            return;
        }
        try {
            ((y1) n1Var).q(th);
        } catch (Throwable th2) {
            Z(new e0("Exception in completion handler " + n1Var + " for " + this, th2));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void N(c cVar, t tVar, Object obj) {
        if (r0.a()) {
            if (!(X() == cVar)) {
                throw new AssertionError();
            }
        }
        t tVarJ0 = j0(tVar);
        if (tVarJ0 == null || !C0(cVar, tVarJ0, obj)) {
            w(P(cVar, obj));
        }
    }

    private final Throwable O(Object obj) {
        if (obj == null ? true : obj instanceof Throwable) {
            Throwable th = (Throwable) obj;
            return th == null ? new t1(K(), null, this) : th;
        }
        if (obj != null) {
            return ((f2) obj).q();
        }
        throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.ParentJob");
    }

    private final Object P(c cVar, Object obj) throws Throwable {
        boolean zF;
        Throwable thS;
        boolean z = true;
        if (r0.a()) {
            if (!(X() == cVar)) {
                throw new AssertionError();
            }
        }
        if (r0.a() && !(!cVar.h())) {
            throw new AssertionError();
        }
        if (r0.a() && !cVar.g()) {
            throw new AssertionError();
        }
        b0 b0Var = obj instanceof b0 ? (b0) obj : null;
        Throwable th = b0Var == null ? null : b0Var.a;
        synchronized (cVar) {
            zF = cVar.f();
            List<Throwable> listI = cVar.i(th);
            thS = S(cVar, listI);
            if (thS != null) {
                v(thS, listI);
            }
        }
        if (thS != null && thS != th) {
            obj = new b0(thS, false, 2, null);
        }
        if (thS != null) {
            if (!J(thS) && !Y(thS)) {
                z = false;
            }
            if (z) {
                if (obj == null) {
                    throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
                }
                ((b0) obj).b();
            }
        }
        if (!zF) {
            m0(thS);
        }
        n0(obj);
        boolean zCompareAndSet = b.compareAndSet(this, cVar, a2.g(obj));
        if (r0.a() && !zCompareAndSet) {
            throw new AssertionError();
        }
        M(cVar, obj);
        return obj;
    }

    private final t Q(n1 n1Var) {
        t tVar = n1Var instanceof t ? (t) n1Var : null;
        if (tVar != null) {
            return tVar;
        }
        c2 c2VarB = n1Var.b();
        if (c2VarB == null) {
            return null;
        }
        return j0(c2VarB);
    }

    private final Throwable R(Object obj) {
        b0 b0Var = obj instanceof b0 ? (b0) obj : null;
        if (b0Var == null) {
            return null;
        }
        return b0Var.a;
    }

    private final Throwable S(c cVar, List<? extends Throwable> list) {
        Object obj = null;
        if (list.isEmpty()) {
            if (cVar.f()) {
                return new t1(K(), 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 c2 V(n1 n1Var) {
        c2 c2VarB = n1Var.b();
        if (c2VarB != null) {
            return c2VarB;
        }
        if (n1Var instanceof e1) {
            return new c2();
        }
        if (!(n1Var instanceof y1)) {
            throw new IllegalStateException(h.x.d.k.i("State should have list: ", n1Var).toString());
        }
        q0((y1) n1Var);
        return null;
    }

    private final Object e0(Object obj) throws Throwable {
        Throwable thO = null;
        while (true) {
            Object objX = X();
            if (objX instanceof c) {
                synchronized (objX) {
                    if (((c) objX).h()) {
                        return a2.f1069d;
                    }
                    boolean zF = ((c) objX).f();
                    if (obj != null || !zF) {
                        if (thO == null) {
                            thO = O(obj);
                        }
                        ((c) objX).a(thO);
                    }
                    Throwable thE = zF ^ true ? ((c) objX).e() : null;
                    if (thE != null) {
                        k0(((c) objX).b(), thE);
                    }
                    return a2.a;
                }
            }
            if (!(objX instanceof n1)) {
                return a2.f1069d;
            }
            if (thO == null) {
                thO = O(obj);
            }
            n1 n1Var = (n1) objX;
            if (!n1Var.isActive()) {
                Object objA0 = A0(objX, new b0(thO, false, 2, null));
                if (objA0 == a2.a) {
                    throw new IllegalStateException(h.x.d.k.i("Cannot happen in ", objX).toString());
                }
                if (objA0 != a2.f1068c) {
                    return objA0;
                }
            } else if (z0(n1Var, thO)) {
                return a2.a;
            }
        }
    }

    private final y1 h0(h.x.c.l<? super Throwable, h.r> lVar, boolean z) {
        if (z) {
            r1Var = lVar instanceof u1 ? (u1) lVar : null;
            if (r1Var == null) {
                r1Var = new q1(lVar);
            }
        } else {
            y1 y1Var = lVar instanceof y1 ? (y1) lVar : null;
            if (y1Var != null) {
                if (r0.a() && !(!(y1Var instanceof u1))) {
                    throw new AssertionError();
                }
                r1Var = y1Var;
            }
            if (r1Var == null) {
                r1Var = new r1(lVar);
            }
        }
        r1Var.s(this);
        return r1Var;
    }

    private final t j0(kotlinx.coroutines.o2.j jVar) {
        while (jVar.l()) {
            jVar = jVar.k();
        }
        while (true) {
            jVar = jVar.j();
            if (!jVar.l()) {
                if (jVar instanceof t) {
                    return (t) jVar;
                }
                if (jVar instanceof c2) {
                    return null;
                }
            }
        }
    }

    private final void k0(c2 c2Var, Throwable th) throws Throwable {
        e0 e0Var;
        m0(th);
        e0 e0Var2 = null;
        for (kotlinx.coroutines.o2.j jVarJ = (kotlinx.coroutines.o2.j) c2Var.i(); !h.x.d.k.a(jVarJ, c2Var); jVarJ = jVarJ.j()) {
            if (jVarJ instanceof u1) {
                y1 y1Var = (y1) jVarJ;
                try {
                    y1Var.q(th);
                } catch (Throwable th2) {
                    if (e0Var2 == null) {
                        e0Var = null;
                    } else {
                        h.b.a(e0Var2, th2);
                        e0Var = e0Var2;
                    }
                    if (e0Var == null) {
                        e0Var2 = new e0("Exception in completion handler " + y1Var + " for " + this, th2);
                    }
                }
            }
        }
        if (e0Var2 != null) {
            Z(e0Var2);
        }
        J(th);
    }

    private final void l0(c2 c2Var, Throwable th) throws Throwable {
        e0 e0Var;
        e0 e0Var2 = null;
        for (kotlinx.coroutines.o2.j jVarJ = (kotlinx.coroutines.o2.j) c2Var.i(); !h.x.d.k.a(jVarJ, c2Var); jVarJ = jVarJ.j()) {
            if (jVarJ instanceof y1) {
                y1 y1Var = (y1) jVarJ;
                try {
                    y1Var.q(th);
                } catch (Throwable th2) {
                    if (e0Var2 == null) {
                        e0Var = null;
                    } else {
                        h.b.a(e0Var2, th2);
                        e0Var = e0Var2;
                    }
                    if (e0Var == null) {
                        e0Var2 = new e0("Exception in completion handler " + y1Var + " for " + this, th2);
                    }
                }
            }
        }
        if (e0Var2 == null) {
            return;
        }
        Z(e0Var2);
    }

    private final boolean p(Object obj, c2 c2Var, y1 y1Var) {
        int iP;
        d dVar = new d(y1Var, this, obj);
        do {
            iP = c2Var.k().p(y1Var, c2Var, dVar);
            if (iP == 1) {
                return true;
            }
        } while (iP != 2);
        return false;
    }

    private final void p0(e1 e1Var) {
        c2 c2Var = new c2();
        Object m1Var = c2Var;
        if (!e1Var.isActive()) {
            m1Var = new m1(c2Var);
        }
        b.compareAndSet(this, e1Var, m1Var);
    }

    private final void q0(y1 y1Var) {
        y1Var.e(new c2());
        b.compareAndSet(this, y1Var, y1Var.j());
    }

    private final int t0(Object obj) {
        if (obj instanceof e1) {
            if (((e1) obj).isActive()) {
                return 0;
            }
            if (!b.compareAndSet(this, obj, a2.f1072g)) {
                return -1;
            }
            o0();
            return 1;
        }
        if (!(obj instanceof m1)) {
            return 0;
        }
        if (!b.compareAndSet(this, obj, ((m1) obj).b())) {
            return -1;
        }
        o0();
        return 1;
    }

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

    private final void v(Throwable th, List<? extends Throwable> list) {
        if (list.size() <= 1) {
            return;
        }
        Set setNewSetFromMap = Collections.newSetFromMap(new IdentityHashMap(list.size()));
        Throwable thL = !r0.d() ? th : kotlinx.coroutines.o2.p.l(th);
        for (Throwable thL2 : list) {
            if (r0.d()) {
                thL2 = kotlinx.coroutines.o2.p.l(thL2);
            }
            if (thL2 != th && thL2 != thL && !(thL2 instanceof CancellationException) && setNewSetFromMap.add(thL2)) {
                h.b.a(th, thL2);
            }
        }
    }

    public static /* synthetic */ CancellationException w0(z1 z1Var, 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 z1Var.v0(th, str);
    }

    private final boolean y0(n1 n1Var, Object obj) throws Throwable {
        if (r0.a()) {
            if (!((n1Var instanceof e1) || (n1Var instanceof y1))) {
                throw new AssertionError();
            }
        }
        if (r0.a() && !(!(obj instanceof b0))) {
            throw new AssertionError();
        }
        if (!b.compareAndSet(this, n1Var, a2.g(obj))) {
            return false;
        }
        m0(null);
        n0(obj);
        M(n1Var, obj);
        return true;
    }

    private final boolean z0(n1 n1Var, Throwable th) throws Throwable {
        if (r0.a() && !(!(n1Var instanceof c))) {
            throw new AssertionError();
        }
        if (r0.a() && !n1Var.isActive()) {
            throw new AssertionError();
        }
        c2 c2VarV = V(n1Var);
        if (c2VarV == null) {
            return false;
        }
        if (!b.compareAndSet(this, n1Var, new c(c2VarV, false, th))) {
            return false;
        }
        k0(c2VarV, th);
        return true;
    }

    @Override // h.u.g
    public h.u.g A(g.c<?> cVar) {
        return s1.a.e(this, cVar);
    }

    public final Object B(h.u.d<Object> dVar) throws Throwable {
        Object objX;
        do {
            objX = X();
            if (!(objX instanceof n1)) {
                if (!(objX instanceof b0)) {
                    return a2.h(objX);
                }
                Throwable th = ((b0) objX).a;
                if (r0.d() && (dVar instanceof h.u.j.a.e)) {
                    throw kotlinx.coroutines.o2.p.j(th, (h.u.j.a.e) dVar);
                }
                throw th;
            }
        } while (t0(objX) < 0);
        return C(dVar);
    }

    public final boolean E(Throwable th) {
        return G(th);
    }

    @Override // kotlinx.coroutines.s1
    public final s F(u uVar) {
        return (s) s1.a.d(this, true, false, new t(uVar), 2, null);
    }

    public final boolean G(Object obj) throws Throwable {
        Object objE0 = a2.a;
        if (U() && (objE0 = I(obj)) == a2.b) {
            return true;
        }
        if (objE0 == a2.a) {
            objE0 = e0(obj);
        }
        if (objE0 == a2.a || objE0 == a2.b) {
            return true;
        }
        if (objE0 == a2.f1069d) {
            return false;
        }
        w(objE0);
        return true;
    }

    public void H(Throwable th) throws Throwable {
        G(th);
    }

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

    public boolean L(Throwable th) {
        if (th instanceof CancellationException) {
            return true;
        }
        return G(th) && T();
    }

    public boolean T() {
        return true;
    }

    public boolean U() {
        return false;
    }

    public final s W() {
        return (s) this._parentHandle;
    }

    public final Object X() {
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof kotlinx.coroutines.o2.m)) {
                return obj;
            }
            ((kotlinx.coroutines.o2.m) obj).c(this);
        }
    }

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

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

    @Override // kotlinx.coroutines.s1
    public final boolean a() {
        int iT0;
        do {
            iT0 = t0(X());
            if (iT0 == 0) {
                return false;
            }
        } while (iT0 != 1);
        return true;
    }

    protected final void a0(s1 s1Var) {
        if (r0.a()) {
            if (!(W() == null)) {
                throw new AssertionError();
            }
        }
        if (s1Var == null) {
            s0(d2.b);
            return;
        }
        s1Var.a();
        s sVarF = s1Var.F(this);
        s0(sVarF);
        if (c0()) {
            sVarF.dispose();
            s0(d2.b);
        }
    }

    @Override // h.u.g.b, h.u.g
    public <E extends g.b> E b(g.c<E> cVar) {
        return (E) s1.a.c(this, cVar);
    }

    public final c1 b0(h.x.c.l<? super Throwable, h.r> lVar) {
        return r(false, true, lVar);
    }

    public final boolean c0() {
        return !(X() instanceof n1);
    }

    protected boolean d0() {
        return false;
    }

    public final boolean f0(Object obj) {
        Object objA0;
        do {
            objA0 = A0(X(), obj);
            if (objA0 == a2.a) {
                return false;
            }
            if (objA0 == a2.b) {
                return true;
            }
        } while (objA0 == a2.f1068c);
        w(objA0);
        return true;
    }

    public final Object g0(Object obj) {
        Object objA0;
        do {
            objA0 = A0(X(), obj);
            if (objA0 == a2.a) {
                throw new IllegalStateException("Job " + this + " is already complete or completing, but is being completed with " + obj, R(obj));
            }
        } while (objA0 == a2.f1068c);
        return objA0;
    }

    @Override // h.u.g.b
    public final g.c<?> getKey() {
        return s1.f1166d;
    }

    public String i0() {
        return s0.a(this);
    }

    @Override // kotlinx.coroutines.s1
    public boolean isActive() {
        Object objX = X();
        return (objX instanceof n1) && ((n1) objX).isActive();
    }

    @Override // h.u.g
    public h.u.g j(h.u.g gVar) {
        return s1.a.f(this, gVar);
    }

    protected void m0(Throwable th) {
    }

    protected void n0(Object obj) {
    }

    protected void o0() {
    }

    @Override // kotlinx.coroutines.f2
    public CancellationException q() {
        Throwable thE;
        Object objX = X();
        if (objX instanceof c) {
            thE = ((c) objX).e();
        } else if (objX instanceof b0) {
            thE = ((b0) objX).a;
        } else {
            if (objX instanceof n1) {
                throw new IllegalStateException(h.x.d.k.i("Cannot be cancelling child in this state: ", objX).toString());
            }
            thE = null;
        }
        CancellationException cancellationException = thE instanceof CancellationException ? (CancellationException) thE : null;
        return cancellationException == null ? new t1(h.x.d.k.i("Parent job is ", u0(objX)), thE, this) : cancellationException;
    }

    @Override // kotlinx.coroutines.s1
    public final c1 r(boolean z, boolean z2, h.x.c.l<? super Throwable, h.r> lVar) {
        y1 y1VarH0 = h0(lVar, z);
        while (true) {
            Object objX = X();
            if (objX instanceof e1) {
                e1 e1Var = (e1) objX;
                if (!e1Var.isActive()) {
                    p0(e1Var);
                } else if (b.compareAndSet(this, objX, y1VarH0)) {
                    return y1VarH0;
                }
            } else {
                if (!(objX instanceof n1)) {
                    if (z2) {
                        b0 b0Var = objX instanceof b0 ? (b0) objX : null;
                        lVar.invoke(b0Var != null ? b0Var.a : null);
                    }
                    return d2.b;
                }
                c2 c2VarB = ((n1) objX).b();
                if (c2VarB != null) {
                    c1 c1Var = d2.b;
                    if (z && (objX instanceof c)) {
                        synchronized (objX) {
                            thE = ((c) objX).e();
                            if (thE == null || ((lVar instanceof t) && !((c) objX).g())) {
                                if (p(objX, c2VarB, y1VarH0)) {
                                    if (thE == null) {
                                        return y1VarH0;
                                    }
                                    c1Var = y1VarH0;
                                }
                            }
                            h.r rVar = h.r.a;
                        }
                    }
                    if (thE != null) {
                        if (z2) {
                            lVar.invoke(thE);
                        }
                        return c1Var;
                    }
                    if (p(objX, c2VarB, y1VarH0)) {
                        return y1VarH0;
                    }
                } else {
                    if (objX == null) {
                        throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    }
                    q0((y1) objX);
                }
            }
        }
    }

    public final void r0(y1 y1Var) {
        Object objX;
        do {
            objX = X();
            if (!(objX instanceof y1)) {
                if (!(objX instanceof n1) || ((n1) objX).b() == null) {
                    return;
                }
                y1Var.m();
                return;
            }
            if (objX != y1Var) {
                return;
            }
        } while (!b.compareAndSet(this, objX, a2.f1072g));
    }

    @Override // kotlinx.coroutines.s1
    public final CancellationException s() {
        Object objX = X();
        if (!(objX instanceof c)) {
            if (objX instanceof n1) {
                throw new IllegalStateException(h.x.d.k.i("Job is still new or active: ", this).toString());
            }
            return objX instanceof b0 ? w0(this, ((b0) objX).a, null, 1, null) : new t1(h.x.d.k.i(s0.a(this), " has completed normally"), null, this);
        }
        Throwable thE = ((c) objX).e();
        if (thE != null) {
            return v0(thE, h.x.d.k.i(s0.a(this), " is cancelling"));
        }
        throw new IllegalStateException(h.x.d.k.i("Job is still new or active: ", this).toString());
    }

    public final void s0(s sVar) {
        this._parentHandle = sVar;
    }

    @Override // h.u.g
    public <R> R t(R r, h.x.c.p<? super R, ? super g.b, ? extends R> pVar) {
        return (R) s1.a.b(this, r, pVar);
    }

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

    @Override // kotlinx.coroutines.s1
    public void u(CancellationException cancellationException) throws Throwable {
        if (cancellationException == null) {
            cancellationException = new t1(K(), null, this);
        }
        H(cancellationException);
    }

    protected final CancellationException v0(Throwable th, String str) {
        CancellationException t1Var = th instanceof CancellationException ? (CancellationException) th : null;
        if (t1Var == null) {
            if (str == null) {
                str = K();
            }
            t1Var = new t1(str, th, this);
        }
        return t1Var;
    }

    protected void w(Object obj) {
    }

    public final String x0() {
        return i0() + '{' + u0(X()) + '}';
    }

    @Override // kotlinx.coroutines.u
    public final void y(f2 f2Var) throws Throwable {
        G(f2Var);
    }
}
