package k9; import java.util.concurrent.CancellationException; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import k9.u1; /* loaded from: classes2.dex */ public class n extends x0 implements m, kotlin.coroutines.jvm.internal.e { /* renamed from: g, reason: collision with root package name */ private static final /* synthetic */ AtomicIntegerFieldUpdater f11371g = AtomicIntegerFieldUpdater.newUpdater(n.class, "_decision"); /* renamed from: h, reason: collision with root package name */ private static final /* synthetic */ AtomicReferenceFieldUpdater f11372h = AtomicReferenceFieldUpdater.newUpdater(n.class, Object.class, "_state"); private volatile /* synthetic */ int _decision; private volatile /* synthetic */ Object _state; /* renamed from: d, reason: collision with root package name */ private final v8.d f11373d; /* renamed from: e, reason: collision with root package name */ private final v8.g f11374e; /* renamed from: f, reason: collision with root package name */ private b1 f11375f; public n(v8.d dVar, int i10) { super(i10); this.f11373d = dVar; this.f11374e = dVar.getContext(); this._decision = 0; this._state = d.f11320a; } private final boolean B() { return y0.c(this.f11403c) && ((kotlinx.coroutines.internal.f) this.f11373d).p(); } private final k C(c9.l lVar) { return lVar instanceof k ? (k) lVar : new r1(lVar); } private final void D(c9.l lVar, Object obj) { throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + lVar + ", already has " + obj).toString()); } private final void G() { v8.d dVar = this.f11373d; kotlinx.coroutines.internal.f fVar = dVar instanceof kotlinx.coroutines.internal.f ? (kotlinx.coroutines.internal.f) dVar : null; Throwable s10 = fVar != null ? fVar.s(this) : null; if (s10 == null) { return; } r(); f(s10); } private final void I(Object obj, int i10, c9.l lVar) { Object obj2; do { obj2 = this._state; if (!(obj2 instanceof j2)) { if (obj2 instanceof q) { q qVar = (q) obj2; if (qVar.c()) { if (lVar == null) { return; } p(lVar, qVar.f11296a); return; } } m(obj); throw new s8.d(); } } while (!androidx.concurrent.futures.b.a(f11372h, this, obj2, K((j2) obj2, obj, i10, lVar, null))); s(); t(i10); } static /* synthetic */ void J(n nVar, Object obj, int i10, c9.l lVar, int i11, Object obj2) { if (obj2 != null) { throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: resumeImpl"); } if ((i11 & 4) != 0) { lVar = null; } nVar.I(obj, i10, lVar); } private final Object K(j2 j2Var, Object obj, int i10, c9.l lVar, Object obj2) { if (obj instanceof a0) { return obj; } if (!y0.b(i10) && obj2 == null) { return obj; } if (lVar != null || (((j2Var instanceof k) && !(j2Var instanceof f)) || obj2 != null)) { return new z(obj, j2Var instanceof k ? (k) j2Var : null, lVar, obj2, null, 16, null); } return obj; } private final boolean L() { do { int i10 = this._decision; if (i10 != 0) { if (i10 == 1) { return false; } throw new IllegalStateException("Already resumed".toString()); } } while (!f11371g.compareAndSet(this, 0, 2)); return true; } private final kotlinx.coroutines.internal.y M(Object obj, Object obj2, c9.l lVar) { Object obj3; do { obj3 = this._state; if (!(obj3 instanceof j2)) { if ((obj3 instanceof z) && obj2 != null && ((z) obj3).f11414d == obj2) { return o.f11377a; } return null; } } while (!androidx.concurrent.futures.b.a(f11372h, this, obj3, K((j2) obj3, obj, this.f11403c, lVar, obj2))); s(); return o.f11377a; } private final boolean N() { do { int i10 = this._decision; if (i10 != 0) { if (i10 == 2) { return false; } throw new IllegalStateException("Already suspended".toString()); } } while (!f11371g.compareAndSet(this, 0, 1)); return true; } private final Void m(Object obj) { throw new IllegalStateException(d9.j.l("Already resumed, but proposed with update ", obj).toString()); } private final void n(c9.l lVar, Throwable th) { try { lVar.invoke(th); } catch (Throwable th2) { j0.a(getContext(), new d0(d9.j.l("Exception in invokeOnCancellation handler for ", this), th2)); } } private final boolean q(Throwable th) { if (B()) { return ((kotlinx.coroutines.internal.f) this.f11373d).q(th); } return false; } private final void s() { if (B()) { return; } r(); } private final void t(int i10) { if (L()) { return; } y0.a(this, i10); } private final String x() { Object w10 = w(); return w10 instanceof j2 ? "Active" : w10 instanceof q ? "Cancelled" : "Completed"; } private final b1 z() { u1 u1Var = (u1) getContext().get(u1.T); if (u1Var == null) { return null; } b1 d10 = u1.a.d(u1Var, true, false, new r(this), 2, null); this.f11375f = d10; return d10; } public boolean A() { return !(w() instanceof j2); } protected String E() { return "CancellableContinuation"; } public final void F(Throwable th) { if (q(th)) { return; } f(th); s(); } public final boolean H() { Object obj = this._state; if ((obj instanceof z) && ((z) obj).f11414d != null) { r(); return false; } this._decision = 0; this._state = d.f11320a; return true; } @Override // k9.m public Object a(Object obj, Object obj2, c9.l lVar) { return M(obj, obj2, lVar); } @Override // k9.m public void b(c9.l lVar) { k C = C(lVar); while (true) { Object obj = this._state; if (obj instanceof d) { if (androidx.concurrent.futures.b.a(f11372h, this, obj, C)) { return; } } else if (obj instanceof k) { D(lVar, obj); } else { boolean z10 = obj instanceof a0; if (z10) { a0 a0Var = (a0) obj; if (!a0Var.b()) { D(lVar, obj); } if (obj instanceof q) { if (!z10) { a0Var = null; } n(lVar, a0Var != null ? a0Var.f11296a : null); return; } return; } if (obj instanceof z) { z zVar = (z) obj; if (zVar.f11412b != null) { D(lVar, obj); } if (C instanceof f) { return; } if (zVar.c()) { n(lVar, zVar.f11415e); return; } else { if (androidx.concurrent.futures.b.a(f11372h, this, obj, z.b(zVar, null, C, null, null, null, 29, null))) { return; } } } else { if (C instanceof f) { return; } if (androidx.concurrent.futures.b.a(f11372h, this, obj, new z(obj, C, null, null, null, 28, null))) { return; } } } } } @Override // k9.x0 public void c(Object obj, Throwable th) { while (true) { Object obj2 = this._state; if (obj2 instanceof j2) { throw new IllegalStateException("Not completed".toString()); } if (obj2 instanceof a0) { return; } if (obj2 instanceof z) { z zVar = (z) obj2; if (!(!zVar.c())) { throw new IllegalStateException("Must be called at most once".toString()); } if (androidx.concurrent.futures.b.a(f11372h, this, obj2, z.b(zVar, null, null, null, null, th, 15, null))) { zVar.d(this, th); return; } } else if (androidx.concurrent.futures.b.a(f11372h, this, obj2, new z(obj2, null, null, null, th, 14, null))) { return; } } } @Override // k9.x0 public final v8.d d() { return this.f11373d; } @Override // k9.m public void e(Object obj, c9.l lVar) { I(obj, this.f11403c, lVar); } @Override // k9.m public boolean f(Throwable th) { Object obj; boolean z10; do { obj = this._state; if (!(obj instanceof j2)) { return false; } z10 = obj instanceof k; } while (!androidx.concurrent.futures.b.a(f11372h, this, obj, new q(this, th, z10))); k kVar = z10 ? (k) obj : null; if (kVar != null) { o(kVar, th); } s(); t(this.f11403c); return true; } @Override // k9.m public void g(g0 g0Var, Object obj) { v8.d dVar = this.f11373d; kotlinx.coroutines.internal.f fVar = dVar instanceof kotlinx.coroutines.internal.f ? (kotlinx.coroutines.internal.f) dVar : null; J(this, obj, (fVar != null ? fVar.f11447d : null) == g0Var ? 4 : this.f11403c, null, 4, null); } @Override // kotlin.coroutines.jvm.internal.e public kotlin.coroutines.jvm.internal.e getCallerFrame() { v8.d dVar = this.f11373d; if (dVar instanceof kotlin.coroutines.jvm.internal.e) { return (kotlin.coroutines.jvm.internal.e) dVar; } return null; } @Override // v8.d public v8.g getContext() { return this.f11374e; } @Override // k9.m public void h(Object obj) { t(this.f11403c); } @Override // k9.x0 public Throwable i(Object obj) { Throwable i10 = super.i(obj); if (i10 == null) { return null; } d(); return i10; } @Override // k9.x0 public Object j(Object obj) { return obj instanceof z ? ((z) obj).f11411a : obj; } @Override // k9.x0 public Object l() { return w(); } public final void o(k kVar, Throwable th) { try { kVar.a(th); } catch (Throwable th2) { j0.a(getContext(), new d0(d9.j.l("Exception in invokeOnCancellation handler for ", this), th2)); } } public final void p(c9.l lVar, Throwable th) { try { lVar.invoke(th); } catch (Throwable th2) { j0.a(getContext(), new d0(d9.j.l("Exception in resume onCancellation handler for ", this), th2)); } } public final void r() { b1 b1Var = this.f11375f; if (b1Var == null) { return; } b1Var.e(); this.f11375f = i2.f11356a; } @Override // v8.d public void resumeWith(Object obj) { J(this, e0.c(obj, this), this.f11403c, null, 4, null); } public String toString() { return E() + '(' + o0.c(this.f11373d) + "){" + x() + "}@" + o0.b(this); } public Throwable u(u1 u1Var) { return u1Var.S(); } public final Object v() { u1 u1Var; Object c10; boolean B = B(); if (N()) { if (this.f11375f == null) { z(); } if (B) { G(); } c10 = w8.d.c(); return c10; } if (B) { G(); } Object w10 = w(); if (w10 instanceof a0) { throw ((a0) w10).f11296a; } if (!y0.b(this.f11403c) || (u1Var = (u1) getContext().get(u1.T)) == null || u1Var.c()) { return j(w10); } CancellationException S = u1Var.S(); c(w10, S); throw S; } public final Object w() { return this._state; } public void y() { b1 z10 = z(); if (z10 != null && A()) { z10.e(); this.f11375f = i2.f11356a; } } }