package h.a;

import h.a.g0;
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 */
@g.i
public final class v extends g0 implements Runnable {
    private static volatile Thread _thread;
    private static volatile int debugStatus;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final v f4330j;

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

    static {
        Long l2;
        v vVar = new v();
        f4330j = vVar;
        vVar.x(false);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        try {
            l2 = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L);
        } catch (SecurityException unused) {
            l2 = 1000L;
        }
        f4331k = timeUnit.toNanos(l2.longValue());
    }

    @Override // h.a.g0, h.a.f0
    public void A() {
        debugStatus = 4;
        super.A();
    }

    @Override // h.a.h0
    public Thread D() {
        Thread thread = _thread;
        if (thread == null) {
            synchronized (this) {
                thread = _thread;
                if (thread == null) {
                    thread = new Thread(this, "kotlinx.coroutines.DefaultExecutor");
                    _thread = thread;
                    thread.setDaemon(true);
                    thread.start();
                }
            }
        }
        return thread;
    }

    @Override // h.a.h0
    public void E(long j2, g0.a aVar) {
        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 // h.a.g0
    public void G(Runnable runnable) {
        if (debugStatus == 4) {
            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");
        }
        super.G(runnable);
    }

    public final synchronized void Q() {
        if (S()) {
            debugStatus = 3;
            O();
            notifyAll();
        }
    }

    public final boolean S() {
        int i2 = debugStatus;
        return i2 == 2 || i2 == 3;
    }

    @Override // java.lang.Runnable
    public void run() {
        boolean z;
        boolean zL;
        d1 d1Var = d1.a;
        d1.f4209b.set(this);
        try {
            synchronized (this) {
                if (S()) {
                    z = false;
                } else {
                    z = true;
                    debugStatus = 1;
                    notifyAll();
                }
            }
            if (!z) {
                if (zL) {
                    return;
                } else {
                    return;
                }
            }
            long j2 = Long.MAX_VALUE;
            while (true) {
                Thread.interrupted();
                long jN = N();
                if (jN == Long.MAX_VALUE) {
                    long jNanoTime = System.nanoTime();
                    if (j2 == Long.MAX_VALUE) {
                        j2 = f4331k + jNanoTime;
                    }
                    long j3 = j2 - jNanoTime;
                    if (j3 <= 0) {
                        _thread = null;
                        Q();
                        if (L()) {
                            return;
                        }
                        D();
                        return;
                    }
                    if (jN > j3) {
                        jN = j3;
                    }
                } else {
                    j2 = Long.MAX_VALUE;
                }
                if (jN > 0) {
                    if (S()) {
                        _thread = null;
                        Q();
                        if (L()) {
                            return;
                        }
                        D();
                        return;
                    }
                    LockSupport.parkNanos(this, jN);
                }
            }
        } finally {
            _thread = null;
            Q();
            if (!L()) {
                D();
            }
        }
    }
}
