package kotlinx.coroutines.o2;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlinx.coroutines.c0;
import kotlinx.coroutines.f0;
import kotlinx.coroutines.f1;
import kotlinx.coroutines.i0;
import kotlinx.coroutines.j2;
import kotlinx.coroutines.r0;
import kotlinx.coroutines.s0;
import kotlinx.coroutines.z0;

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final i0 f1105g;

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

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

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

    /* JADX WARN: Multi-variable type inference failed */
    public e(i0 i0Var, h.u.d<? super T> dVar) {
        super(-1);
        this.f1105g = i0Var;
        this.f1106h = dVar;
        this.f1107i = f.a;
        this.f1108j = u.b(getContext());
        this._reusableCancellableContinuation = null;
    }

    private final kotlinx.coroutines.o<?> m() {
        Object obj = this._reusableCancellableContinuation;
        if (obj instanceof kotlinx.coroutines.o) {
            return (kotlinx.coroutines.o) obj;
        }
        return null;
    }

    @Override // kotlinx.coroutines.z0
    public void a(Object obj, Throwable th) {
        if (obj instanceof c0) {
            ((c0) obj).b.invoke(th);
        }
    }

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

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

    @Override // h.u.d
    public void g(Object obj) {
        h.u.g context = this.f1106h.getContext();
        Object objD = f0.d(obj, null, 1, null);
        if (this.f1105g.J(context)) {
            this.f1107i = objD;
            this.f1175f = 0;
            this.f1105g.H(context, this);
            return;
        }
        r0.a();
        f1 f1VarB = j2.a.b();
        if (f1VarB.U()) {
            this.f1107i = objD;
            this.f1175f = 0;
            f1VarB.Q(this);
            return;
        }
        f1VarB.S(true);
        try {
            h.u.g context2 = getContext();
            Object objC = u.c(context2, this.f1108j);
            try {
                this.f1106h.g(obj);
                h.r rVar = h.r.a;
                while (f1VarB.X()) {
                }
            } finally {
                u.a(context2, objC);
            }
        } finally {
            try {
            } finally {
            }
        }
    }

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

    @Override // kotlinx.coroutines.z0
    public Object j() {
        Object obj = this.f1107i;
        if (r0.a()) {
            if (!(obj != f.a)) {
                throw new AssertionError();
            }
        }
        this.f1107i = f.a;
        return obj;
    }

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

    public final void l() {
        while (this._reusableCancellableContinuation == f.b) {
        }
    }

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

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

    public final void p() {
        l();
        kotlinx.coroutines.o<?> oVarM = m();
        if (oVarM == null) {
            return;
        }
        oVarM.r();
    }

    public final Throwable q(kotlinx.coroutines.n<?> nVar) {
        q qVar;
        do {
            Object obj = this._reusableCancellableContinuation;
            qVar = f.b;
            if (obj != qVar) {
                if (!(obj instanceof Throwable)) {
                    throw new IllegalStateException(h.x.d.k.i("Inconsistent state ", obj).toString());
                }
                if (f1104k.compareAndSet(this, obj, null)) {
                    return (Throwable) obj;
                }
                throw new IllegalArgumentException("Failed requirement.".toString());
            }
        } while (!f1104k.compareAndSet(this, qVar, nVar));
        return null;
    }

    public String toString() {
        return "DispatchedContinuation[" + this.f1105g + ", " + s0.c(this.f1106h) + ']';
    }
}
