package s.coroutines;

import g.h.a.i;
import java.util.concurrent.CancellationException;
import kotlin.Metadata;
import kotlin.Result;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.jvm.internal.o;
import kotlin.reflect.w.internal.y0.n.c2.c;
import s.coroutines.Job;
import s.coroutines.internal.DispatchedContinuation;
import s.coroutines.internal.u;
import s.coroutines.scheduling.Task;
import s.coroutines.scheduling.TaskContext;

/* JADX INFO: renamed from: s.a.k0, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0003\n\u0002\b\u000e\b \u0018\u0000*\u0006\b\u0000\u0010\u0001 \u00002\u00060\u0002j\u0002`\u0003B\r\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\u001f\u0010\u000b\u001a\u00020\f2\b\u0010\r\u001a\u0004\u0018\u00010\u000e2\u0006\u0010\u000f\u001a\u00020\u0010H\u0010¢\u0006\u0002\b\u0011J\u0019\u0010\u0012\u001a\u0004\u0018\u00010\u00102\b\u0010\u0013\u001a\u0004\u0018\u00010\u000eH\u0010¢\u0006\u0002\b\u0014J\u001f\u0010\u0015\u001a\u0002H\u0001\"\u0004\b\u0001\u0010\u00012\b\u0010\u0013\u001a\u0004\u0018\u00010\u000eH\u0010¢\u0006\u0004\b\u0016\u0010\u0017J\u001a\u0010\u0018\u001a\u00020\f2\b\u0010\u0019\u001a\u0004\u0018\u00010\u00102\b\u0010\u001a\u001a\u0004\u0018\u00010\u0010J\u0006\u0010\u001b\u001a\u00020\fJ\u000f\u0010\u001c\u001a\u0004\u0018\u00010\u000eH ¢\u0006\u0002\b\u001dR\u0018\u0010\u0007\u001a\b\u0012\u0004\u0012\u00028\u00000\bX \u0004¢\u0006\u0006\u001a\u0004\b\t\u0010\nR\u0012\u0010\u0004\u001a\u00020\u00058\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000¨\u0006\u001e"}, d2 = {"Lkotlinx/coroutines/DispatchedTask;", "T", "Lkotlinx/coroutines/scheduling/Task;", "Lkotlinx/coroutines/SchedulerTask;", "resumeMode", "", "(I)V", "delegate", "Lkotlin/coroutines/Continuation;", "getDelegate$kotlinx_coroutines_core", "()Lkotlin/coroutines/Continuation;", "cancelCompletedResult", "", "takenState", "", "cause", "", "cancelCompletedResult$kotlinx_coroutines_core", "getExceptionalResult", "state", "getExceptionalResult$kotlinx_coroutines_core", "getSuccessfulResult", "getSuccessfulResult$kotlinx_coroutines_core", "(Ljava/lang/Object;)Ljava/lang/Object;", "handleFatalException", "exception", "finallyException", "run", "takeState", "takeState$kotlinx_coroutines_core", "kotlinx-coroutines-core"}, k = 1, mv = {1, 6, 0}, xi = 48)
public abstract class DispatchedTask<T> extends Task {

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

    public DispatchedTask(int i2) {
        this.f9453n = i2;
    }

    public void a(Object obj, Throwable th) {
    }

    public abstract Continuation<T> c();

    public Throwable d(Object obj) {
        CompletedExceptionally completedExceptionally = obj instanceof CompletedExceptionally ? (CompletedExceptionally) obj : null;
        if (completedExceptionally != null) {
            return completedExceptionally.f9464b;
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public <T> T f(Object obj) {
        return obj;
    }

    public final void g(Throwable th, Throwable th2) {
        if (th == null && th2 == null) {
            return;
        }
        if (th != null && th2 != null) {
            i.p(th, th2);
        }
        if (th == null) {
            th = th2;
        }
        o.c(th);
        c.S(c().getF9433r(), new CoroutinesInternalError("Fatal exception in coroutines machinery for " + this + ". Please read KDoc to 'handleFatalException' method and report this incident to maintainers", th));
    }

    /* JADX INFO: renamed from: h */
    public abstract Object get_state();

    @Override // java.lang.Runnable
    public final void run() {
        Object objQ0;
        Job job;
        Object objQ02;
        TaskContext taskContext = this.f9408m;
        try {
            DispatchedContinuation dispatchedContinuation = (DispatchedContinuation) c();
            Continuation<T> continuation = dispatchedContinuation.f9334q;
            Object obj = dispatchedContinuation.f9336s;
            CoroutineContext context = continuation.getF9433r();
            Object objB = u.b(context, obj);
            UndispatchedCoroutine<?> undispatchedCoroutineQ0 = objB != u.a ? c.Q0(continuation, context, objB) : null;
            try {
                CoroutineContext context2 = continuation.getF9433r();
                Object objH = get_state();
                Throwable thD = d(objH);
                if (thD == null && e.a(this.f9453n)) {
                    int i2 = Job.f9422l;
                    job = (Job) context2.get(Job.a.f9423f);
                } else {
                    job = null;
                }
                if (job == null || job.isActive()) {
                    objQ02 = thD != null ? i.q0(thD) : f(objH);
                } else {
                    CancellationException cancellationExceptionH = job.h();
                    a(objH, cancellationExceptionH);
                    objQ02 = i.q0(cancellationExceptionH);
                }
                continuation.resumeWith(objQ02);
                Object objQ03 = Unit.a;
                if (undispatchedCoroutineQ0 == null || undispatchedCoroutineQ0.V()) {
                    u.a(context, objB);
                }
                try {
                    taskContext.a();
                } catch (Throwable th) {
                    objQ03 = i.q0(th);
                }
                g(null, Result.a(objQ03));
            } catch (Throwable th2) {
                if (undispatchedCoroutineQ0 == null || undispatchedCoroutineQ0.V()) {
                    u.a(context, objB);
                }
                throw th2;
            }
        } catch (Throwable th3) {
            try {
                taskContext.a();
                objQ0 = Unit.a;
            } catch (Throwable th4) {
                objQ0 = i.q0(th4);
            }
            g(th3, Result.a(objQ0));
        }
    }
}
