package kotlinx.coroutines;

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 t0 extends g1 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 t0 f1168k;
    private static final long l;

    static {
        Long l2;
        t0 t0Var = new t0();
        f1168k = t0Var;
        f1.T(t0Var, 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 t0() {
    }

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

    private final synchronized void x0() {
        if (z0()) {
            debugStatus = 3;
            s0();
            notifyAll();
        }
    }

    private final synchronized Thread y0() {
        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 z0() {
        int i2 = debugStatus;
        return i2 == 2 || i2 == 3;
    }

    @Override // kotlinx.coroutines.h1
    protected Thread c0() {
        Thread thread = _thread;
        return thread == null ? y0() : thread;
    }

    @Override // java.lang.Runnable
    public void run() {
        boolean zQ0;
        j2.a.d(this);
        d dVarA = e.a();
        if (dVarA != null) {
            dVarA.c();
        }
        try {
            if (!A0()) {
                if (zQ0) {
                    return;
                } else {
                    return;
                }
            }
            long j2 = Long.MAX_VALUE;
            while (true) {
                Thread.interrupted();
                long jW = W();
                if (jW == Long.MAX_VALUE) {
                    d dVarA2 = e.a();
                    long jNanoTime = dVarA2 == null ? System.nanoTime() : dVarA2.a();
                    if (j2 == Long.MAX_VALUE) {
                        j2 = l + jNanoTime;
                    }
                    long j3 = j2 - jNanoTime;
                    if (j3 <= 0) {
                        _thread = null;
                        x0();
                        d dVarA3 = e.a();
                        if (dVarA3 != null) {
                            dVarA3.g();
                        }
                        if (q0()) {
                            return;
                        }
                        c0();
                        return;
                    }
                    jW = h.z.n.e(jW, j3);
                } else {
                    j2 = Long.MAX_VALUE;
                }
                if (jW > 0) {
                    if (z0()) {
                        _thread = null;
                        x0();
                        d dVarA4 = e.a();
                        if (dVarA4 != null) {
                            dVarA4.g();
                        }
                        if (q0()) {
                            return;
                        }
                        c0();
                        return;
                    }
                    d dVarA5 = e.a();
                    if (dVarA5 == null) {
                        LockSupport.parkNanos(this, jW);
                    } else {
                        dVarA5.b(this, jW);
                    }
                }
            }
        } finally {
            _thread = null;
            x0();
            d dVarA6 = e.a();
            if (dVarA6 != null) {
                dVarA6.g();
            }
            if (!q0()) {
                c0();
            }
        }
    }
}
