package kotlinx.coroutines;

import androidx.media3.common.C;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.Result;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes.dex */
public class h extends f0 implements g, nd.b, v1 {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f29435f = AtomicIntegerFieldUpdater.newUpdater(h.class, "_decisionAndIndex$volatile");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29436g = AtomicReferenceFieldUpdater.newUpdater(h.class, Object.class, "_state$volatile");

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Continuation f29438d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final kotlin.coroutines.h f29439e;

    public h(int i10, Continuation continuation) {
        super(i10);
        this.f29438d = continuation;
        this.f29439e = continuation.getContext();
        this._decisionAndIndex$volatile = 536870911;
        this._state$volatile = b.f29292a;
    }

    public static Object C(l1 l1Var, Object obj, int i10, sd.l lVar) {
        if ((obj instanceof q) || !y.H(i10)) {
            return obj;
        }
        if (lVar != null || (l1Var instanceof f)) {
            return new p(obj, l1Var instanceof f ? (f) l1Var : null, lVar, (CancellationException) null, 16);
        }
        return obj;
    }

    public static void y(Object obj, Object obj2) {
        throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + obj + ", already has " + obj2).toString());
    }

    public final void A() {
        Continuation continuation = this.f29438d;
        Throwable th = null;
        kotlinx.coroutines.internal.g gVar = continuation instanceof kotlinx.coroutines.internal.g ? (kotlinx.coroutines.internal.g) continuation : null;
        if (gVar != null) {
            loop0: while (true) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = kotlinx.coroutines.internal.g.f29460h;
                Object obj = atomicReferenceFieldUpdater.get(gVar);
                la.e eVar = kotlinx.coroutines.internal.a.f29452d;
                if (obj == eVar) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(gVar, eVar, this)) {
                        if (atomicReferenceFieldUpdater.get(gVar) != eVar) {
                            break;
                        }
                    }
                    break loop0;
                } else {
                    if (!(obj instanceof Throwable)) {
                        throw new IllegalStateException(("Inconsistent state " + obj).toString());
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(gVar, obj, null)) {
                        if (atomicReferenceFieldUpdater.get(gVar) != obj) {
                            throw new IllegalArgumentException("Failed requirement.".toString());
                        }
                    }
                    th = (Throwable) obj;
                }
            }
            if (th == null) {
                return;
            }
            p();
            o(th);
        }
    }

    public final void B(Object obj, int i10, sd.l lVar) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29436g;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (obj2 instanceof l1) {
                Object objC = C((l1) obj2, obj, i10, lVar);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objC)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                if (!x()) {
                    p();
                }
                q(i10);
                return;
            }
            if (obj2 instanceof i) {
                i iVar = (i) obj2;
                iVar.getClass();
                if (i.f29443c.compareAndSet(iVar, 0, 1)) {
                    if (lVar != null) {
                        m(lVar, iVar.f29523a);
                        return;
                    }
                    return;
                }
            }
            throw new IllegalStateException(("Already resumed, but proposed with update " + obj).toString());
        }
    }

    @Override // kotlinx.coroutines.v1
    public final void a(kotlinx.coroutines.internal.s sVar, int i10) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i11;
        do {
            atomicIntegerFieldUpdater = f29435f;
            i11 = atomicIntegerFieldUpdater.get(this);
            if ((i11 & 536870911) != 536870911) {
                throw new IllegalStateException("invokeOnCancellation should be called at most once".toString());
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i11, ((i11 >> 29) << 29) + i10));
        w(sVar);
    }

    @Override // kotlinx.coroutines.f0
    public final void b(Object obj, CancellationException cancellationException) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29436g;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (obj2 instanceof l1) {
                throw new IllegalStateException("Not completed".toString());
            }
            if (obj2 instanceof q) {
                return;
            }
            if (!(obj2 instanceof p)) {
                p pVar = new p(obj2, (f) null, (sd.l) null, cancellationException, 14);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, pVar)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                return;
            }
            p pVar2 = (p) obj2;
            if (!(!(pVar2.f29518e != null))) {
                throw new IllegalStateException("Must be called at most once".toString());
            }
            p pVarA = p.a(pVar2, null, cancellationException, 15);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, pVarA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj2) {
                    break;
                }
            }
            f fVar = pVar2.f29515b;
            if (fVar != null) {
                l(fVar, cancellationException);
            }
            sd.l lVar = pVar2.f29516c;
            if (lVar != null) {
                m(lVar, cancellationException);
                return;
            }
            return;
        }
    }

    @Override // kotlinx.coroutines.f0
    public final Continuation c() {
        return this.f29438d;
    }

    @Override // kotlinx.coroutines.g
    public final void d(Object obj, sd.l lVar) {
        B(obj, this.f29356c, lVar);
    }

    @Override // kotlinx.coroutines.f0
    public final Throwable e(Object obj) {
        Throwable thE = super.e(obj);
        if (thE != null) {
            return thE;
        }
        return null;
    }

    @Override // kotlinx.coroutines.g
    public final void f(t tVar) {
        kd.e eVar = kd.e.f29109a;
        Continuation continuation = this.f29438d;
        kotlinx.coroutines.internal.g gVar = continuation instanceof kotlinx.coroutines.internal.g ? (kotlinx.coroutines.internal.g) continuation : null;
        B(eVar, (gVar != null ? gVar.f29461d : null) == tVar ? 4 : this.f29356c, null);
    }

    @Override // kotlinx.coroutines.f0
    public final Object g(Object obj) {
        return obj instanceof p ? ((p) obj).f29514a : obj;
    }

    @Override // nd.b
    public final nd.b getCallerFrame() {
        Continuation continuation = this.f29438d;
        if (continuation instanceof nd.b) {
            return (nd.b) continuation;
        }
        return null;
    }

    @Override // kotlin.coroutines.Continuation
    public final kotlin.coroutines.h getContext() {
        return this.f29439e;
    }

    @Override // kotlinx.coroutines.g
    public final la.e h(Object obj, sd.l lVar) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29436g;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            boolean z10 = obj2 instanceof l1;
            la.e eVar = y.f29582i;
            if (!z10) {
                boolean z11 = obj2 instanceof p;
                return null;
            }
            Object objC = C((l1) obj2, obj, this.f29356c, lVar);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objC)) {
                if (atomicReferenceFieldUpdater.get(this) != obj2) {
                    break;
                }
            }
            if (x()) {
                return eVar;
            }
            p();
            return eVar;
        }
    }

    @Override // kotlinx.coroutines.f0
    public final Object j() {
        return f29436g.get(this);
    }

    @Override // kotlinx.coroutines.g
    public final void k(Object obj) {
        q(this.f29356c);
    }

    public final void l(f fVar, Throwable th) {
        try {
            fVar.c(th);
        } catch (Throwable th2) {
            hb.a.g0(this.f29439e, new CompletionHandlerException("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    public final void m(sd.l lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            hb.a.g0(this.f29439e, new CompletionHandlerException("Exception in resume onCancellation handler for " + this, th2));
        }
    }

    public final void n(kotlinx.coroutines.internal.s sVar, Throwable th) {
        kotlin.coroutines.h hVar = this.f29439e;
        int i10 = f29435f.get(this) & 536870911;
        if (i10 == 536870911) {
            throw new IllegalStateException("The index for Segment.onCancellation(..) is broken".toString());
        }
        try {
            sVar.g(i10, hVar);
        } catch (Throwable th2) {
            hb.a.g0(hVar, new CompletionHandlerException("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    public final void o(Throwable th) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29436g;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof l1) {
                i iVar = new i(this, th, (obj instanceof f) || (obj instanceof kotlinx.coroutines.internal.s));
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, iVar)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                l1 l1Var = (l1) obj;
                if (l1Var instanceof f) {
                    l((f) obj, th);
                } else if (l1Var instanceof kotlinx.coroutines.internal.s) {
                    n((kotlinx.coroutines.internal.s) obj, th);
                }
                if (!x()) {
                    p();
                }
                q(this.f29356c);
                return;
            }
            return;
        }
    }

    public final void p() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29437h;
        j0 j0Var = (j0) atomicReferenceFieldUpdater.get(this);
        if (j0Var == null) {
            return;
        }
        j0Var.dispose();
        atomicReferenceFieldUpdater.set(this, k1.f29507a);
    }

    public final void q(int i10) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i11;
        do {
            atomicIntegerFieldUpdater = f29435f;
            i11 = atomicIntegerFieldUpdater.get(this);
            int i12 = i11 >> 29;
            if (i12 != 0) {
                if (i12 != 1) {
                    throw new IllegalStateException("Already resumed".toString());
                }
                boolean z10 = i10 == 4;
                Continuation continuation = this.f29438d;
                if (z10 || !(continuation instanceof kotlinx.coroutines.internal.g) || y.H(i10) != y.H(this.f29356c)) {
                    y.U(this, continuation, z10);
                    return;
                }
                t tVar = ((kotlinx.coroutines.internal.g) continuation).f29461d;
                kotlin.coroutines.h context = continuation.getContext();
                if (tVar.M(context)) {
                    tVar.L(context, this);
                    return;
                }
                q0 q0VarA = q1.a();
                if (q0VarA.f29525c < IjkMediaMeta.AV_CH_WIDE_RIGHT) {
                    q0VarA.Q(true);
                    try {
                        y.U(this, continuation, true);
                        do {
                        } while (q0VarA.S());
                    } finally {
                        try {
                        } finally {
                        }
                    }
                    return;
                }
                kotlin.collections.h hVar = q0VarA.f29527e;
                if (hVar == null) {
                    hVar = new kotlin.collections.h();
                    q0VarA.f29527e = hVar;
                }
                hVar.addLast(this);
                return;
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i11, 1073741824 + (536870911 & i11)));
    }

    public Throwable r(g1 g1Var) {
        return g1Var.z();
    }

    @Override // kotlin.coroutines.Continuation
    public final void resumeWith(Object obj) {
        Throwable thA = Result.a(obj);
        if (thA != null) {
            obj = new q(thA, false);
        }
        B(obj, this.f29356c, null);
    }

    public final Object s() throws Throwable {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i10;
        boolean zX = x();
        do {
            atomicIntegerFieldUpdater = f29435f;
            i10 = atomicIntegerFieldUpdater.get(this);
            int i11 = i10 >> 29;
            if (i11 != 0) {
                if (i11 != 2) {
                    throw new IllegalStateException("Already suspended".toString());
                }
                if (zX) {
                    A();
                }
                Object obj = f29436g.get(this);
                if (obj instanceof q) {
                    throw ((q) obj).f29523a;
                }
                if (y.H(this.f29356c)) {
                    y0 y0Var = (y0) this.f29439e.m(u.f29565b);
                    if (y0Var != null && !y0Var.isActive()) {
                        CancellationException cancellationExceptionZ = ((g1) y0Var).z();
                        b(obj, cancellationExceptionZ);
                        throw cancellationExceptionZ;
                    }
                }
                return g(obj);
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i10, C.BUFFER_FLAG_LAST_SAMPLE + (536870911 & i10)));
        if (((j0) f29437h.get(this)) == null) {
            u();
        }
        if (zX) {
            A();
        }
        return CoroutineSingletons.f29226a;
    }

    public final void t() {
        j0 j0VarU = u();
        if (j0VarU != null && (!(f29436g.get(this) instanceof l1))) {
            j0VarU.dispose();
            f29437h.set(this, k1.f29507a);
        }
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(z());
        sb.append('(');
        sb.append(y.e0(this.f29438d));
        sb.append("){");
        Object obj = f29436g.get(this);
        sb.append(obj instanceof l1 ? "Active" : obj instanceof i ? "Cancelled" : "Completed");
        sb.append("}@");
        sb.append(y.z(this));
        return sb.toString();
    }

    public final j0 u() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        y0 y0Var = (y0) this.f29439e.m(u.f29565b);
        if (y0Var == null) {
            return null;
        }
        j0 j0VarG = nb.g.G(y0Var, true, new j(this), 2);
        do {
            atomicReferenceFieldUpdater = f29437h;
            if (atomicReferenceFieldUpdater.compareAndSet(this, null, j0VarG)) {
                break;
            }
        } while (atomicReferenceFieldUpdater.get(this) == null);
        return j0VarG;
    }

    public final void v(sd.l lVar) {
        w(lVar instanceof f ? (f) lVar : new e(lVar, 2));
    }

    /* JADX WARN: Code restructure failed: missing block: B:62:0x00b8, code lost:
    
        y(r10, r7);
     */
    /* JADX WARN: Code restructure failed: missing block: B:63:0x00bb, code lost:
    
        throw null;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void w(Object obj) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29436g;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (obj2 instanceof b) {
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, obj)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                return;
            }
            if ((obj2 instanceof f) || (obj2 instanceof kotlinx.coroutines.internal.s)) {
                break;
            }
            boolean z10 = obj2 instanceof q;
            if (z10) {
                q qVar = (q) obj2;
                qVar.getClass();
                if (!q.f29522b.compareAndSet(qVar, 0, 1)) {
                    y(obj, obj2);
                    throw null;
                }
                if (obj2 instanceof i) {
                    if (!z10) {
                        qVar = null;
                    }
                    Throwable th = qVar != null ? qVar.f29523a : null;
                    if (obj instanceof f) {
                        l((f) obj, th);
                        return;
                    } else {
                        nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.internal.Segment<*>");
                        n((kotlinx.coroutines.internal.s) obj, th);
                        return;
                    }
                }
                return;
            }
            if (!(obj2 instanceof p)) {
                if (obj instanceof kotlinx.coroutines.internal.s) {
                    return;
                }
                nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancelHandler");
                p pVar = new p(obj2, (f) obj, (sd.l) null, (CancellationException) null, 28);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, pVar)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                return;
            }
            p pVar2 = (p) obj2;
            if (pVar2.f29515b != null) {
                y(obj, obj2);
                throw null;
            }
            if (obj instanceof kotlinx.coroutines.internal.s) {
                return;
            }
            nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancelHandler");
            f fVar = (f) obj;
            Throwable th2 = pVar2.f29518e;
            if (th2 != null) {
                l(fVar, th2);
                return;
            }
            p pVarA = p.a(pVar2, fVar, null, 29);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, pVarA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj2) {
                    break;
                }
            }
            return;
        }
    }

    public final boolean x() {
        if (this.f29356c == 2) {
            Continuation continuation = this.f29438d;
            nb.g.g(continuation, "null cannot be cast to non-null type kotlinx.coroutines.internal.DispatchedContinuation<*>");
            if (kotlinx.coroutines.internal.g.f29460h.get((kotlinx.coroutines.internal.g) continuation) != null) {
                return true;
            }
        }
        return false;
    }

    public String z() {
        return "CancellableContinuation";
    }
}
