package g.a;

import g.a.n1;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: CancellableContinuationImpl.kt */
/* JADX INFO: loaded from: classes.dex */
public class k<T> extends v0<T> implements j<T>, f.w.k.a.e {

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final f.w.d<T> f4037h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final f.w.g f4038i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private y0 f4039j;

    /* JADX WARN: Multi-variable type inference failed */
    public k(f.w.d<? super T> dVar, int i2) {
        super(i2);
        this.f4037h = dVar;
        if (l0.a()) {
            if (!(i2 != -1)) {
                throw new AssertionError();
            }
        }
        this.f4038i = this.f4037h.getContext();
        this._decision = 0;
        this._state = d.f4024b;
    }

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

    private final void D() {
        Throwable thN;
        f.w.d<T> dVar = this.f4037h;
        g.a.n2.g gVar = dVar instanceof g.a.n2.g ? (g.a.n2.g) dVar : null;
        if (gVar == null || (thN = gVar.n(this)) == null) {
            return;
        }
        o();
        m(thN);
    }

    private final void E(Object obj, int i2, f.z.c.l<? super Throwable, f.t> lVar) {
        Object obj2;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof b2)) {
                if (obj2 instanceof m) {
                    m mVar = (m) obj2;
                    if (mVar.c()) {
                        if (lVar != null) {
                            l(lVar, mVar.a);
                            return;
                        }
                        return;
                    }
                }
                i(obj);
                throw null;
            }
        } while (!l.compareAndSet(this, obj2, G((b2) obj2, obj, i2, lVar, null)));
        p();
        q(i2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    static /* synthetic */ void F(k kVar, Object obj, int i2, f.z.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;
        }
        kVar.E(obj, i2, lVar);
    }

    private final Object G(b2 b2Var, Object obj, int i2, f.z.c.l<? super Throwable, f.t> lVar, Object obj2) {
        if (obj instanceof t) {
            if (l0.a()) {
                if (!(obj2 == null)) {
                    throw new AssertionError();
                }
            }
            if (!l0.a()) {
                return obj;
            }
            if (lVar == null) {
                return obj;
            }
            throw new AssertionError();
        }
        if (!w0.b(i2) && obj2 == null) {
            return obj;
        }
        if (lVar == null && !(b2Var instanceof h) && obj2 == null) {
            return obj;
        }
        return new s(obj, b2Var instanceof h ? (h) b2Var : null, lVar, obj2, null, 16, null);
    }

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

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

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

    private final void j(f.z.c.l<? super Throwable, f.t> lVar, Throwable th) {
        try {
            lVar.c(th);
        } catch (Throwable th2) {
            e0.a(getContext(), new w("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    private final boolean n(Throwable th) {
        if (y()) {
            return ((g.a.n2.g) this.f4037h).l(th);
        }
        return false;
    }

    private final void p() {
        if (y()) {
            return;
        }
        o();
    }

    private final void q(int i2) {
        if (H()) {
            return;
        }
        w0.a(this, i2);
    }

    private final String u() {
        Object objT = t();
        return objT instanceof b2 ? "Active" : objT instanceof m ? "Cancelled" : "Completed";
    }

    private final y0 w() {
        n1 n1Var = (n1) getContext().get(n1.f4054e);
        if (n1Var == null) {
            return null;
        }
        y0 y0VarD = n1.a.d(n1Var, true, false, new n(this), 2, null);
        this.f4039j = y0VarD;
        return y0VarD;
    }

    private final boolean y() {
        return w0.c(this.f4160g) && ((g.a.n2.g) this.f4037h).k();
    }

    private final h z(f.z.c.l<? super Throwable, f.t> lVar) {
        return lVar instanceof h ? (h) lVar : new k1(lVar);
    }

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

    public final void C(Throwable th) {
        if (n(th)) {
            return;
        }
        m(th);
        p();
    }

    @Override // g.a.v0
    public void a(Object obj, Throwable th) {
        while (true) {
            Object obj2 = this._state;
            if (obj2 instanceof b2) {
                throw new IllegalStateException("Not completed".toString());
            }
            if (obj2 instanceof t) {
                return;
            }
            if (obj2 instanceof s) {
                s sVar = (s) obj2;
                if (!(!sVar.c())) {
                    throw new IllegalStateException("Must be called at most once".toString());
                }
                if (l.compareAndSet(this, obj2, s.b(sVar, null, null, null, null, th, 15, null))) {
                    sVar.d(this, th);
                    return;
                }
            } else if (l.compareAndSet(this, obj2, new s(obj2, null, null, null, th, 14, null))) {
                return;
            }
        }
    }

    @Override // g.a.j
    public void b(b0 b0Var, T t) {
        f.w.d<T> dVar = this.f4037h;
        g.a.n2.g gVar = dVar instanceof g.a.n2.g ? (g.a.n2.g) dVar : null;
        F(this, t, (gVar != null ? gVar.f4066h : null) == b0Var ? 4 : this.f4160g, null, 4, null);
    }

    @Override // g.a.v0
    public final f.w.d<T> c() {
        return this.f4037h;
    }

    @Override // g.a.v0
    public Throwable d(Object obj) {
        Throwable thD = super.d(obj);
        if (thD == null) {
            return null;
        }
        f.w.d<T> dVar = this.f4037h;
        return (l0.d() && (dVar instanceof f.w.k.a.e)) ? g.a.n2.a0.j(thD, (f.w.k.a.e) dVar) : thD;
    }

    @Override // g.a.j
    public void e(f.z.c.l<? super Throwable, f.t> lVar) {
        h hVarZ = z(lVar);
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof d)) {
                if (obj instanceof h) {
                    A(lVar, obj);
                    throw null;
                }
                boolean z = obj instanceof t;
                if (z) {
                    t tVar = (t) obj;
                    if (!tVar.b()) {
                        A(lVar, obj);
                        throw null;
                    }
                    if (obj instanceof m) {
                        if (!z) {
                            tVar = null;
                        }
                        j(lVar, tVar != null ? tVar.a : null);
                        return;
                    }
                    return;
                }
                if (obj instanceof s) {
                    s sVar = (s) obj;
                    if (sVar.f4144b != null) {
                        A(lVar, obj);
                        throw null;
                    }
                    if (sVar.c()) {
                        j(lVar, sVar.f4147e);
                        return;
                    } else {
                        if (l.compareAndSet(this, obj, s.b(sVar, null, hVarZ, null, null, null, 29, null))) {
                            return;
                        }
                    }
                } else {
                    if (l.compareAndSet(this, obj, new s(obj, hVarZ, null, null, null, 28, null))) {
                        return;
                    }
                }
            } else if (l.compareAndSet(this, obj, hVarZ)) {
                return;
            }
        }
    }

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

    @Override // f.w.k.a.e
    public f.w.k.a.e getCallerFrame() {
        f.w.d<T> dVar = this.f4037h;
        if (dVar instanceof f.w.k.a.e) {
            return (f.w.k.a.e) dVar;
        }
        return null;
    }

    @Override // f.w.d
    public f.w.g getContext() {
        return this.f4038i;
    }

    @Override // f.w.k.a.e
    public StackTraceElement getStackTraceElement() {
        return null;
    }

    @Override // g.a.v0
    public Object h() {
        return t();
    }

    public final void k(h hVar, Throwable th) {
        try {
            hVar.b(th);
        } catch (Throwable th2) {
            e0.a(getContext(), new w("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    public final void l(f.z.c.l<? super Throwable, f.t> lVar, Throwable th) {
        try {
            lVar.c(th);
        } catch (Throwable th2) {
            e0.a(getContext(), new w("Exception in resume onCancellation handler for " + this, th2));
        }
    }

    public boolean m(Throwable th) {
        Object obj;
        boolean z;
        do {
            obj = this._state;
            if (!(obj instanceof b2)) {
                return false;
            }
            z = obj instanceof h;
        } while (!l.compareAndSet(this, obj, new m(this, th, z)));
        h hVar = z ? (h) obj : null;
        if (hVar != null) {
            k(hVar, th);
        }
        p();
        q(this.f4160g);
        return true;
    }

    public final void o() {
        y0 y0Var = this.f4039j;
        if (y0Var == null) {
            return;
        }
        y0Var.dispose();
        this.f4039j = a2.f4009b;
    }

    public Throwable r(n1 n1Var) {
        return n1Var.n();
    }

    @Override // f.w.d
    public void resumeWith(Object obj) {
        F(this, x.c(obj, this), this.f4160g, null, 4, null);
    }

    public final Object s() {
        n1 n1Var;
        boolean zY = y();
        if (I()) {
            if (this.f4039j == null) {
                w();
            }
            if (zY) {
                D();
            }
            return f.w.j.d.d();
        }
        if (zY) {
            D();
        }
        Object objT = t();
        if (objT instanceof t) {
            Throwable th = ((t) objT).a;
            if (l0.d()) {
                throw g.a.n2.a0.j(th, this);
            }
            throw th;
        }
        if (!w0.b(this.f4160g) || (n1Var = (n1) getContext().get(n1.f4054e)) == null || n1Var.isActive()) {
            return f(objT);
        }
        CancellationException cancellationExceptionN = n1Var.n();
        a(objT, cancellationExceptionN);
        if (l0.d()) {
            throw g.a.n2.a0.j(cancellationExceptionN, this);
        }
        throw cancellationExceptionN;
    }

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

    public String toString() {
        return B() + '(' + m0.c(this.f4037h) + "){" + u() + "}@" + m0.b(this);
    }

    public void v() {
        y0 y0VarW = w();
        if (y0VarW != null && x()) {
            y0VarW.dispose();
            this.f4039j = a2.f4009b;
        }
    }

    public boolean x() {
        return !(t() instanceof b2);
    }
}
