package d2;

import d2.p0;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/* JADX INFO: compiled from: DefaultExecutor.kt */
/* JADX INFO: loaded from: classes.dex */
public final class e0 extends p0 implements Runnable {
    private static volatile Thread _thread;
    private static volatile int debugStatus;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final e0 f4471k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static final long f4472l;

    static {
        Long l2;
        e0 e0Var = new e0();
        f4471k = e0Var;
        o0.s(e0Var, false, 1, null);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        try {
            l2 = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L);
        } catch (SecurityException unused) {
            l2 = 1000L;
        }
        f4472l = timeUnit.toNanos(l2.longValue());
    }

    private e0() {
    }

    private final synchronized void O() {
        if (R()) {
            debugStatus = 3;
            J();
            notifyAll();
        }
    }

    private final synchronized Thread P() {
        Thread thread;
        thread = _thread;
        if (thread == null) {
            thread = new Thread(this, "kotlinx.coroutines.DefaultExecutor");
            _thread = thread;
            thread.setDaemon(true);
            thread.start();
        }
        return thread;
    }

    private final boolean Q() {
        return debugStatus == 4;
    }

    private final boolean R() {
        int i3 = debugStatus;
        return i3 == 2 || i3 == 3;
    }

    private final synchronized boolean S() {
        if (R()) {
            return false;
        }
        debugStatus = 1;
        notifyAll();
        return true;
    }

    private final void T() {
        throw new RejectedExecutionException("DefaultExecutor was shut down. This error indicates that Dispatchers.shutdown() was invoked prior to completion of exiting coroutines, leaving coroutines in incomplete state. Please refer to Dispatchers.shutdown documentation for more details");
    }

    @Override // d2.p0
    public void D(Runnable runnable) {
        if (Q()) {
            T();
        }
        super.D(runnable);
    }

    @Override // java.lang.Runnable
    public void run() {
        boolean zG;
        n1.f4510a.c(this);
        c.a();
        try {
            if (!S()) {
                if (zG) {
                    return;
                } else {
                    return;
                }
            }
            long j3 = Long.MAX_VALUE;
            while (true) {
                Thread.interrupted();
                long jH = H();
                if (jH == Long.MAX_VALUE) {
                    c.a();
                    long jNanoTime = System.nanoTime();
                    if (j3 == Long.MAX_VALUE) {
                        j3 = f4472l + jNanoTime;
                    }
                    long j4 = j3 - jNanoTime;
                    if (j4 <= 0) {
                        _thread = null;
                        O();
                        c.a();
                        if (G()) {
                            return;
                        }
                        x();
                        return;
                    }
                    jH = z1.f.d(jH, j4);
                } else {
                    j3 = Long.MAX_VALUE;
                }
                if (jH > 0) {
                    if (R()) {
                        _thread = null;
                        O();
                        c.a();
                        if (G()) {
                            return;
                        }
                        x();
                        return;
                    }
                    c.a();
                    LockSupport.parkNanos(this, jH);
                }
            }
        } finally {
            _thread = null;
            O();
            c.a();
            if (!G()) {
                x();
            }
        }
    }

    @Override // d2.p0, d2.o0
    public void w() {
        debugStatus = 4;
        super.w();
    }

    @Override // d2.q0
    protected Thread x() {
        Thread thread = _thread;
        return thread == null ? P() : thread;
    }

    @Override // d2.q0
    protected void y(long j3, p0.a aVar) {
        T();
    }
}
