package V4;

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

/* JADX INFO: loaded from: classes3.dex */
public final class F extends X implements Runnable {
    private static volatile Thread _thread;
    private static volatile int debugStatus;
    public static final F l;

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

    static {
        Long l6;
        F f6 = new F();
        l = f6;
        f6.A(false);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        try {
            l6 = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L);
        } catch (SecurityException unused) {
            l6 = 1000L;
        }
        f8240m = timeUnit.toNanos(l6.longValue());
    }

    @Override // V4.Y
    public final void D(long j4, V v3) {
        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 // V4.X
    public final void E(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.E(runnable);
    }

    public final synchronized void J() {
        int i6 = debugStatus;
        if (i6 == 2 || i6 == 3) {
            debugStatus = 3;
            X.f8272f.set(this, null);
            X.f8273j.set(this, null);
            notifyAll();
        }
    }

    @Override // V4.X, V4.J
    public final P b(long j4, I0 i02, A4.j jVar) {
        long j6 = j4 > 0 ? j4 >= 9223372036854L ? Long.MAX_VALUE : 1000000 * j4 : 0L;
        if (j6 >= 4611686018427387903L) {
            return y0.f8351a;
        }
        long jNanoTime = System.nanoTime();
        U u6 = new U(j6 + jNanoTime, i02);
        I(jNanoTime, u6);
        return u6;
    }

    @Override // java.lang.Runnable
    public final void run() {
        boolean zH;
        F0.f8241a.set(this);
        try {
            synchronized (this) {
                int i6 = debugStatus;
                if (i6 == 2 || i6 == 3) {
                    if (zH) {
                        return;
                    } else {
                        return;
                    }
                }
                debugStatus = 1;
                notifyAll();
                long j4 = Long.MAX_VALUE;
                while (true) {
                    Thread.interrupted();
                    long jB = B();
                    if (jB == Long.MAX_VALUE) {
                        long jNanoTime = System.nanoTime();
                        if (j4 == Long.MAX_VALUE) {
                            j4 = f8240m + jNanoTime;
                        }
                        long j6 = j4 - jNanoTime;
                        if (j6 <= 0) {
                            _thread = null;
                            J();
                            if (H()) {
                                return;
                            }
                            z();
                            return;
                        }
                        if (jB > j6) {
                            jB = j6;
                        }
                    } else {
                        j4 = Long.MAX_VALUE;
                    }
                    if (jB > 0) {
                        int i7 = debugStatus;
                        if (i7 == 2 || i7 == 3) {
                            _thread = null;
                            J();
                            if (H()) {
                                return;
                            }
                            z();
                            return;
                        }
                        LockSupport.parkNanos(this, jB);
                    }
                }
            }
        } finally {
            _thread = null;
            J();
            if (!H()) {
                z();
            }
        }
    }

    @Override // V4.X, V4.Y
    public final void shutdown() {
        debugStatus = 4;
        super.shutdown();
    }

    @Override // V4.AbstractC0786y
    public final String toString() {
        return "DefaultExecutor";
    }

    @Override // V4.Y
    public final Thread z() {
        Thread thread;
        Thread thread2 = _thread;
        if (thread2 != null) {
            return thread2;
        }
        synchronized (this) {
            thread = _thread;
            if (thread == null) {
                thread = new Thread(this, "kotlinx.coroutines.DefaultExecutor");
                _thread = thread;
                thread.setContextClassLoader(l.getClass().getClassLoader());
                thread.setDaemon(true);
                thread.start();
            }
        }
        return thread;
    }
}
