package g.a;

import g.a.c1;
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 n0 extends c1 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 n0 f4053k;
    private static final long l;

    static {
        Long l2;
        n0 n0Var = new n0();
        f4053k = n0Var;
        b1.J(n0Var, false, 1, null);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        try {
            l2 = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L);
        } catch (SecurityException unused) {
            l2 = 1000L;
        }
        l = timeUnit.toNanos(l2.longValue());
    }

    private n0() {
    }

    private final synchronized void p0() {
        if (s0()) {
            debugStatus = 3;
            j0();
            notifyAll();
        }
    }

    private final synchronized Thread q0() {
        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 r0() {
        return debugStatus == 4;
    }

    private final boolean s0() {
        int i2 = debugStatus;
        return i2 == 2 || i2 == 3;
    }

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

    private final void u0() {
        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 // g.a.d1
    protected Thread Q() {
        Thread thread = _thread;
        return thread == null ? q0() : thread;
    }

    @Override // g.a.d1
    protected void R(long j2, c1.b bVar) {
        u0();
        throw null;
    }

    @Override // g.a.c1
    public void W(Runnable runnable) {
        if (r0()) {
            u0();
            throw null;
        }
        super.W(runnable);
    }

    @Override // java.lang.Runnable
    public void run() {
        f.t tVar;
        boolean zD0;
        i2.a.c(this);
        b bVarA = c.a();
        if (bVarA != null) {
            bVarA.c();
        }
        try {
            if (!t0()) {
                if (zD0) {
                    return;
                } else {
                    return;
                }
            }
            long j2 = Long.MAX_VALUE;
            while (true) {
                Thread.interrupted();
                long jG0 = g0();
                if (jG0 == Long.MAX_VALUE) {
                    b bVarA2 = c.a();
                    long jA = bVarA2 != null ? bVarA2.a() : System.nanoTime();
                    if (j2 == Long.MAX_VALUE) {
                        j2 = l + jA;
                    }
                    long j3 = j2 - jA;
                    if (j3 <= 0) {
                        _thread = null;
                        p0();
                        b bVarA3 = c.a();
                        if (bVarA3 != null) {
                            bVarA3.g();
                        }
                        if (d0()) {
                            return;
                        }
                        Q();
                        return;
                    }
                    jG0 = f.b0.n.d(jG0, j3);
                } else {
                    j2 = Long.MAX_VALUE;
                }
                if (jG0 > 0) {
                    if (s0()) {
                        _thread = null;
                        p0();
                        b bVarA4 = c.a();
                        if (bVarA4 != null) {
                            bVarA4.g();
                        }
                        if (d0()) {
                            return;
                        }
                        Q();
                        return;
                    }
                    b bVarA5 = c.a();
                    if (bVarA5 != null) {
                        bVarA5.b(this, jG0);
                        tVar = f.t.a;
                    } else {
                        tVar = null;
                    }
                    if (tVar == null) {
                        LockSupport.parkNanos(this, jG0);
                    }
                }
            }
        } finally {
            _thread = null;
            p0();
            b bVarA6 = c.a();
            if (bVarA6 != null) {
                bVarA6.g();
            }
            if (!d0()) {
                Q();
            }
        }
    }

    @Override // g.a.c1, g.a.b1
    public void shutdown() {
        debugStatus = 4;
        super.shutdown();
    }
}
