package Z6;

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

/* JADX INFO: loaded from: classes.dex */
public final class A extends T implements Runnable {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final long f7466A;
    private static volatile Thread _thread;
    private static volatile int debugStatus;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public static final A f7467z;

    static {
        Long l8;
        A a3 = new A();
        f7467z = a3;
        a3.o0(false);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        try {
            l8 = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L);
        } catch (SecurityException unused) {
            l8 = 1000L;
        }
        f7466A = timeUnit.toNanos(l8.longValue());
    }

    @Override // Z6.U
    public final Thread n0() {
        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(f7467z.getClass().getClassLoader());
                thread.setDaemon(true);
                thread.start();
            }
        }
        return thread;
    }

    @Override // Z6.U
    public final void r0(long j8, Q q8) {
        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 // java.lang.Runnable
    public final void run() {
        boolean zV0;
        t0.f7547a.set(this);
        try {
            synchronized (this) {
                int i = debugStatus;
                if (i == 2 || i == 3) {
                    if (zV0) {
                        return;
                    } else {
                        return;
                    }
                }
                debugStatus = 1;
                notifyAll();
                long j8 = Long.MAX_VALUE;
                while (true) {
                    Thread.interrupted();
                    long jP0 = p0();
                    if (jP0 == Long.MAX_VALUE) {
                        long jNanoTime = System.nanoTime();
                        if (j8 == Long.MAX_VALUE) {
                            j8 = f7466A + jNanoTime;
                        }
                        long j9 = j8 - jNanoTime;
                        if (j9 <= 0) {
                            _thread = null;
                            x0();
                            if (v0()) {
                                return;
                            }
                            n0();
                            return;
                        }
                        if (jP0 > j9) {
                            jP0 = j9;
                        }
                    } else {
                        j8 = Long.MAX_VALUE;
                    }
                    if (jP0 > 0) {
                        int i5 = debugStatus;
                        if (i5 == 2 || i5 == 3) {
                            _thread = null;
                            x0();
                            if (v0()) {
                                return;
                            }
                            n0();
                            return;
                        }
                        LockSupport.parkNanos(this, jP0);
                    }
                }
            }
        } finally {
            _thread = null;
            x0();
            if (!v0()) {
                n0();
            }
        }
    }

    @Override // Z6.T
    public final void s0(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.s0(runnable);
    }

    @Override // Z6.T, Z6.U
    public final void shutdown() {
        debugStatus = 4;
        super.shutdown();
    }

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

    @Override // Z6.T, Z6.E
    public final L u(long j8, Runnable runnable, E6.i iVar) {
        long j9 = j8 > 0 ? j8 >= 9223372036854L ? Long.MAX_VALUE : 1000000 * j8 : 0L;
        if (j9 >= 4611686018427387903L) {
            return o0.f7534q;
        }
        long jNanoTime = System.nanoTime();
        P p3 = new P(runnable, j9 + jNanoTime);
        w0(jNanoTime, p3);
        return p3;
    }

    public final synchronized void x0() {
        int i = debugStatus;
        if (i == 2 || i == 3) {
            debugStatus = 3;
            T.f7485w.set(this, null);
            T.f7486x.set(this, null);
            notifyAll();
        }
    }
}
