package h.a;

import h.a.r0;
import java.util.Objects;
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 */
@g.i
public class f<T> extends a0<T> implements e<T>, g.e0.j.a.d {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f4212g = AtomicIntegerFieldUpdater.newUpdater(f.class, "_decision");

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final g.e0.d<T> f4214i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final g.e0.f f4215j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public d0 f4216k;

    /* JADX WARN: Multi-variable type inference failed */
    public f(g.e0.d<? super T> dVar, int i2) {
        super(i2);
        this.f4214i = dVar;
        this.f4215j = dVar.e();
        this._decision = 0;
        this._state = b.f4207b;
    }

    @Override // h.a.a0
    public void a(Object obj, Throwable th) {
        while (true) {
            Object obj2 = this._state;
            if (obj2 instanceof a1) {
                throw new IllegalStateException("Not completed".toString());
            }
            if (obj2 instanceof l) {
                return;
            }
            if (obj2 instanceof k) {
                k kVar = (k) obj2;
                if (!(!(kVar.f4240e != null))) {
                    throw new IllegalStateException("Must be called at most once".toString());
                }
                if (f4213h.compareAndSet(this, obj2, k.a(kVar, null, null, null, null, th, 15))) {
                    d dVar = kVar.f4237b;
                    if (dVar != null) {
                        k(dVar, th);
                    }
                    g.h0.b.l<Throwable, g.a0> lVar = kVar.f4238c;
                    if (lVar == null) {
                        return;
                    }
                    l(lVar, th);
                    return;
                }
            } else if (f4213h.compareAndSet(this, obj2, new k(obj2, null, null, null, th, 14))) {
                return;
            }
        }
    }

    @Override // h.a.a0
    public final g.e0.d<T> b() {
        return this.f4214i;
    }

    @Override // g.e0.j.a.d
    public g.e0.j.a.d c() {
        g.e0.d<T> dVar = this.f4214i;
        if (dVar instanceof g.e0.j.a.d) {
            return (g.e0.j.a.d) dVar;
        }
        return null;
    }

    @Override // h.a.a0
    public Throwable d(Object obj) {
        Throwable thD = super.d(obj);
        if (thD == null) {
            return null;
        }
        return thD;
    }

    @Override // g.e0.d
    public g.e0.f e() {
        return this.f4215j;
    }

