package g.a.n2;

import g.a.b1;
import g.a.i2;
import g.a.l0;
import g.a.m0;
import g.a.v0;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: DispatchedContinuation.kt */
/* JADX INFO: loaded from: classes.dex */
public final class g<T> extends v0<T> implements f.w.k.a.e, f.w.d<T> {
    private static final /* synthetic */ AtomicReferenceFieldUpdater l = AtomicReferenceFieldUpdater.newUpdater(g.class, Object.class, "_reusableCancellableContinuation");
    private volatile /* synthetic */ Object _reusableCancellableContinuation;

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final f.w.d<T> f4067i;

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

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

    /* JADX WARN: Multi-variable type inference failed */
    public g(g.a.b0 b0Var, f.w.d<? super T> dVar) {
        super(-1);
        this.f4066h = b0Var;
        this.f4067i = dVar;
        this.f4068j = h.a;
        this.f4069k = f0.b(getContext());
        this._reusableCancellableContinuation = null;
    }

    private final g.a.k<?> j() {
        Object obj = this._reusableCancellableContinuation;
        if (obj instanceof g.a.k) {
            return (g.a.k) obj;
        }
        return null;
    }

    @Override // g.a.v0
    public void a(Object obj, Throwable th) {
        if (obj instanceof g.a.u) {
            ((g.a.u) obj).f4151b.c(th);
        }
    }

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

    @Override // f.w.k.a.e
    public f.w.k.a.e getCallerFrame() {
        f.w.d<T> dVar = this.f4067i;
        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.f4067i.getContext();
    }

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

    @Override // g.a.v0
    public Object h() {
        Object obj = this.f4068j;
        if (l0.a()) {
            if (!(obj != h.a)) {
                throw new AssertionError();
            }
        }
        this.f4068j = h.a;
        return obj;
    }

    public final void i() {
        while (this._reusableCancellableContinuation == h.f4070b) {
        }
    }

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

    public final boolean l(Throwable th) {
        while (true) {
            Object obj = this._reusableCancellableContinuation;
            if (f.z.d.k.a(obj, h.f4070b)) {
                if (l.compareAndSet(this, h.f4070b, th)) {
                    return true;
                }
            } else {
                if (obj instanceof Throwable) {
                    return true;
                }
                if (l.compareAndSet(this, obj, null)) {
                    return false;
                }
            }
        }
    }

    public final void m() {
        i();
        g.a.k<?> kVarJ = j();
        if (kVarJ != null) {
            kVarJ.o();
        }
    }

    public final Throwable n(g.a.j<?> jVar) {
        b0 b0Var;
        do {
            Object obj = this._reusableCancellableContinuation;
            b0Var = h.f4070b;
            if (obj != b0Var) {
                if (obj instanceof Throwable) {
                    if (l.compareAndSet(this, obj, null)) {
                        return (Throwable) obj;
                    }
                    throw new IllegalArgumentException("Failed requirement.".toString());
                }
                throw new IllegalStateException(("Inconsistent state " + obj).toString());
            }
        } while (!l.compareAndSet(this, b0Var, jVar));
        return null;
    }

    @Override // f.w.d
    public void resumeWith(Object obj) {
        f.w.g context = this.f4067i.getContext();
        Object objD = g.a.x.d(obj, null, 1, null);
        if (this.f4066h.x(context)) {
            this.f4068j = objD;
            this.f4160g = 0;
            this.f4066h.w(context, this);
            return;
        }
        l0.a();
        b1 b1VarA = i2.a.a();
        if (b1VarA.M()) {
            this.f4068j = objD;
            this.f4160g = 0;
            b1VarA.D(this);
            return;
        }
        b1VarA.H(true);
        try {
            f.w.g context2 = getContext();
            Object objC = f0.c(context2, this.f4069k);
            try {
                this.f4067i.resumeWith(obj);
                f.t tVar = f.t.a;
                while (b1VarA.P()) {
                }
            } finally {
                f0.a(context2, objC);
            }
        } finally {
            try {
            } finally {
            }
        }
    }

    public String toString() {
        return "DispatchedContinuation[" + this.f4066h + ", " + m0.c(this.f4067i) + ']';
    }
}
