package kotlinx.coroutines;

import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlinx.coroutines.s1;

/* JADX INFO: compiled from: CancellableContinuationImpl.kt */
/* JADX INFO: loaded from: classes.dex */
public class o<T> extends z0<T> implements n<T>, h.u.j.a.e {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final /* synthetic */ AtomicIntegerFieldUpdater f1098j = AtomicIntegerFieldUpdater.newUpdater(o.class, "_decision");

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f1099k = AtomicReferenceFieldUpdater.newUpdater(o.class, Object.class, "_state");
    private volatile /* synthetic */ int _decision;
    private volatile /* synthetic */ Object _state;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final h.u.d<T> f1100g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final h.u.g f1101h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private c1 f1102i;

    /* JADX WARN: Multi-variable type inference failed */
    public o(h.u.d<? super T> dVar, int i2) {
        super(i2);
        this.f1100g = dVar;
        if (r0.a()) {
            if (!(i2 != -1)) {
                throw new AssertionError();
            }
        }
        this.f1101h = this.f1100g.getContext();
        this._decision = 0;
        this._state = f.b;
    }

    private final boolean B() {
        return a1.c(this.f1175f) && ((kotlinx.coroutines.o2.e) this.f1100g).n();
    }

    private final l C(h.x.c.l<? super Throwable, h.r> lVar) {
        return lVar instanceof l ? (l) lVar : new p1(lVar);
    }