    @Override // g.e0.d
    public void f(Object obj) {
        Object obj2;
        Object kVar;
        Throwable thA = g.l.a(obj);
        if (thA != null) {
            obj = new l(thA, false, 2);
        }
        int i2 = this.f4206f;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof a1)) {
                if (obj2 instanceof g) {
                    g gVar = (g) obj2;
                    Objects.requireNonNull(gVar);
                    if (g.f4220c.compareAndSet(gVar, 0, 1)) {
                        return;
                    }
                }
                throw new IllegalStateException(g.h0.c.l.k("Already resumed, but proposed with update ", obj).toString());
            }
            a1 a1Var = (a1) obj2;
            if (!(obj instanceof l) && b0.a(i2) && (a1Var instanceof d)) {
                kVar = new k(obj, a1Var instanceof d ? (d) a1Var : null, null, null, null, 16);
            } else {
                kVar = obj;
            }
        } while (!f4213h.compareAndSet(this, obj2, kVar));
        o();
        p(i2);
    }

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

    @Override // h.a.a0
    public Object i() {
        return this._state;
    }

    public final void j(g.h0.b.l<? super Throwable, g.a0> lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            f.a.y.a.j0(this.f4215j, new o(g.h0.c.l.k("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    public final void k(d dVar, Throwable th) {
        try {
            dVar.a(th);
        } catch (Throwable th2) {
            f.a.y.a.j0(this.f4215j, new o(g.h0.c.l.k("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    public final void l(g.h0.b.l<? super Throwable, g.a0> lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            f.a.y.a.j0(this.f4215j, new o(g.h0.c.l.k("Exception in resume onCancellation handler for ", this), th2));
        }
    }

    public boolean m(Throwable th) {
        Object obj;
        boolean z;
        do {
            obj = this._state;
            if (!(obj instanceof a1)) {
                return false;
            }
            z = obj instanceof d;
        } while (!f4213h.compareAndSet(this, obj, new g(this, th, z)));
        d dVar = z ? (d) obj : null;
        if (dVar != null) {
            k(dVar, th);
        }
        o();
        p(this.f4206f);
        return true;
    }

    public final void n() {
        d0 d0Var = this.f4216k;
        if (d0Var == null) {
            return;
        }
        d0Var.dispose();
        this.f4216k = z0.f4348b;
    }

    public final void o() {
        if (s()) {
            return;
        }
        n();
    }

    public final void p(int i2) {
        boolean z;
        while (true) {
            int i3 = this._decision;
            if (i3 != 0) {
                if (i3 != 1) {
                    throw new IllegalStateException("Already resumed".toString());
                }
                z = false;
            } else if (f4212g.compareAndSet(this, 0, 2)) {
                z = true;
                break;
            }
        }
        if (z) {
            return;
        }
        g.e0.d<T> dVarB = b();
        boolean z2 = i2 == 4;
        if (z2 || !(dVarB instanceof h.a.k1.d) || b0.a(i2) != b0.a(this.f4206f)) {
            b0.b(this, dVarB, z2);
            return;
        }
        q qVar = ((h.a.k1.d) dVarB).f4244h;
        g.e0.f fVarE = dVarB.e();
        if (qVar.q(fVarE)) {
            qVar.e(fVarE, this);
            return;
        }
        d1 d1Var = d1.a;
        f0 f0VarA = d1.a();
        if (f0VarA.y()) {
            f0VarA.w(this);
            return;
        }
        f0VarA.x(true);
        try {
            b0.b(this, b(), true);
            do {
            } while (f0VarA.z());
        } finally {
            try {
            } finally {
            }
        }
    }

    public final Object q() throws Throwable {
        boolean z;
        boolean zS = s();
        while (true) {
            int i2 = this._decision;
            z = false;
            if (i2 != 0) {
                if (i2 != 2) {
                    throw new IllegalStateException("Already suspended".toString());
                }
            } else if (f4212g.compareAndSet(this, 0, 1)) {
                z = true;
                break;
            }
        }
        if (z) {
            if (this.f4216k == null) {
                g.e0.f fVar = this.f4215j;
                int i3 = r0.f4326d;
                r0 r0Var = (r0) fVar.get(r0.a.f4327b);
                if (r0Var != null) {
                    this.f4216k = f.a.y.a.n0(r0Var, true, false, new h(this), 2, null);
                }
            }
            if (zS) {
                u();
            }
            return g.e0.i.a.COROUTINE_SUSPENDED;
        }
        if (zS) {
            u();
        }
        Object obj = this._state;
        if (obj instanceof l) {
            throw ((l) obj).f4276b;
        }
        if (b0.a(this.f4206f)) {
            g.e0.f fVar2 = this.f4215j;
            int i4 = r0.f4326d;
            r0 r0Var2 = (r0) fVar2.get(r0.a.f4327b);
            if (r0Var2 != null && !r0Var2.a()) {
                CancellationException cancellationExceptionI = r0Var2.i();
                a(obj, cancellationExceptionI);
                throw cancellationExceptionI;
            }
        }
        return g(obj);
    }

    public void r(g.h0.b.l<? super Throwable, g.a0> lVar) {
        d o0Var = lVar instanceof d ? (d) lVar : new o0(lVar);
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof b)) {
                if (obj instanceof d) {
                    t(lVar, obj);
                    throw null;
                }
                boolean z = obj instanceof l;
                if (z) {
                    l lVar2 = (l) obj;
                    Objects.requireNonNull(lVar2);
                    if (!l.a.compareAndSet(lVar2, 0, 1)) {
                        t(lVar, obj);
                        throw null;
                    }
                    if (obj instanceof g) {
                        if (!z) {
                            lVar2 = null;
                        }
                        j(lVar, lVar2 != null ? lVar2.f4276b : null);
                        return;
                    }
                    return;
                }
                if (obj instanceof k) {
                    k kVar = (k) obj;
                    if (kVar.f4237b != null) {
                        t(lVar, obj);
                        throw null;
                    }
                    Throwable th = kVar.f4240e;
                    if (th != null) {
                        j(lVar, th);
                        return;
                    } else {
                        if (f4213h.compareAndSet(this, obj, k.a(kVar, null, o0Var, null, null, null, 29))) {
                            return;
                        }
                    }
                } else {
                    if (f4213h.compareAndSet(this, obj, new k(obj, o0Var, null, null, null, 28))) {
                        return;
                    }
                }
            } else if (f4213h.compareAndSet(this, obj, o0Var)) {
                return;
            }
        }
    }

    public final boolean s() {
        return (this.f4206f == 2) && ((h.a.k1.d) this.f4214i).j();
    }

    public final void t(g.h0.b.l<? super Throwable, g.a0> lVar, Object obj) {
        throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + lVar + ", already has " + obj).toString());
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("CancellableContinuation");
        sb.append('(');
        sb.append(u.c(this.f4214i));
        sb.append("){");
        Object obj = this._state;
        sb.append(obj instanceof a1 ? "Active" : obj instanceof g ? "Cancelled" : "Completed");
        sb.append("}@");
        sb.append(u.b(this));
        return sb.toString();
    }

    public final void u() {
        g.e0.d<T> dVar = this.f4214i;
        h.a.k1.d dVar2 = dVar instanceof h.a.k1.d ? (h.a.k1.d) dVar : null;
        Throwable thM = dVar2 != null ? dVar2.m(this) : null;
        if (thM == null) {
            return;
        }
        n();
        m(thM);
    }
}
