package kotlinx.coroutines;

import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Ref$ObjectRef;

/* JADX INFO: loaded from: classes.dex */
public class g1 implements y0, m, m1 {

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

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

    public g1(boolean z10) {
        this._state$volatile = z10 ? y.f29591r : y.f29590q;
    }

    public static l Q(kotlinx.coroutines.internal.i iVar) {
        while (iVar.i()) {
            kotlinx.coroutines.internal.i iVarE = iVar.e();
            if (iVarE == null) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = kotlinx.coroutines.internal.i.f29472b;
                Object obj = atomicReferenceFieldUpdater.get(iVar);
                while (true) {
                    iVar = (kotlinx.coroutines.internal.i) obj;
                    if (!iVar.i()) {
                        break;
                    }
                    obj = atomicReferenceFieldUpdater.get(iVar);
                }
            } else {
                iVar = iVarE;
            }
        }
        while (true) {
            iVar = iVar.h();
            if (!iVar.i()) {
                if (iVar instanceof l) {
                    return (l) iVar;
                }
                if (iVar instanceof j1) {
                    return null;
                }
            }
        }
    }

    public static String W(Object obj) {
        if (!(obj instanceof e1)) {
            return obj instanceof v0 ? ((v0) obj).isActive() ? "Active" : "New" : obj instanceof q ? "Cancelled" : "Completed";
        }
        e1 e1Var = (e1) obj;
        return e1Var.c() ? "Cancelling" : e1Var.e() ? "Completing" : "Active";
    }

    public final Object A() throws Throwable {
        Object objG = G();
        if (!(!(objG instanceof v0))) {
            throw new IllegalStateException("This job has not completed yet".toString());
        }
        if (objG instanceof q) {
            throw ((q) objG).f29523a;
        }
        return y.g0(objG);
    }

    public final Throwable B(e1 e1Var, ArrayList arrayList) {
        Object next;
        Object obj = null;
        if (arrayList.isEmpty()) {
            if (e1Var.c()) {
                return new JobCancellationException(t(), null, this);
            }
            return null;
        }
        Iterator it = arrayList.iterator();
        while (true) {
            if (!it.hasNext()) {
                next = null;
                break;
            }
            next = it.next();
            if (!(((Throwable) next) instanceof CancellationException)) {
                break;
            }
        }
        Throwable th = (Throwable) next;
        if (th != null) {
            return th;
        }
        Throwable th2 = (Throwable) arrayList.get(0);
        if (th2 instanceof TimeoutCancellationException) {
            Iterator it2 = arrayList.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break;
                }
                Object next2 = it2.next();
                Throwable th3 = (Throwable) next2;
                if (th3 != th2 && (th3 instanceof TimeoutCancellationException)) {
                    obj = next2;
                    break;
                }
            }
            Throwable th4 = (Throwable) obj;
            if (th4 != null) {
                return th4;
            }
        }
        return th2;
    }

    public boolean C() {
        return true;
    }

    public boolean D() {
        return this instanceof o;
    }

    @Override // kotlin.coroutines.h
    public final Object E(Object obj, Function2 function2) {
        nb.g.i(function2, "operation");
        return function2.invoke(obj, this);
    }

    public final j1 F(v0 v0Var) {
        j1 j1VarD = v0Var.d();
        if (j1VarD != null) {
            return j1VarD;
        }
        if (v0Var instanceof l0) {
            return new j1();
        }
        if (v0Var instanceof b1) {
            U((b1) v0Var);
            return null;
        }
        throw new IllegalStateException(("State should have list: " + v0Var).toString());
    }

    public final Object G() {
        while (true) {
            Object obj = f29433a.get(this);
            if (!(obj instanceof kotlinx.coroutines.internal.o)) {
                return obj;
            }
            ((kotlinx.coroutines.internal.o) obj).a(this);
        }
    }

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

    public final void K(y0 y0Var) {
        int iV;
        k1 k1Var = k1.f29507a;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29434b;
        if (y0Var == null) {
            atomicReferenceFieldUpdater.set(this, k1Var);
            return;
        }
        g1 g1Var = (g1) y0Var;
        do {
            iV = g1Var.V(g1Var.G());
            if (iV == 0) {
                break;
            }
        } while (iV != 1);
        k kVar = (k) nb.g.G(g1Var, true, new l(this), 2);
        atomicReferenceFieldUpdater.set(this, kVar);
        if (!(G() instanceof v0)) {
            kVar.dispose();
            atomicReferenceFieldUpdater.set(this, k1Var);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:75:0x00bc  */
    /* JADX WARN: Removed duplicated region for block: B:97:0x00b6 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final j0 L(boolean z10, boolean z11, sd.l lVar) {
        b1 k0Var;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        Throwable thB;
        if (z10) {
            k0Var = lVar instanceof z0 ? (z0) lVar : null;
            if (k0Var == null) {
                k0Var = new x0(lVar);
            }
        } else {
            k0Var = lVar instanceof b1 ? (b1) lVar : null;
            if (k0Var == null) {
                k0Var = new k0(lVar, 1);
            }
        }
        k0Var.f29293d = this;
        while (true) {
            Object objG = G();
            if (objG instanceof l0) {
                l0 l0Var = (l0) objG;
                if (l0Var.f29509a) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = f29433a;
                    while (!atomicReferenceFieldUpdater2.compareAndSet(this, objG, k0Var)) {
                        if (atomicReferenceFieldUpdater2.get(this) != objG) {
                            break;
                        }
                    }
                    return k0Var;
                }
                j1 j1Var = new j1();
                v0 u0Var = l0Var.f29509a ? j1Var : new u0(j1Var);
                do {
                    atomicReferenceFieldUpdater = f29433a;
                    if (atomicReferenceFieldUpdater.compareAndSet(this, l0Var, u0Var)) {
                        break;
                    }
                } while (atomicReferenceFieldUpdater.get(this) == l0Var);
            } else {
                if (!(objG instanceof v0)) {
                    if (z11) {
                        q qVar = objG instanceof q ? (q) objG : null;
                        lVar.invoke(qVar != null ? qVar.f29523a : null);
                    }
                    return k1.f29507a;
                }
                j1 j1VarD = ((v0) objG).d();
                if (j1VarD == null) {
                    nb.g.g(objG, "null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    U((b1) objG);
                } else {
                    j0 j0Var = k1.f29507a;
                    if (z10 && (objG instanceof e1)) {
                        synchronized (objG) {
                            try {
                                thB = ((e1) objG).b();
                                if (thB == null || ((lVar instanceof l) && !((e1) objG).e())) {
                                    if (l(objG, j1VarD, k0Var)) {
                                        if (thB == null) {
                                            return k0Var;
                                        }
                                        j0Var = k0Var;
                                    }
                                }
                            } catch (Throwable th) {
                                throw th;
                            }
                        }
                        if (thB == null) {
                        }
                    } else {
                        thB = null;
                        if (thB == null) {
                            if (z11) {
                                lVar.invoke(thB);
                            }
                            return j0Var;
                        }
                        if (l(objG, j1VarD, k0Var)) {
                            return k0Var;
                        }
                    }
                }
            }
        }
    }

    public boolean M() {
        return this instanceof c;
    }

    public final boolean N(Object obj) {
        Object objX;
        do {
            objX = X(G(), obj);
            if (objX == y.f29585l) {
                return false;
            }
            if (objX == y.f29586m) {
                return true;
            }
        } while (objX == y.f29587n);
        n(objX);
        return true;
    }

    public final Object O(Object obj) {
        Object objX;
        do {
            objX = X(G(), obj);
            if (objX == y.f29585l) {
                String str = "Job " + this + " is already complete or completing, but is being completed with " + obj;
                q qVar = obj instanceof q ? (q) obj : null;
                throw new IllegalStateException(str, qVar != null ? qVar.f29523a : null);
            }
        } while (objX == y.f29587n);
        return objX;
    }

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

    public final void R(j1 j1Var, Throwable th) {
        Object objG = j1Var.g();
        nb.g.g(objG, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode{ kotlinx.coroutines.internal.LockFreeLinkedListKt.Node }");
        CompletionHandlerException completionHandlerException = null;
        for (kotlinx.coroutines.internal.i iVarH = (kotlinx.coroutines.internal.i) objG; !nb.g.c(iVarH, j1Var); iVarH = iVarH.h()) {
            if (iVarH instanceof z0) {
                b1 b1Var = (b1) iVarH;
                try {
                    b1Var.k(th);
                } catch (Throwable th2) {
                    if (completionHandlerException != null) {
                        kotlin.a.a(completionHandlerException, th2);
                    } else {
                        completionHandlerException = new CompletionHandlerException("Exception in completion handler " + b1Var + " for " + this, th2);
                    }
                }
            }
        }
        if (completionHandlerException != null) {
            J(completionHandlerException);
        }
        s(th);
    }

    public void S(Object obj) {
    }

    public void T() {
    }

    public final void U(b1 b1Var) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        j1 j1Var = new j1();
        b1Var.getClass();
        kotlinx.coroutines.internal.i.f29472b.set(j1Var, b1Var);
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = kotlinx.coroutines.internal.i.f29471a;
        atomicReferenceFieldUpdater2.set(j1Var, b1Var);
        loop0: while (true) {
            if (b1Var.g() == b1Var) {
                while (!atomicReferenceFieldUpdater2.compareAndSet(b1Var, b1Var, j1Var)) {
                    if (atomicReferenceFieldUpdater2.get(b1Var) != b1Var) {
                        break;
                    }
                }
                j1Var.f(b1Var);
                break loop0;
            }
            break;
        }
        kotlinx.coroutines.internal.i iVarH = b1Var.h();
        do {
            atomicReferenceFieldUpdater = f29433a;
            if (atomicReferenceFieldUpdater.compareAndSet(this, b1Var, iVarH)) {
                return;
            }
        } while (atomicReferenceFieldUpdater.get(this) == b1Var);
    }

    public final int V(Object obj) {
        boolean z10 = obj instanceof l0;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29433a;
        if (z10) {
            if (((l0) obj).f29509a) {
                return 0;
            }
            l0 l0Var = y.f29591r;
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, l0Var)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    return -1;
                }
            }
            T();
            return 1;
        }
        if (!(obj instanceof u0)) {
            return 0;
        }
        j1 j1Var = ((u0) obj).f29566a;
        while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, j1Var)) {
            if (atomicReferenceFieldUpdater.get(this) != obj) {
                return -1;
            }
        }
        T();
        return 1;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r6v11 */
    /* JADX WARN: Type inference failed for: r6v4 */
    /* JADX WARN: Type inference failed for: r6v5, types: [T, java.lang.Throwable] */
    public final Object X(Object obj, Object obj2) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        if (!(obj instanceof v0)) {
            return y.f29585l;
        }
        if (((obj instanceof l0) || (obj instanceof b1)) && !(obj instanceof l) && !(obj2 instanceof q)) {
            v0 v0Var = (v0) obj;
            Object w0Var = obj2 instanceof v0 ? new w0((v0) obj2) : obj2;
            do {
                atomicReferenceFieldUpdater = f29433a;
                if (atomicReferenceFieldUpdater.compareAndSet(this, v0Var, w0Var)) {
                    S(obj2);
                    v(v0Var, obj2);
                    return obj2;
                }
            } while (atomicReferenceFieldUpdater.get(this) == v0Var);
            return y.f29587n;
        }
        v0 v0Var2 = (v0) obj;
        j1 j1VarF = F(v0Var2);
        if (j1VarF == null) {
            return y.f29587n;
        }
        l lVarQ = null;
        e1 e1Var = v0Var2 instanceof e1 ? (e1) v0Var2 : null;
        if (e1Var == null) {
            e1Var = new e1(j1VarF, null);
        }
        Ref$ObjectRef ref$ObjectRef = new Ref$ObjectRef();
        synchronized (e1Var) {
            try {
                if (e1Var.e()) {
                    return y.f29585l;
                }
                e1.f29352b.set(e1Var, 1);
                if (e1Var != v0Var2) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = f29433a;
                    while (!atomicReferenceFieldUpdater2.compareAndSet(this, v0Var2, e1Var)) {
                        if (atomicReferenceFieldUpdater2.get(this) != v0Var2) {
                            return y.f29587n;
                        }
                    }
                }
                boolean zC = e1Var.c();
                q qVar = obj2 instanceof q ? (q) obj2 : null;
                if (qVar != null) {
                    e1Var.a(qVar.f29523a);
                }
                ?? B = Boolean.valueOf(zC ^ true).booleanValue() ? e1Var.b() : 0;
                ref$ObjectRef.element = B;
                if (B != 0) {
                    R(j1VarF, B);
                }
                l lVar = v0Var2 instanceof l ? (l) v0Var2 : null;
                if (lVar == null) {
                    j1 j1VarD = v0Var2.d();
                    if (j1VarD != null) {
                        lVarQ = Q(j1VarD);
                    }
                } else {
                    lVarQ = lVar;
                }
                if (lVarQ != null) {
                    while (nb.g.G(lVarQ.f29508e, false, new d1(this, e1Var, lVarQ, obj2), 1) == k1.f29507a) {
                        lVarQ = Q(lVarQ);
                        if (lVarQ == null) {
                        }
                    }
                    return y.f29586m;
                }
                return y(e1Var, obj2);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // kotlinx.coroutines.y0, kotlinx.coroutines.channels.m
    public void e(CancellationException cancellationException) {
        if (cancellationException == null) {
            cancellationException = new JobCancellationException(t(), null, this);
        }
        r(cancellationException);
    }

    public Object g() {
        return A();
    }

    @Override // kotlin.coroutines.f
    public final kotlin.coroutines.g getKey() {
        return u.f29565b;
    }

    @Override // kotlinx.coroutines.y0
    public boolean isActive() {
        Object objG = G();
        return (objG instanceof v0) && ((v0) objG).isActive();
    }

    public final boolean l(Object obj, j1 j1Var, b1 b1Var) {
        char c6;
        f1 f1Var = new f1(b1Var, this, obj);
        do {
            kotlinx.coroutines.internal.i iVarE = j1Var.e();
            if (iVarE == null) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = kotlinx.coroutines.internal.i.f29472b;
                Object obj2 = atomicReferenceFieldUpdater.get(j1Var);
                while (true) {
                    iVarE = (kotlinx.coroutines.internal.i) obj2;
                    if (!iVarE.i()) {
                        break;
                    }
                    obj2 = atomicReferenceFieldUpdater.get(iVarE);
                }
            }
            kotlinx.coroutines.internal.i.f29472b.set(b1Var, iVarE);
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = kotlinx.coroutines.internal.i.f29471a;
            atomicReferenceFieldUpdater2.set(b1Var, j1Var);
            f1Var.f29358c = j1Var;
            while (true) {
                if (atomicReferenceFieldUpdater2.compareAndSet(iVarE, j1Var, f1Var)) {
                    c6 = f1Var.a(iVarE) == null ? (char) 1 : (char) 2;
                } else if (atomicReferenceFieldUpdater2.get(iVarE) != j1Var) {
                    c6 = 0;
                    break;
                }
            }
            if (c6 == 1) {
                return true;
            }
        } while (c6 != 2);
        return false;
    }

    @Override // kotlin.coroutines.h
    public final kotlin.coroutines.f m(kotlin.coroutines.g gVar) {
        return ua.b.w(this, gVar);
    }

    public void n(Object obj) {
    }

    public void o(Object obj) {
        n(obj);
    }

    @Override // kotlin.coroutines.h
    public final kotlin.coroutines.h p(kotlin.coroutines.h hVar) {
        nb.g.i(hVar, "context");
        return kotlin.coroutines.e.a(this, hVar);
    }

    /* JADX WARN: Code restructure failed: missing block: B:28:0x0058, code lost:
    
        r0 = r10;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean q(Object obj) {
        la.e eVar;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        Object objX = y.f29585l;
        if (D()) {
            do {
                Object objG = G();
                if (!(objG instanceof v0) || ((objG instanceof e1) && ((e1) objG).e())) {
                    objX = y.f29585l;
                    break;
                }
                objX = X(objG, new q(w(obj), false));
            } while (objX == y.f29587n);
            if (objX == y.f29586m) {
                return true;
            }
        }
        if (objX == y.f29585l) {
            Throwable thW = null;
            loop1: while (true) {
                Object objG2 = G();
                if (!(objG2 instanceof e1)) {
                    if (!(objG2 instanceof v0)) {
                        eVar = y.f29588o;
                        break;
                    }
                    if (thW == null) {
                        thW = w(obj);
                    }
                    v0 v0Var = (v0) objG2;
                    if (v0Var.isActive()) {
                        j1 j1VarF = F(v0Var);
                        if (j1VarF == null) {
                            continue;
                        } else {
                            e1 e1Var = new e1(j1VarF, thW);
                            do {
                                atomicReferenceFieldUpdater = f29433a;
                                if (atomicReferenceFieldUpdater.compareAndSet(this, v0Var, e1Var)) {
                                    R(j1VarF, thW);
                                    eVar = y.f29585l;
                                    break loop1;
                                }
                            } while (atomicReferenceFieldUpdater.get(this) == v0Var);
                        }
                    } else {
                        Object objX2 = X(objG2, new q(thW, false));
                        if (objX2 == y.f29585l) {
                            throw new IllegalStateException(("Cannot happen in " + objG2).toString());
                        }
                        if (objX2 != y.f29587n) {
                            objX = objX2;
                            break;
                        }
                    }
                } else {
                    synchronized (objG2) {
                        try {
                            if (e1.f29354d.get((e1) objG2) == y.f29589p) {
                                eVar = y.f29588o;
                            } else {
                                boolean zC = ((e1) objG2).c();
                                if (thW == null) {
                                    thW = w(obj);
                                }
                                ((e1) objG2).a(thW);
                                Throwable thB = zC ^ true ? ((e1) objG2).b() : null;
                                if (thB != null) {
                                    R(((e1) objG2).f29355a, thB);
                                }
                                eVar = y.f29585l;
                            }
                        } catch (Throwable th) {
                            throw th;
                        }
                    }
                }
            }
        }
        if (objX != y.f29585l && objX != y.f29586m) {
            if (objX == y.f29588o) {
                return false;
            }
            n(objX);
        }
        return true;
    }

    public void r(CancellationException cancellationException) {
        q(cancellationException);
    }

    public final boolean s(Throwable th) {
        if (M()) {
            return true;
        }
        boolean z10 = th instanceof CancellationException;
        k kVar = (k) f29434b.get(this);
        return (kVar == null || kVar == k1.f29507a) ? z10 : kVar.c(th) || z10;
    }

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

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(P() + '{' + W(G()) + '}');
        sb.append('@');
        sb.append(y.z(this));
        return sb.toString();
    }

    public boolean u(Throwable th) {
        if (th instanceof CancellationException) {
            return true;
        }
        return q(th) && C();
    }

    public final void v(v0 v0Var, Object obj) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29434b;
        k kVar = (k) atomicReferenceFieldUpdater.get(this);
        if (kVar != null) {
            kVar.dispose();
            atomicReferenceFieldUpdater.set(this, k1.f29507a);
        }
        CompletionHandlerException completionHandlerException = null;
        q qVar = obj instanceof q ? (q) obj : null;
        Throwable th = qVar != null ? qVar.f29523a : null;
        if (v0Var instanceof b1) {
            try {
                ((b1) v0Var).k(th);
                return;
            } catch (Throwable th2) {
                J(new CompletionHandlerException("Exception in completion handler " + v0Var + " for " + this, th2));
                return;
            }
        }
        j1 j1VarD = v0Var.d();
        if (j1VarD != null) {
            Object objG = j1VarD.g();
            nb.g.g(objG, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode{ kotlinx.coroutines.internal.LockFreeLinkedListKt.Node }");
            for (kotlinx.coroutines.internal.i iVarH = (kotlinx.coroutines.internal.i) objG; !nb.g.c(iVarH, j1VarD); iVarH = iVarH.h()) {
                if (iVarH instanceof b1) {
                    b1 b1Var = (b1) iVarH;
                    try {
                        b1Var.k(th);
                    } catch (Throwable th3) {
                        if (completionHandlerException != null) {
                            kotlin.a.a(completionHandlerException, th3);
                        } else {
                            completionHandlerException = new CompletionHandlerException("Exception in completion handler " + b1Var + " for " + this, th3);
                        }
                    }
                }
            }
            if (completionHandlerException != null) {
                J(completionHandlerException);
            }
        }
    }

    public final Throwable w(Object obj) {
        Throwable thB;
        if (obj instanceof Throwable) {
            return (Throwable) obj;
        }
        g1 g1Var = (g1) ((m1) obj);
        Object objG = g1Var.G();
        if (objG instanceof e1) {
            thB = ((e1) objG).b();
        } else if (objG instanceof q) {
            thB = ((q) objG).f29523a;
        } else {
            if (objG instanceof v0) {
                throw new IllegalStateException(("Cannot be cancelling child in this state: " + objG).toString());
            }
            thB = null;
        }
        CancellationException jobCancellationException = thB instanceof CancellationException ? (CancellationException) thB : null;
        if (jobCancellationException == null) {
            jobCancellationException = new JobCancellationException("Parent job is ".concat(W(objG)), thB, g1Var);
        }
        return jobCancellationException;
    }

    @Override // kotlin.coroutines.h
    public final kotlin.coroutines.h x(kotlin.coroutines.g gVar) {
        return ua.b.E(this, gVar);
    }

    public final Object y(e1 e1Var, Object obj) {
        Throwable thB;
        q qVar = obj instanceof q ? (q) obj : null;
        Throwable th = qVar != null ? qVar.f29523a : null;
        synchronized (e1Var) {
            e1Var.c();
            ArrayList<Throwable> arrayListF = e1Var.f(th);
            thB = B(e1Var, arrayListF);
            if (thB != null && arrayListF.size() > 1) {
                Set setNewSetFromMap = Collections.newSetFromMap(new IdentityHashMap(arrayListF.size()));
                for (Throwable th2 : arrayListF) {
                    if (th2 != thB && th2 != thB && !(th2 instanceof CancellationException) && setNewSetFromMap.add(th2)) {
                        kotlin.a.a(thB, th2);
                    }
                }
            }
        }
        if (thB != null && thB != th) {
            obj = new q(thB, false);
        }
        if (thB != null && (s(thB) || I(thB))) {
            nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
            q.f29522b.compareAndSet((q) obj, 0, 1);
        }
        S(obj);
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29433a;
        Object w0Var = obj instanceof v0 ? new w0((v0) obj) : obj;
        while (!atomicReferenceFieldUpdater.compareAndSet(this, e1Var, w0Var) && atomicReferenceFieldUpdater.get(this) == e1Var) {
        }
        v(e1Var, obj);
        return obj;
    }

    public final CancellationException z() {
        CancellationException cancellationException;
        Object objG = G();
        if (!(objG instanceof e1)) {
            if (objG instanceof v0) {
                throw new IllegalStateException(("Job is still new or active: " + this).toString());
            }
            if (!(objG instanceof q)) {
                return new JobCancellationException(getClass().getSimpleName().concat(" has completed normally"), null, this);
            }
            Throwable th = ((q) objG).f29523a;
            cancellationException = th instanceof CancellationException ? (CancellationException) th : null;
            return cancellationException == null ? new JobCancellationException(t(), th, this) : cancellationException;
        }
        Throwable thB = ((e1) objG).b();
        if (thB == null) {
            throw new IllegalStateException(("Job is still new or active: " + this).toString());
        }
        String strConcat = getClass().getSimpleName().concat(" is cancelling");
        cancellationException = thB instanceof CancellationException ? (CancellationException) thB : null;
        if (cancellationException != null) {
            return cancellationException;
        }
        if (strConcat == null) {
            strConcat = t();
        }
        return new JobCancellationException(strConcat, thB, this);
    }

    public void J(CompletionHandlerException completionHandlerException) {
        throw completionHandlerException;
    }
}
