package h.a.k1;

import h.a.a0;
import h.a.d1;
import h.a.f0;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: DispatchedContinuation.kt */
/* JADX INFO: loaded from: classes.dex */
@g.i
public final class d<T> extends a0<T> implements g.e0.j.a.d, g.e0.d<T> {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f4243g = AtomicReferenceFieldUpdater.newUpdater(d.class, Object.class, "_reusableCancellableContinuation");
    private volatile /* synthetic */ Object _reusableCancellableContinuation;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final h.a.q f4244h;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public Object f4246j;

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

    /* JADX WARN: Multi-variable type inference failed */
    public d(h.a.q qVar, g.e0.d<? super T> dVar) {
        super(-1);
        this.f4244h = qVar;
        this.f4245i = dVar;
        this.f4246j = e.a;
        Object objFold = e().fold(0, t.f4267b);
        g.h0.c.l.c(objFold);
        this.f4247k = objFold;
        this._reusableCancellableContinuation = null;
    }

    @Override // h.a.a0
    public void a(Object obj, Throwable th) {
        if (obj instanceof h.a.m) {
            ((h.a.m) obj).f4317b.invoke(th);
        }
    }

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

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

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

    @Override // g.e0.d
    public void f(Object obj) {
        g.e0.f fVarE;
        Object objB;
        g.e0.f fVarE2 = this.f4245i.e();
        Object objN1 = f.a.y.a.n1(obj, null);
        if (this.f4244h.q(fVarE2)) {
            this.f4246j = objN1;
            this.f4206f = 0;
            this.f4244h.e(fVarE2, this);
            return;
        }
        d1 d1Var = d1.a;
        f0 f0VarA = d1.a();
        if (f0VarA.y()) {
            this.f4246j = objN1;
            this.f4206f = 0;
            f0VarA.w(this);
            return;
        }
        f0VarA.x(true);
        try {
            fVarE = e();
            objB = t.b(fVarE, this.f4247k);
        } finally {
            try {
            } finally {
            }
        }
        try {
            this.f4245i.f(obj);
            while (f0VarA.z()) {
            }
        } finally {
            t.a(fVarE, objB);
        }
    }

    @Override // h.a.a0
    public Object i() {
        Object obj = this.f4246j;
        this.f4246j = e.a;
        return obj;
    }

    public final boolean j() {
        return this._reusableCancellableContinuation != null;
    }

    public final boolean k(Throwable th) {
        while (true) {
            Object obj = this._reusableCancellableContinuation;
            r rVar = e.f4248b;
            if (g.h0.c.l.a(obj, rVar)) {
                if (f4243g.compareAndSet(this, rVar, th)) {
                    return true;
                }
            } else {
                if (obj instanceof Throwable) {
                    return true;
                }
                if (f4243g.compareAndSet(this, obj, null)) {
                    return false;
                }
            }
        }
    }

    public final void l() {
        while (this._reusableCancellableContinuation == e.f4248b) {
        }
        Object obj = this._reusableCancellableContinuation;
        h.a.f fVar = obj instanceof h.a.f ? (h.a.f) obj : null;
        if (fVar == null) {
            return;
        }
        fVar.n();
    }

    public final Throwable m(h.a.e<?> eVar) {
        r rVar;
        do {
            Object obj = this._reusableCancellableContinuation;
            rVar = e.f4248b;
            if (obj != rVar) {
                if (!(obj instanceof Throwable)) {
                    throw new IllegalStateException(g.h0.c.l.k("Inconsistent state ", obj).toString());
                }
                if (f4243g.compareAndSet(this, obj, null)) {
                    return (Throwable) obj;
                }
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
        } while (!f4243g.compareAndSet(this, rVar, eVar));
        return null;
    }

    public String toString() {
        StringBuilder sbB = d.b.a.a.a.B("DispatchedContinuation[");
        sbB.append(this.f4244h);
        sbB.append(", ");
        sbB.append(h.a.u.c(this.f4245i));
        sbB.append(']');
        return sbB.toString();
    }
}