    private final void D(h.x.c.l<? super Throwable, h.r> lVar, Object obj) {
        throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + lVar + ", already has " + obj).toString());
    }

    private final void G() {
        h.u.d<T> dVar = this.f1100g;
        kotlinx.coroutines.o2.e eVar = dVar instanceof kotlinx.coroutines.o2.e ? (kotlinx.coroutines.o2.e) dVar : null;
        Throwable thQ = eVar != null ? eVar.q(this) : null;
        if (thQ == null) {
            return;
        }
        r();
        p(thQ);
    }

    private final void H(Object obj, int i2, h.x.c.l<? super Throwable, h.r> lVar) {
        Object obj2;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof e2)) {
                if (obj2 instanceof q) {
                    q qVar = (q) obj2;
                    if (qVar.c()) {
                        if (lVar == null) {
                            return;
                        }
                        o(lVar, qVar.a);
                        return;
                    }
                }
                l(obj);
                throw null;
            }
        } while (!f1099k.compareAndSet(this, obj2, J((e2) obj2, obj, i2, lVar, null)));
        s();
        t(i2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    static /* synthetic */ void I(o oVar, Object obj, int i2, h.x.c.l lVar, int i3, Object obj2) {
        if (obj2 != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: resumeImpl");
        }
        if ((i3 & 4) != 0) {
            lVar = null;
        }
        oVar.H(obj, i2, lVar);
    }

    private final Object J(e2 e2Var, Object obj, int i2, h.x.c.l<? super Throwable, h.r> lVar, Object obj2) {
        if (obj instanceof b0) {
            if (r0.a()) {
                if (!(obj2 == null)) {
                    throw new AssertionError();
                }
            }
            if (!r0.a()) {
                return obj;
            }
            if (lVar == null) {
                return obj;
            }
            throw new AssertionError();
        }
        if (!a1.b(i2) && obj2 == null) {
            return obj;
        }
        if (lVar == null && !(e2Var instanceof l) && obj2 == null) {
            return obj;
        }
        return new a0(obj, e2Var instanceof l ? (l) e2Var : null, lVar, obj2, null, 16, null);
    }

    private final boolean K() {
        do {
            int i2 = this._decision;
            if (i2 != 0) {
                if (i2 == 1) {
                    return false;
                }
                throw new IllegalStateException("Already resumed".toString());
            }
        } while (!f1098j.compareAndSet(this, 0, 2));
        return true;
    }

    private final boolean L() {
        do {
            int i2 = this._decision;
            if (i2 != 0) {
                if (i2 == 2) {
                    return false;
                }
                throw new IllegalStateException("Already suspended".toString());
            }
        } while (!f1098j.compareAndSet(this, 0, 1));
        return true;
    }

    private final Void l(Object obj) {
        throw new IllegalStateException(h.x.d.k.i("Already resumed, but proposed with update ", obj).toString());
    }

    private final void m(h.x.c.l<? super Throwable, h.r> lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            k0.a(getContext(), new e0(h.x.d.k.i("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    private final boolean q(Throwable th) {
        if (B()) {
            return ((kotlinx.coroutines.o2.e) this.f1100g).o(th);
        }
        return false;
    }

    private final void s() {
        if (B()) {
            return;
        }
        r();
    }

    private final void t(int i2) {
        if (K()) {
            return;
        }
        a1.a(this, i2);
    }

    private final String x() {
        Object objW = w();
        return objW instanceof e2 ? "Active" : objW instanceof q ? "Cancelled" : "Completed";
    }

    private final c1 z() {
        s1 s1Var = (s1) getContext().b(s1.f1166d);
        if (s1Var == null) {
            return null;
        }
        c1 c1VarD = s1.a.d(s1Var, true, false, new r(this), 2, null);
        this.f1102i = c1VarD;
        return c1VarD;
    }

    public boolean A() {
        return !(w() instanceof e2);
    }

    protected String E() {
        return "CancellableContinuation";
    }

    public final void F(Throwable th) {
        if (q(th)) {
            return;
        }
        p(th);
        s();
    }

    @Override // kotlinx.coroutines.z0
    public void a(Object obj, Throwable th) {
        while (true) {
            Object obj2 = this._state;
            if (obj2 instanceof e2) {
                throw new IllegalStateException("Not completed".toString());
            }
            if (obj2 instanceof b0) {
                return;
            }
            if (obj2 instanceof a0) {
                a0 a0Var = (a0) obj2;
                if (!(!a0Var.c())) {
                    throw new IllegalStateException("Must be called at most once".toString());
                }
                if (f1099k.compareAndSet(this, obj2, a0.b(a0Var, null, null, null, null, th, 15, null))) {
                    a0Var.d(this, th);
                    return;
                }
            } else if (f1099k.compareAndSet(this, obj2, new a0(obj2, null, null, null, th, 14, null))) {
                return;
            }
        }
    }

    @Override // kotlinx.coroutines.z0
    public final h.u.d<T> b() {
        return this.f1100g;
    }

    @Override // kotlinx.coroutines.n
    public void c(i0 i0Var, T t) {
        h.u.d<T> dVar = this.f1100g;
        kotlinx.coroutines.o2.e eVar = dVar instanceof kotlinx.coroutines.o2.e ? (kotlinx.coroutines.o2.e) dVar : null;
        I(this, t, (eVar != null ? eVar.f1105g : null) == i0Var ? 4 : this.f1175f, null, 4, null);
    }

    @Override // kotlinx.coroutines.z0
    public Throwable d(Object obj) {
        Throwable thD = super.d(obj);
        if (thD == null) {
            return null;
        }
        h.u.d<T> dVarB = b();
        return (r0.d() && (dVarB instanceof h.u.j.a.e)) ? kotlinx.coroutines.o2.p.j(thD, (h.u.j.a.e) dVarB) : thD;
    }

    @Override // h.u.j.a.e
    public h.u.j.a.e e() {
        h.u.d<T> dVar = this.f1100g;
        if (dVar instanceof h.u.j.a.e) {
            return (h.u.j.a.e) dVar;
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // kotlinx.coroutines.z0
    public <T> T f(Object obj) {
        return obj instanceof a0 ? (T) ((a0) obj).a : obj;
    }

    @Override // h.u.d
    public void g(Object obj) {
        I(this, f0.c(obj, this), this.f1175f, null, 4, null);
    }

    @Override // h.u.d
    public h.u.g getContext() {
        return this.f1101h;
    }

    @Override // kotlinx.coroutines.n
    public void h(h.x.c.l<? super Throwable, h.r> lVar) {
        l lVarC = C(lVar);
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof f)) {
                if (obj instanceof l) {
                    D(lVar, obj);
                    throw null;
                }
                boolean z = obj instanceof b0;
                if (z) {
                    b0 b0Var = (b0) obj;
                    if (!b0Var.b()) {
                        D(lVar, obj);
                        throw null;
                    }
                    if (obj instanceof q) {
                        if (!z) {
                            b0Var = null;
                        }
                        m(lVar, b0Var != null ? b0Var.a : null);
                        return;
                    }
                    return;
                }
                if (obj instanceof a0) {
                    a0 a0Var = (a0) obj;
                    if (a0Var.b != null) {
                        D(lVar, obj);
                        throw null;
                    }
                    if (a0Var.c()) {
                        m(lVar, a0Var.f1067e);
                        return;
                    } else {
                        if (f1099k.compareAndSet(this, obj, a0.b(a0Var, null, lVarC, null, null, null, 29, null))) {
                            return;
                        }
                    }
                } else {
                    if (f1099k.compareAndSet(this, obj, new a0(obj, lVarC, null, null, null, 28, null))) {
                        return;
                    }
                }
            } else if (f1099k.compareAndSet(this, obj, lVarC)) {
                return;
            }
        }
    }

    @Override // kotlinx.coroutines.z0
    public Object j() {
        return w();
    }

    @Override // h.u.j.a.e
    public StackTraceElement k() {
        return null;
    }

    public final void n(l lVar, Throwable th) {
        try {
            lVar.b(th);
        } catch (Throwable th2) {
            k0.a(getContext(), new e0(h.x.d.k.i("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    public final void o(h.x.c.l<? super Throwable, h.r> lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            k0.a(getContext(), new e0(h.x.d.k.i("Exception in resume onCancellation handler for ", this), th2));
        }
    }

    public boolean p(Throwable th) {
        Object obj;
        boolean z;
        do {
            obj = this._state;
            if (!(obj instanceof e2)) {
                return false;
            }
            z = obj instanceof l;
        } while (!f1099k.compareAndSet(this, obj, new q(this, th, z)));
        l lVar = z ? (l) obj : null;
        if (lVar != null) {
            n(lVar, th);
        }
        s();
        t(this.f1175f);
        return true;
    }

    public final void r() {
        c1 c1Var = this.f1102i;
        if (c1Var == null) {
            return;
        }
        c1Var.dispose();
        this.f1102i = d2.b;
    }

    public String toString() {
        return E() + '(' + s0.c(this.f1100g) + "){" + x() + "}@" + s0.b(this);
    }

    public Throwable u(s1 s1Var) {
        return s1Var.s();
    }

    public final Object v() {
        s1 s1Var;
        boolean zB = B();
        if (L()) {
            if (this.f1102i == null) {
                z();
            }
            if (zB) {
                G();
            }
            return h.u.i.d.d();
        }
        if (zB) {
            G();
        }
        Object objW = w();
        if (objW instanceof b0) {
            Throwable th = ((b0) objW).a;
            if (r0.d()) {
                throw kotlinx.coroutines.o2.p.j(th, this);
            }
            throw th;
        }
        if (!a1.b(this.f1175f) || (s1Var = (s1) getContext().b(s1.f1166d)) == null || s1Var.isActive()) {
            return f(objW);
        }
        CancellationException cancellationExceptionS = s1Var.s();
        a(objW, cancellationExceptionS);
        if (r0.d()) {
            throw kotlinx.coroutines.o2.p.j(cancellationExceptionS, this);
        }
        throw cancellationExceptionS;
    }

    public final Object w() {
        return this._state;
    }

    public void y() {
        c1 c1VarZ = z();
        if (c1VarZ != null && A()) {
            c1VarZ.dispose();
            this.f1102i = d2.b;
        }
    }
}
