package F1;

import java.util.concurrent.CancellationException;
import n1.AbstractC0645a;
import n1.l;

/* JADX INFO: loaded from: classes.dex */
public abstract class U extends kotlinx.coroutines.scheduling.i {

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public int f591r;

    public U(int i2) {
        this.f591r = i2;
    }

    public abstract void a(Object obj, Throwable th);

    public abstract q1.d c();

    public Throwable d(Object obj) {
        C0143y c0143y = obj instanceof C0143y ? (C0143y) obj : null;
        if (c0143y == null) {
            return null;
        }
        return c0143y.f667a;
    }

    public Object e(Object obj) {
        return obj;
    }

    public final void f(Throwable th, Throwable th2) {
        if (th == null && th2 == null) {
            return;
        }
        if (th != null && th2 != null) {
            AbstractC0645a.a(th, th2);
        }
        if (th == null) {
            th = th2;
        }
        kotlin.jvm.internal.l.b(th);
        G.a(c().getContext(), new K("Fatal exception in coroutines machinery for " + this + ". Please read KDoc to 'handleFatalException' method and report this incident to maintainers", th));
    }

    public abstract Object h();

    @Override // java.lang.Runnable
    public final void run() {
        Object objB;
        Object objB2;
        kotlinx.coroutines.scheduling.j jVar = this.f5226g;
        try {
            kotlinx.coroutines.internal.e eVar = (kotlinx.coroutines.internal.e) c();
            q1.d dVar = eVar.f5150y;
            Object obj = eVar.f5148I;
            q1.g context = dVar.getContext();
            Object objC = kotlinx.coroutines.internal.v.c(context, obj);
            I0 i0E = objC != kotlinx.coroutines.internal.v.f5176a ? D.e(dVar, context, objC) : null;
            try {
                q1.g context2 = dVar.getContext();
                Object objH = h();
                Throwable thD = d(objH);
                n0 n0Var = (thD == null && V.b(this.f591r)) ? (n0) context2.get(n0.f630a) : null;
                if (n0Var != null && !n0Var.isActive()) {
                    CancellationException cancellationExceptionE = n0Var.e();
                    a(objH, cancellationExceptionE);
                    l.a aVar = n1.l.f5861g;
                    dVar.resumeWith(n1.l.b(n1.m.a(cancellationExceptionE)));
                } else if (thD != null) {
                    l.a aVar2 = n1.l.f5861g;
                    dVar.resumeWith(n1.l.b(n1.m.a(thD)));
                } else {
                    dVar.resumeWith(n1.l.b(e(objH)));
                }
                n1.s sVar = n1.s.f5871a;
                if (i0E == null || i0E.w0()) {
                    kotlinx.coroutines.internal.v.a(context, objC);
                }
                try {
                    jVar.b();
                    objB2 = n1.l.b(n1.s.f5871a);
                } catch (Throwable th) {
                    l.a aVar3 = n1.l.f5861g;
                    objB2 = n1.l.b(n1.m.a(th));
                }
                f(null, n1.l.d(objB2));
            } catch (Throwable th2) {
                if (i0E == null || i0E.w0()) {
                    kotlinx.coroutines.internal.v.a(context, objC);
                }
                throw th2;
            }
        } catch (Throwable th3) {
            try {
                l.a aVar4 = n1.l.f5861g;
                jVar.b();
                objB = n1.l.b(n1.s.f5871a);
            } catch (Throwable th4) {
                l.a aVar5 = n1.l.f5861g;
                objB = n1.l.b(n1.m.a(th4));
            }
            f(th3, n1.l.d(objB));
        }
    }
}
